Skip to content

Commit

Permalink
XIVY-10541 Detect different plugin versions and throw error if so
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-lli committed Dec 18, 2024
1 parent d81d8e2 commit f7905cf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ch.ivyteam.ivy.maven.validate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

@Mojo(name = ValidateMojo.GOAL, requiresProject = true)
public class ValidateMojo extends AbstractMojo {

public static final String GOAL = "validate";
private static final String PLUGIN_GROUPID = "com.axonivy.ivy.ci";
private static final String PLUGIN_ARTIFACTID = "project-build-plugin";

@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConsistentPluginVersion(session.getAllProjects());
}

protected void validateConsistentPluginVersion(List<MavenProject> projects) throws MojoExecutionException {
Map<String, Set<MavenProject>> versionToProjectsMap = new HashMap<>();
for (var project : projects) {
for (var plugin : project.getBuild().getPlugins()) {
if (PLUGIN_GROUPID.equals(plugin.getGroupId()) && PLUGIN_ARTIFACTID.equals(plugin.getArtifactId())) {
var version = plugin.getVersion();
// Skip checking plug ins that do not have a version
if (version == null) {
continue;
}
getLog().debug(PLUGIN_GROUPID + ":" + plugin.getArtifactId() + ":" + version + " configured in " + project);
var projectSet = versionToProjectsMap.get(version);
if (projectSet == null) {
projectSet = new LinkedHashSet<>();
versionToProjectsMap.put(version, projectSet);
}
projectSet.add(project);
}
}
}
if (versionToProjectsMap.size() > 1) {
var versions = new ArrayList<>(versionToProjectsMap.keySet());
Collections.sort(versions);
getLog().error("Several versions of project-build-plugins are configured " + versions + ":");
for (var version : versions) {
getLog().error(version + ":");
for (var project : versionToProjectsMap.get(version)) {
getLog().error("\t" + project.toString());
}
}
throw new MojoExecutionException("All project-build-plugins configured in one reactor must use the same version");
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plexus/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<configuration>
<phases>
<clean>com.axonivy.ivy.ci:project-build-plugin:maven-dependency-cleanup</clean>
<validate>com.axonivy.ivy.ci:project-build-plugin:validate</validate>
<initialize>com.axonivy.ivy.ci:project-build-plugin:installEngine</initialize>
<process-resources>
com.axonivy.ivy.ci:project-build-plugin:ivy-resources-properties,
Expand Down Expand Up @@ -38,6 +39,7 @@
</implementation>
<configuration>
<phases>
<validate>com.axonivy.ivy.ci:project-build-plugin:validate</validate>
<initialize>com.axonivy.ivy.ci:project-build-plugin:installEngine</initialize>
<process-resources>
com.axonivy.ivy.ci:project-build-plugin:ivy-resources-properties,
Expand Down

0 comments on commit f7905cf

Please sign in to comment.