diff --git a/plugin-modernizer-cli/pom.xml b/plugin-modernizer-cli/pom.xml index f8a49dba..b73db8d4 100644 --- a/plugin-modernizer-cli/pom.xml +++ b/plugin-modernizer-cli/pom.xml @@ -12,7 +12,6 @@ plugin-modernizer-cli Plugin Modernizer CLI Interface - io.jenkins.plugin-modernizer @@ -44,10 +43,54 @@ org.slf4j slf4j-api + + + org.apache.maven + apache-maven + test + bin + zip + ${maven.version} + + + org.apache.maven.plugins + maven-surefire-plugin + + + ${project.build.directory}/apache-maven-${maven.version} + ${project.build.directory}/apache-maven-${maven.version} + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + unpack + + unpack + + process-test-resources + + + + org.apache.maven + apache-maven + bin + zip + false + ${project.build.directory} + + + + + + maven-assembly-plugin diff --git a/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java b/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java index 04f03274..7ae0edac 100644 --- a/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java +++ b/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java @@ -1,8 +1,10 @@ package io.jenkins.tools.pluginmodernizer.cli; +import java.nio.file.Path; import java.util.List; import io.jenkins.tools.pluginmodernizer.core.config.Config; +import io.jenkins.tools.pluginmodernizer.core.config.Settings; import io.jenkins.tools.pluginmodernizer.core.impl.PluginModernizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +39,12 @@ public static void main(final String[] args) { @Option(names = {"-d", "--debug"}, description = "Enable debug logging.") public boolean debug; + @Option(names = {"-c", "--cache-path"}, description = "Path to the cache directory.") + public Path cachePath = Settings.DEFAULT_CACHE_PATH; + + @Option(names = {"-m", "--maven-home"}, description = "Path to the Maven Home directory.") + public Path mavenHome = Settings.DEFAULT_MAVEN_HOME; + public Config setup() { Config.DEBUG = debug; return Config.builder() @@ -44,6 +52,8 @@ public Config setup() { .withPlugins(plugins) .withRecipes(recipes) .withDryRun(dryRun) + .withCachePath(cachePath) + .withMavenHome(mavenHome) .build(); } diff --git a/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java b/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java index 9de9d916..06e330a3 100644 --- a/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java +++ b/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java @@ -3,6 +3,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.io.IOException; +import java.nio.file.Files; import java.util.List; import org.junit.jupiter.api.Test; @@ -50,4 +52,12 @@ public void testMissingRecipesArgument() { int exitCode = new CommandLine(main).execute(args); assertEquals(CommandLine.ExitCode.USAGE, exitCode); } + + @Test + public void testMavenHome() throws IOException { + String[] args = {"--maven-home", Files.createTempDirectory("unsued").toString()}; + Main main = new Main(); + int exitCode = new CommandLine(main).execute(args); + assertEquals(CommandLine.ExitCode.USAGE, exitCode); + } } \ No newline at end of file diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Config.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Config.java index 55f9d7e3..6816f609 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Config.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Config.java @@ -10,17 +10,15 @@ public class Config { private final List plugins; private final List recipes; private final Path cachePath; - private final String mavenHome; - private final String mavenRewritePluginVersion; + private final Path mavenHome; private final boolean dryRun; - private Config(String version, List plugins, List recipes, Path cachePath, String mavenHome, String mavenRewritePluginVersion, boolean dryRun) { + private Config(String version, List plugins, List recipes, Path cachePath, Path mavenHome, boolean dryRun) { this.version = version; this.plugins = plugins; this.recipes = recipes; this.cachePath = cachePath; this.mavenHome = mavenHome; - this.mavenRewritePluginVersion = mavenRewritePluginVersion; this.dryRun = dryRun; } @@ -40,14 +38,10 @@ public Path getCachePath() { return cachePath; } - public String getMavenHome() { + public Path getMavenHome() { return mavenHome; } - public String getMavenPluginVersion() { - return mavenRewritePluginVersion; - } - public boolean isDryRun() { return dryRun; } @@ -61,8 +55,7 @@ public static class Builder { private List plugins; private List recipes; private Path cachePath = Settings.DEFAULT_CACHE_PATH; - private String mavenHome = Settings.MAVEN_HOME_PATH; - private String mavenRewritePluginVersion = Settings.MAVEN_REWRITE_PLUGIN_VERSION; + private Path mavenHome = Settings.DEFAULT_MAVEN_HOME; private boolean dryRun = false; public Builder withVersion(String version) { @@ -81,17 +74,16 @@ public Builder withRecipes(List recipes) { } public Builder withCachePath(Path cachePath) { - this.cachePath = cachePath; - return this; - } - - public Builder withMavenHome(String mavenHome) { - this.mavenHome = mavenHome; + if (cachePath != null) { + this.cachePath = cachePath; + } return this; } - public Builder withMavenPluginVersion(String mavenPluginVersion) { - this.mavenRewritePluginVersion = mavenPluginVersion; + public Builder withMavenHome(Path mavenHome) { + if (mavenHome != null) { + this.mavenHome = mavenHome; + } return this; } @@ -101,7 +93,7 @@ public Builder withDryRun(boolean dryRun) { } public Config build() { - return new Config(version, plugins, recipes, cachePath, mavenHome, mavenRewritePluginVersion, dryRun); + return new Config(version, plugins, recipes, cachePath, mavenHome, dryRun); } } diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java index 580fef59..c247e18e 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java @@ -7,7 +7,7 @@ public class Settings { public static final Path DEFAULT_CACHE_PATH; - public static final String MAVEN_HOME_PATH; + public static final Path DEFAULT_MAVEN_HOME; public static final String MAVEN_REWRITE_PLUGIN_VERSION = "5.34.1"; @@ -23,14 +23,17 @@ public class Settings { } else { DEFAULT_CACHE_PATH = Paths.get(cacheDirFromEnv); } - MAVEN_HOME_PATH = getMavenHomePath(); + DEFAULT_MAVEN_HOME = getDefaultMavenHome(); } - private static String getMavenHomePath() { + private static Path getDefaultMavenHome() { String mavenHome = System.getenv("MAVEN_HOME"); if (mavenHome == null) { mavenHome = System.getenv("M2_HOME"); } - return mavenHome; + if (mavenHome == null) { + return null; + } + return Path.of(mavenHome); } } diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/impl/MavenInvoker.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/impl/MavenInvoker.java index 8721443b..baec65ff 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/impl/MavenInvoker.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/impl/MavenInvoker.java @@ -5,7 +5,6 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -14,6 +13,7 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.jenkins.tools.pluginmodernizer.core.config.Config; +import io.jenkins.tools.pluginmodernizer.core.config.Settings; import org.apache.maven.shared.invoker.DefaultInvocationRequest; import org.apache.maven.shared.invoker.DefaultInvoker; import org.apache.maven.shared.invoker.InvocationRequest; @@ -57,7 +57,7 @@ public void invokeRewrite(String plugin, String pluginPath) { private List createGoalsList() throws IOException { List goals = new ArrayList<>(); - String mavenPluginVersion = config.getMavenPluginVersion(); + String mavenPluginVersion = Settings.MAVEN_REWRITE_PLUGIN_VERSION; String mode = config.isDryRun() ? "dryRun" : "run"; goals.add("org.openrewrite.maven:rewrite-maven-plugin:" + mavenPluginVersion + ":" + mode); @@ -109,7 +109,7 @@ private void invokeGoals(String plugin, String pluginPath, List goals) { } Invoker invoker = new DefaultInvoker(); - invoker.setMavenHome(new File(config.getMavenHome())); + invoker.setMavenHome(config.getMavenHome().toFile()); try { InvocationRequest request = createInvocationRequest(pluginPath, goals); request.setBatchMode(true); @@ -128,20 +128,14 @@ private void invokeGoals(String plugin, String pluginPath, List goals) { } private boolean validateMavenHome() { - String mavenHome = config.getMavenHome(); + Path mavenHome = config.getMavenHome(); if (mavenHome == null) { LOG.error("Neither MAVEN_HOME nor M2_HOME environment variables are set."); return false; } - try { - Path mavenHomePath = Paths.get(mavenHome).toRealPath(); - if (!Files.isDirectory(mavenHomePath) || !Files.isExecutable(mavenHomePath.resolve("bin/mvn"))) { - LOG.error("Invalid Maven home directory. Aborting build."); - return false; - } - } catch (IOException e) { - LOG.error("Error validating Maven home directory: ", e); + if (!Files.isDirectory(mavenHome) || !Files.isExecutable(mavenHome.resolve("bin/mvn"))) { + LOG.error("Invalid Maven home directory. Aborting build."); return false; } diff --git a/pom.xml b/pom.xml index 0bfdb57c..e7bd80b2 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,7 @@ 9.7 2.11.0 2.16.1 + 3.9.8