Skip to content

Commit

Permalink
maven: Fixes to lifecycle participant execution changing
Browse files Browse the repository at this point in the history
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 <bj@hargrave.dev>
  • Loading branch information
bjhargrave committed May 5, 2022
1 parent 6ccd33d commit 35abc2f
Showing 1 changed file with 35 additions and 23 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -86,11 +87,10 @@ public void enableLogging(final Logger logger) {
this.logger = logger;
}

protected Optional<PluginExecution> findMatchingMavenPackagingPluginExecution(Plugin mavenPackagingPlugin,
String classifier) {
return mavenPackagingPlugin.getExecutions()
.stream()
.filter(ex -> matchesClassifier(ex, classifier))
protected Optional<PluginExecution> findMatchingMavenPackagingPluginExecution(
List<PluginExecution> mavenPackagingPluginExecutions, String classifier) {
return mavenPackagingPluginExecutions.stream()
.filter(execution -> matchesClassifier(execution, classifier))
.findFirst();
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -166,7 +170,6 @@ && nullToEmpty(artifactId).equals(nullToEmpty(plugin.getArtifactId()))) {
}
result = plugin;
}

}
return result;
}
Expand All @@ -176,18 +179,27 @@ protected String nullToEmpty(String str) {
.orElse("");
}

protected void processExecutions(List<PluginExecution> bndMavenPluginExecutions, Plugin mavenPackagingPlugin, MavenProject project) {
protected void processExecutions(List<PluginExecution> 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<String> 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);
}
});
});
Expand Down

0 comments on commit 35abc2f

Please sign in to comment.