From 35abc2f57a67c29aaaf0529bfd1394ec318e7c64 Mon Sep 17 00:00:00 2001 From: BJ Hargrave Date: Thu, 5 May 2022 15:48:54 -0400 Subject: [PATCH] maven: Fixes to lifecycle participant execution changing Only alter jar plugin executions for jar packaging. Only alter war executions for war packaging. Don't remove execution if one of the executions goals matches. First we remove the goals and if no goals are left, we remove the execution. Signed-off-by: BJ Hargrave --- .../BndPackagingLifecycleParticipant.java | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/maven/bnd-maven-plugin/src/main/java/aQute/bnd/maven/plugin/BndPackagingLifecycleParticipant.java b/maven/bnd-maven-plugin/src/main/java/aQute/bnd/maven/plugin/BndPackagingLifecycleParticipant.java index a5f18f9deb..36d1eeb153 100644 --- a/maven/bnd-maven-plugin/src/main/java/aQute/bnd/maven/plugin/BndPackagingLifecycleParticipant.java +++ b/maven/bnd-maven-plugin/src/main/java/aQute/bnd/maven/plugin/BndPackagingLifecycleParticipant.java @@ -4,6 +4,7 @@ import static aQute.bnd.maven.lib.executions.PluginExecutions.matchesClassifier; import java.util.List; +import java.util.Objects; import java.util.Optional; import org.apache.maven.AbstractMavenLifecycleParticipant; @@ -52,7 +53,7 @@ public class BndPackagingLifecycleParticipant extends AbstractMavenLifecyclePart public static final String MAVEN_WAR_PLUGIN_ARTIFACT_ID = "maven-war-plugin"; - private Logger logger; + private Logger logger; @Override public void afterProjectsRead(final MavenSession session) throws MavenExecutionException { @@ -86,11 +87,10 @@ public void enableLogging(final Logger logger) { this.logger = logger; } - protected Optional findMatchingMavenPackagingPluginExecution(Plugin mavenPackagingPlugin, - String classifier) { - return mavenPackagingPlugin.getExecutions() - .stream() - .filter(ex -> matchesClassifier(ex, classifier)) + protected Optional findMatchingMavenPackagingPluginExecution( + List mavenPackagingPluginExecutions, String classifier) { + return mavenPackagingPluginExecutions.stream() + .filter(execution -> matchesClassifier(execution, classifier)) .findFirst(); } @@ -119,9 +119,11 @@ protected Plugin getBndMavenPluginFromContainer(final PluginContainer pluginCont * {@code null} if not present. */ protected Plugin getMavenJarPlugin(final Model model) { - final Build build = model.getBuild(); - if (build != null) { - return getMavenJarPluginFromContainer(build); + if (Objects.equals(model.getPackaging(), "jar")) { + final Build build = model.getBuild(); + if (build != null) { + return getMavenJarPluginFromContainer(build); + } } return null; } @@ -139,9 +141,11 @@ protected Plugin getMavenJarPluginFromContainer(final PluginContainer pluginCont * {@code null} if not present. */ protected Plugin getMavenWarPlugin(final Model model) { - final Build build = model.getBuild(); - if (build != null) { - return getMavenWarPluginFromContainer(build); + if (Objects.equals(model.getPackaging(), "war")) { + final Build build = model.getBuild(); + if (build != null) { + return getMavenWarPluginFromContainer(build); + } } return null; } @@ -166,7 +170,6 @@ && nullToEmpty(artifactId).equals(nullToEmpty(plugin.getArtifactId()))) { } result = plugin; } - } return result; } @@ -176,18 +179,27 @@ protected String nullToEmpty(String str) { .orElse(""); } - protected void processExecutions(List bndMavenPluginExecutions, Plugin mavenPackagingPlugin, MavenProject project) { + protected void processExecutions(List bndMavenPluginExecutions, Plugin mavenPackagingPlugin, + MavenProject project) { bndMavenPluginExecutions.stream() .filter(PluginExecutions::isPackagingGoal) - .forEach(bndMavenPluginEx -> { - findMatchingMavenPackagingPluginExecution(mavenPackagingPlugin, extractClassifier(bndMavenPluginEx)) - .ifPresent(packagingPluginEx -> { - mavenPackagingPlugin.getExecutions() - .remove(packagingPluginEx); - - if (logger.isDebugEnabled()) { - logger.debug("Disabled execution of " + packagingPluginEx + " in " + project + " by " - + THIS_ARTIFACT_ID); + .forEach(bndMavenPluginExecution -> { + String classifier = extractClassifier(bndMavenPluginExecution); + findMatchingMavenPackagingPluginExecution(mavenPackagingPlugin.getExecutions(), classifier) + .ifPresent(execution -> { + List goals = execution.getGoals(); + boolean removed = goals.removeIf(goal -> { + if (PluginExecutions.isPackagingGoal(goal)) { + if (logger.isDebugEnabled()) { + logger.debug(THIS_ARTIFACT_ID + " disabled " + mavenPackagingPlugin.getArtifactId() + + ":" + goal + " (" + execution.getId() + ") @ " + project.getArtifactId()); + } + return true; + } + return false; + }); + if (removed && goals.isEmpty()) { + mavenPackagingPlugin.removeExecution(execution); } }); });