Skip to content

Commit ccb758b

Browse files
authored
Merge pull request #41 from egineering-llc/bugfix/40
Retain plugins for project goals.
2 parents 895aa5f + f11cb94 commit ccb758b

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/main/java/com/e_gineering/maven/gitflowhelper/MasterPromoteExtension.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.apache.maven.AbstractMavenLifecycleParticipant;
55
import org.apache.maven.MavenExecutionException;
66
import org.apache.maven.execution.MavenSession;
7+
import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
78
import org.apache.maven.model.Plugin;
9+
import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
810
import org.apache.maven.project.MavenProject;
911
import org.codehaus.plexus.component.annotations.Component;
1012
import org.codehaus.plexus.component.annotations.Requirement;
@@ -13,7 +15,10 @@
1315
import org.codehaus.plexus.util.xml.Xpp3Dom;
1416

1517
import java.io.IOException;
16-
import java.util.*;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Properties;
1722

1823
/**
1924
* Maven extension which removes (skips) undesired plugins from the build reactor when running on a master branch.
@@ -23,6 +28,9 @@
2328
@Component(role = AbstractMavenLifecycleParticipant.class, hint = "promote-master")
2429
public class MasterPromoteExtension extends AbstractMavenLifecycleParticipant {
2530

31+
@Requirement
32+
private MojoDescriptorCreator descriptorCreator;
33+
2634
@Requirement
2735
private Logger logger;
2836

@@ -41,6 +49,22 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
4149
String gitBranchExpression = null;
4250
boolean pluginFound = false;
4351

52+
// Any plugin which is part of the project goals needs to be retained.
53+
List<Plugin> pluginsToRetain = new ArrayList<Plugin>(session.getGoals().size());
54+
55+
List<String> goals = session.getGoals();
56+
for (String goal : goals) {
57+
int delimiter = goal.indexOf(":");
58+
if (delimiter != -1) {
59+
String prefix = goal.substring(0, delimiter);
60+
try {
61+
pluginsToRetain.add(descriptorCreator.findPluginForPrefix(prefix, session));
62+
} catch (NoPluginFoundForPrefixException ex) {
63+
throw new MavenExecutionException("Unable to resolve plugin for prefix: " + prefix, ex);
64+
}
65+
}
66+
}
67+
4468
// Build up a map of plugins to remove from projects, if we're on the master branch.
4569
HashMap<MavenProject, List<Plugin>> pluginsToDrop = new HashMap<MavenProject, List<Plugin>>();
4670

@@ -61,11 +85,14 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
6185
if (gitBranchExpression == null) {
6286
gitBranchExpression = extractPluginConfigValue("gitBranchExpression", plugin);
6387
}
64-
65-
// Don't drop the maven-deploy-plugin
88+
// Don't drop things we declare goals for.
89+
} else if (pluginsToRetain.contains(plugin)) {
90+
logger.debug("gitflow-helper-maven-plugin retaining plugin: " + plugin + " from project: " + project.getName());
91+
// Don't drop the maven-deploy-plugin
6692
} else if (plugin.getKey().equals("org.apache.maven.plugins:maven-deploy-plugin")) {
67-
logger.debug("gitflow-helper-maven-plugin removing plugin: " + plugin + " from project: " + project.getName());
93+
logger.debug("gitflow-helper-maven-plugin retaining plugin: " + plugin + " from project: " + project.getName());
6894
} else {
95+
logger.debug("gitflow-helper-maven-plugin removing plugin: " + plugin + " from project: " + project.getName());
6996
dropPlugins.add(plugin);
7097
}
7198
}

0 commit comments

Comments
 (0)