Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused config and override maven home and cache on CLI #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion plugin-modernizer-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<artifactId>plugin-modernizer-cli</artifactId>
<name>Plugin Modernizer CLI Interface</name>


<dependencies>
<dependency>
<groupId>io.jenkins.plugin-modernizer</groupId>
Expand Down Expand Up @@ -44,10 +43,54 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>apache-maven</artifactId>
<scope>test</scope>
<classifier>bin</classifier>
<type>zip</type>
<version>${maven.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<MAVEN_HOME>${project.build.directory}/apache-maven-${maven.version}</MAVEN_HOME>
<M2_HOME>${project.build.directory}/apache-maven-${maven.version}</M2_HOME>
</environmentVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<goals>
<goal>unpack</goal>
</goals>
<phase>process-test-resources</phase>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.maven</groupId>
<artifactId>apache-maven</artifactId>
<classifier>bin</classifier>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<!-- version specified in parent pom -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,13 +39,21 @@ 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()
.withVersion(getVersion())
.withPlugins(plugins)
.withRecipes(recipes)
.withDryRun(dryRun)
.withCachePath(cachePath)
.withMavenHome(mavenHome)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ public class Config {
private final List<String> plugins;
private final List<String> 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<String> plugins, List<String> recipes, Path cachePath, String mavenHome, String mavenRewritePluginVersion, boolean dryRun) {
private Config(String version, List<String> plugins, List<String> 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;
}

Expand All @@ -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;
}
Expand All @@ -61,8 +55,7 @@ public static class Builder {
private List<String> plugins;
private List<String> 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) {
Expand All @@ -81,17 +74,16 @@ public Builder withRecipes(List<String> 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;
}

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void invokeRewrite(String plugin, String pluginPath) {

private List<String> createGoalsList() throws IOException {
List<String> 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);

Expand Down Expand Up @@ -109,7 +109,7 @@ private void invokeGoals(String plugin, String pluginPath, List<String> 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);
Expand All @@ -128,20 +128,14 @@ private void invokeGoals(String plugin, String pluginPath, List<String> 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;
}

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<asm.version>9.7</asm.version>
<gson.version>2.11.0</gson.version>
<dataformat.version>2.16.1</dataformat.version>
<maven.version>3.9.8</maven.version>
</properties>

<repositories>
Expand Down