From 240c2797eb82925b0840e2c27849e5b400bfc591 Mon Sep 17 00:00:00 2001 From: Lukas Lieb Date: Wed, 18 Dec 2024 13:44:52 +0100 Subject: [PATCH] XIVY-10541 Detect different plugin versions and throw error if so --- .../ivy/maven/validate/ValidateMojo.java | 66 +++++++++++++++++++ .../resources/META-INF/plexus/components.xml | 2 + 2 files changed, 68 insertions(+) create mode 100644 src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java 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..fcca6988 --- /dev/null +++ b/src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java @@ -0,0 +1,66 @@ +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..f1e68a24 100644 --- a/src/main/resources/META-INF/plexus/components.xml +++ b/src/main/resources/META-INF/plexus/components.xml @@ -9,6 +9,7 @@ + com.axonivy.ivy.ci:project-build-plugin:validate com.axonivy.ivy.ci:project-build-plugin:maven-dependency-cleanup com.axonivy.ivy.ci:project-build-plugin:installEngine @@ -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,