44import org .apache .maven .AbstractMavenLifecycleParticipant ;
55import org .apache .maven .MavenExecutionException ;
66import org .apache .maven .execution .MavenSession ;
7+ import org .apache .maven .lifecycle .internal .MojoDescriptorCreator ;
78import org .apache .maven .model .Plugin ;
9+ import org .apache .maven .plugin .prefix .NoPluginFoundForPrefixException ;
810import org .apache .maven .project .MavenProject ;
911import org .codehaus .plexus .component .annotations .Component ;
1012import org .codehaus .plexus .component .annotations .Requirement ;
1315import org .codehaus .plexus .util .xml .Xpp3Dom ;
1416
1517import 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.
2328@ Component (role = AbstractMavenLifecycleParticipant .class , hint = "promote-master" )
2429public 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