diff --git a/src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java b/src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java new file mode 100644 index 00000000..5ffd0989 --- /dev/null +++ b/src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java @@ -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 projects) throws MojoExecutionException { + Map> 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"); + } + } +} diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml index 4090b4ee..fea06809 100644 --- a/src/main/resources/META-INF/plexus/components.xml +++ b/src/main/resources/META-INF/plexus/components.xml @@ -10,6 +10,7 @@ com.axonivy.ivy.ci:project-build-plugin:maven-dependency-cleanup + com.axonivy.ivy.ci:project-build-plugin:validate com.axonivy.ivy.ci:project-build-plugin:installEngine com.axonivy.ivy.ci:project-build-plugin:ivy-resources-properties, @@ -38,6 +39,7 @@ + com.axonivy.ivy.ci:project-build-plugin:validate com.axonivy.ivy.ci:project-build-plugin:installEngine com.axonivy.ivy.ci:project-build-plugin:ivy-resources-properties,