Skip to content

Commit

Permalink
Merge pull request #64 from jonesbusy/feature/maven-version-check
Browse files Browse the repository at this point in the history
Maven version validation
  • Loading branch information
jonesbusy authored Jul 7, 2024
2 parents 34972ba + 82f4565 commit 47a0969
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Here, `plugin1` and `plugin2` are the names of plugin directories, and `AddPlugi

- `--cache-path` or `-c`: (optional) Custom path to the cache directory

- `--maven-home` or `-m`: (optional) Path to the Maven home directory. Required if both `MAVEN_HOME` and `M2_HOME` environment variables are not set.
- `--maven-home` or `-m`: (optional) Path to the Maven home directory. Required if both `MAVEN_HOME` and `M2_HOME` environment variables are not set. The minimum required version is 3.9.7.

- `--version` or `-v`: (optional) Displays the version of the Plugin Modernizer tool.

Expand Down
4 changes: 4 additions & 0 deletions plugin-modernizer-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.openrewrite</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.maven.artifact.versioning.ComparableVersion;

Check warning on line 14 in plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java

View check run for this annotation

ci.jenkins.io / CheckStyle

CustomImportOrderCheck

ERROR: Extra separation in import group before 'org.apache.maven.artifact.versioning.ComparableVersion'
Raw output
<p>Since Checkstyle 5.8</p><p> Checks that the groups of import declarations appear in the order specified by the user. If there is an import but its group is not specified in the configuration such an import should be placed at the end of the import list. </p><p><a href="#CustomImportOrder_Examples">Examples section</a> contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans </p>

Check warning on line 14 in plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java

View check run for this annotation

ci.jenkins.io / CheckStyle

CustomImportOrderCheck

ERROR: Wrong lexicographical order for 'org.apache.maven.artifact.versioning.ComparableVersion' import. Should be before 'org.slf4j.LoggerFactory'.
Raw output
<p>Since Checkstyle 5.8</p><p> Checks that the groups of import declarations appear in the order specified by the user. If there is an import but its group is not specified in the configuration such an import should be placed at the end of the import list. </p><p><a href="#CustomImportOrder_Examples">Examples section</a> contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans </p>

public class Settings {

private static final Logger LOG = LoggerFactory.getLogger(Settings.class);
Expand All @@ -23,6 +25,8 @@ public class Settings {

public static final String RECIPE_DATA_YAML_PATH = "recipe_data.yaml";

public static final ComparableVersion MAVEN_MINIMAL_VERSION = new ComparableVersion("3.9.7");

static {
String cacheBaseDir = System.getProperty("user.home");
if (cacheBaseDir == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import edu.umd.cs.findbugs.annotations.Nullable;
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.artifact.versioning.ComparableVersion;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
Expand All @@ -35,6 +38,27 @@ public class MavenInvoker {

public MavenInvoker(Config config) {
this.config = config;
validateMavenHome();
validateMavenVersion();
}

public @Nullable ComparableVersion getMavenVersion() {
AtomicReference<String> version = new AtomicReference<>();
try {
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(config.getMavenHome().toFile());
InvocationRequest request = new DefaultInvocationRequest();
request.setBatchMode(true);
request.addArg("-q");
request.addArg("--version");
request.setOutputHandler(version::set);
invoker.execute(request);
return new ComparableVersion(version.get());
}
catch (MavenInvocationException e) {
LOG.error("Failed to check for maven version", e);
return null;
}
}

public void invokeGoal(String plugin, String pluginPath, String goal) {
Expand Down Expand Up @@ -104,10 +128,6 @@ private List<String> getRecipeArtifactCoordinates(List<String> recipes, ArrayNod
}

private void invokeGoals(String plugin, String pluginPath, List<String> goals) {
if (!validateMavenHome()) {
return;
}

Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(config.getMavenHome().toFile());
try {
Expand All @@ -127,19 +147,38 @@ private void invokeGoals(String plugin, String pluginPath, List<String> goals) {
}
}

private boolean validateMavenHome() {
/**
* Validate the Maven home directory.
* @throws IllegalArgumentException if the Maven home directory is not set or invalid.
*/
private void validateMavenHome() {
Path mavenHome = config.getMavenHome();
if (mavenHome == null) {
LOG.error("Neither MAVEN_HOME nor M2_HOME environment variables are set.");
return false;
throw new IllegalArgumentException("Maven home directory not set.");
}

if (!Files.isDirectory(mavenHome) || !Files.isExecutable(mavenHome.resolve("bin/mvn"))) {
LOG.error("Invalid Maven home directory. Aborting build.");
return false;
throw new IllegalArgumentException("Invalid Maven home directory.");
}
}

return true;
/**
* Validate the Maven version.
* @throws IllegalArgumentException if the Maven version is too old or cannot be determined.
*/
private void validateMavenVersion() {
ComparableVersion mavenVersion = getMavenVersion();
LOG.debug("Maven version detected: {}", mavenVersion);
if (mavenVersion == null) {
LOG.error("Failed to check Maven version. Aborting build.");
throw new IllegalArgumentException("Failed to check Maven version.");
}
if (mavenVersion.compareTo(Settings.MAVEN_MINIMAL_VERSION) < 0) {
LOG.error("Maven version detected {}, is too old. Please use at least version {}", mavenVersion, Settings.MAVEN_MINIMAL_VERSION);
throw new IllegalArgumentException("Maven version is too old.");
}
}

private InvocationRequest createInvocationRequest(String pluginPath, List<String> goals) {
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 47a0969

Please sign in to comment.