Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 3.91 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Import maven plugin configuration by composition rather than inheritance. Can it be done with build extensions?
  2. <build>
  3.     ...
  4.     <plugins>
  5.         <plugin>
  6.             <artifactId>maven-antrun-plugin</artifactId>
  7.             <executions>
  8.                 <execution>
  9.                     <id>printboy</id>
  10.                     <phase>none</phase>
  11.                     <configuration>
  12.                         <target>
  13.                             <echo message="@@@@@@@@@@@@ HELLO! Iam a BOY project"/>
  14.                         </target>
  15.                     </configuration>
  16.                     <goals>
  17.                         <goal>run</goal>
  18.                     </goals>
  19.                 </execution>
  20.                 <execution>
  21.                     <id>printkid</id>
  22.                     <phase>none</phase>
  23.                     <configuration>
  24.                         <target>
  25.                             <echo message="@@@@@@@@@@@@ HELLO! Iam a KID project"/>
  26.                         </target>
  27.                     </configuration>
  28.                     <goals>
  29.                         <goal>run</goal>
  30.                     </goals>
  31.                 </execution>
  32.                 <execution>
  33.                     <id>printgirl</id>
  34.                     <phase>none</phase>
  35.                     <configuration>
  36.                         <target>
  37.                             <echo message="@@@@@@@@@@@@ HELLO! Iam a GIRL project"/>
  38.                         </target>
  39.                     </configuration>
  40.                     <goals>
  41.                         <goal>run</goal>
  42.                     </goals>
  43.                 </execution>
  44.             </executions>
  45.         </plugin>
  46.     </plugins>
  47.     ...
  48. </build>
  49.        
  50. <build>
  51.     <extensions>
  52.         <extension>
  53.             <groupId>br.com.touchtec.maven.plugins</groupId>
  54.             <artifactId>execution-enabler-extension</artifactId>
  55.             <version>1.0.0-SNAPSHOT</version>
  56.         </extension>
  57.     </extensions>
  58.     ...
  59. </build>
  60.        
  61. @Component(role = AbstractMavenLifecycleParticipant.class, hint = "enableif")
  62. public class EnableIfExtension extends AbstractMavenLifecycleParticipant {
  63.  
  64.     @Override
  65.     public void afterProjectsRead(MavenSession session)
  66.             throws MavenExecutionException {
  67.         String phase = "validate";
  68.         for(MavenProject project : session.getProjects()){
  69.             Properties properties = project.getProperties();
  70.             if(properties != null){
  71.                 if("boy".equals(properties.getProperty("project_type"))){
  72.                     setExecutionPhase(project, "printboy", phase);
  73.                     setExecutionPhase(project, "printkid", phase);
  74.                 }
  75.                 if("girl".equals(properties.getProperty("project_type"))){
  76.                     setExecutionPhase(project, "printgirl", phase);
  77.                     setExecutionPhase(project, "printkid", phase);
  78.                 }
  79.                 if("kid".equals(properties.getProperty("project_type"))){
  80.                     setExecutionPhase(project, "printkid", phase);
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     private void setExecutionPhase(MavenProject project, String executionId, String phase) {
  87.         for(Plugin plugin : project.getBuild().getPlugins()){
  88.             if(plugin.getExecutions() != null){
  89.                 for(PluginExecution execution : plugin.getExecutions()){
  90.                     if(executionId.equals(execution.getId())){
  91.                         execution.setPhase(phase);
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
  98.        
  99. <project>
  100.     <properties>
  101.         <project_type>boy</project_type>
  102.     </properties>
  103.  
  104.     <modelVersion>4.0.0</modelVersion>
  105.     <groupId>com.family</groupId>
  106.     <artifactId>boy</artifactId>
  107.     <version>1.0-SNAPSHOT</version>
  108.     <packaging>pom</packaging>
  109.  
  110.     <parent>
  111.         <groupId>com.family</groupId>
  112.         <artifactId>daddy</artifactId>
  113.         <version>1.0-SNAPSHOT</version>
  114.     </parent>
  115.  
  116.     <!-- No "build" section. Beautifull. -->
  117. </project>