Skip to content

Commit

Permalink
devonfw#1517 implemented requested changes
Browse files Browse the repository at this point in the history
saved the return of Systemutils.getUserHome() and determineMvnPath in a static variable to avoid multiple calls of the method
  • Loading branch information
jan-vcapgemini committed Nov 9, 2022
1 parent 87504b1 commit 827fc1a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public class MavenUtil {
/** Logger instance. */
private static final Logger LOG = LoggerFactory.getLogger(MavenUtil.class);

/** Stores the user home */
private static File userHome;

/** Stores the maven path */
private static Path mavenPath;

/**
* Executes a Maven class path build command which will download all the transitive dependencies needed for the CLI
*
Expand All @@ -51,7 +57,7 @@ public static void cacheMavenClassPath(Path pomFile, Path cpFile) {

LOG.info("Calculating class path for {} and downloading the needed maven dependencies. Please be patient...",
pomFile);
List<String> args = Lists.newArrayList(SystemUtil.determineMvnPath().toString(), "dependency:build-classpath",
List<String> args = Lists.newArrayList(determineMvnPath().toString(), "dependency:build-classpath",
"-Dmdep.outputFile=" + cpFile.toString());
if (pomFile.getFileSystem().provider().getClass().getSimpleName().equals("ZipFileSystemProvider")) {
pomFile = createCachedPomFromJar(pomFile, cpFile.getParent());
Expand All @@ -74,7 +80,7 @@ public static void resolveDependencies(Path mvnProjectRoot) {
LOG.info(
"Resolving maven dependencies for maven project {} to be able to make use of reflection in templates. Please be patient...",
mvnProjectRoot);
List<String> args = Lists.newArrayList(SystemUtil.determineMvnPath().toString(), "dependency:resolve");
List<String> args = Lists.newArrayList(determineMvnPath().toString(), "dependency:resolve");
runCommand(mvnProjectRoot, args);
LOG.debug("Downloaded dependencies successfully.");
}
Expand All @@ -96,7 +102,7 @@ public static void resolveDependencies(Path pomFile, Path mvnProjectRoot) {
pomFile = createCachedPomFromJar(pomFile, mvnProjectRoot);
}

List<String> args = Lists.newArrayList(SystemUtil.determineMvnPath().toString());
List<String> args = Lists.newArrayList(determineMvnPath().toString());
args.add("-f");
args.add(pomFile.toString());
args.add("dependency:resolve");
Expand Down Expand Up @@ -182,15 +188,40 @@ public static String generatePomFileHash(Path pomFile) {
return pomFileHash;
}

/**
* Determines maven home path if it was not set yet and returns it
*
* @return Path to maven home
*/
private static Path determineMvnPath() {

if (mavenPath == null) {
mavenPath = SystemUtil.determineMvnPath();
}
return mavenPath;
}

/**
* Determines maven home path if it was not set yet and returns it
*
* @return File path to user home
*/
private static File getUserHome() {

if (userHome == null) {
userHome = SystemUtils.getUserHome();
}
return userHome;
}

/**
* @return the maven repository path
*/
public static Path determineMavenRepositoryPath() {

LOG.info("Determine maven repository path");
String m2Repo = runCommand(SystemUtils.getUserHome().toPath(),
Lists.newArrayList(SystemUtil.determineMvnPath().toString(), "help:evaluate",
"-Dexpression=settings.localRepository", "-DforceStdout"));
String m2Repo = runCommand(getUserHome().toPath(), Lists.newArrayList(determineMvnPath().toString(),
"help:evaluate", "-Dexpression=settings.localRepository", "-DforceStdout"));
LOG.debug("Determined {} as maven repository path.", m2Repo);
return Paths.get(m2Repo);
}
Expand All @@ -203,8 +234,8 @@ public static Path determineMavenRepositoryPath() {
public static String determineMavenSettings() {

LOG.info("Determine content of maven's settings.xml");
String mavenSettings = runCommand(SystemUtils.getUserHome().toPath(), Lists.newArrayList(
SystemUtil.determineMvnPath().toString(), "help:evaluate", "-Dexpression=settings", "-DforceStdout"));
String mavenSettings = runCommand(getUserHome().toPath(),
Lists.newArrayList(determineMvnPath().toString(), "help:evaluate", "-Dexpression=settings", "-DforceStdout"));
return mavenSettings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.devonfw.cobigen.retriever.settings.to.model.MavenSettingsServerModel;

/**
* Utils to obtain maven artifact download URLs
* Used to obtain maven artifact download URLs
*/
public class ArtifactRetriever {

Expand Down

0 comments on commit 827fc1a

Please sign in to comment.