View Javadoc

1   package org.sonatype.plugins.munge;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.List;
6   
7   import org.apache.maven.model.Build;
8   import org.apache.maven.plugin.AbstractMojo;
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.project.MavenProject;
11  import org.codehaus.plexus.util.FileUtils;
12  
13  /**
14   * Munges source files by keeping or removing sections of code according to what symbols are enabled.
15   * 
16   * @see http://blog.publicobject.com/2009/02/preprocessing-java-with-munge.html
17   * @see http://weblogs.java.net/blog/2006/09/05/munge-swings-secret-preprocessor
18   * @goal munge
19   * @phase generate-sources
20   */
21  public class MungeMojo
22      extends AbstractMojo
23  {
24      /**
25       * Where to put the munged source files.
26       * 
27       * @parameter default-value="${project.build.directory}/munged"
28       */
29      private String mungedDirectory;
30  
31      /**
32       * List of symbols (separated by commas) identifying which sections of munged code to keep.
33       * 
34       * @parameter default-value="${symbols}"
35       * @required
36       */
37      private String symbols;
38  
39      /**
40       * List of patterns (separated by commas) specifying files that should be munged; by default munge everything.
41       * 
42       * @parameter default-value="${includes}"
43       */
44      private String includes;
45  
46      /**
47       * List of patterns (separated by commas) specifying files that should not be copied; by default exclude nothing.
48       * 
49       * @parameter default-value="${excludes}"
50       */
51      private String excludes;
52  
53      /**
54       * @parameter expression="${project.build}"
55       * @readonly
56       */
57      private Build build;
58  
59      /**
60       * @parameter expression="${executedProject}"
61       * @readonly
62       */
63      private MavenProject executedProject;
64  
65      @SuppressWarnings( "unchecked" )
66      public void execute()
67          throws MojoExecutionException
68      {
69          Munge.symbols.clear();
70          for ( final String s : symbols.split( "," ) )
71          {
72              Munge.symbols.put( s, Boolean.TRUE );
73          }
74  
75          final String mungedMainDirectory = mungedDirectory + File.separator + "main";
76          final String mungedTestDirectory = mungedDirectory + File.separator + "test";
77  
78          munge( build.getSourceDirectory(), mungedMainDirectory, includes, excludes );
79          munge( build.getTestSourceDirectory(), mungedTestDirectory, includes, excludes );
80  
81          if ( null != executedProject )
82          {
83              executedProject.getCompileSourceRoots().clear();
84              executedProject.addCompileSourceRoot( mungedMainDirectory );
85  
86              executedProject.getTestCompileSourceRoots().clear();
87              executedProject.addTestCompileSourceRoot( mungedTestDirectory );
88  
89              final Build executedBuild = executedProject.getBuild();
90              executedBuild.setDirectory( mungedDirectory );
91  
92              executedBuild.setOutputDirectory( mungedDirectory + File.separator + "classes" );
93              executedBuild.setTestOutputDirectory( mungedDirectory + File.separator + "test-classes" );
94          }
95      }
96  
97      /**
98       * Munges source files found in {@code from} and places them in {@code to}, honoring any includes or excludes.
99       * 
100      * @param from The original source directory
101      * @param to The munged source directory
102      * @param includes Comma-separated list of files to include
103      * @param excludes Comma-separated list of files to exclude
104      * @throws MojoExecutionException
105      */
106     @SuppressWarnings( "unchecked" )
107     public static void munge( final String from, final String to, final String includes, final String excludes )
108         throws MojoExecutionException
109     {
110         try
111         {
112             for ( final File f : (List<File>) FileUtils.getFiles( new File( from ), includes, excludes ) )
113             {
114                 final String inPath = f.getPath();
115                 final String outPath = inPath.replace( from, to );
116                 new File( outPath ).getParentFile().mkdirs();
117                 final Munge munge = new Munge( inPath, outPath );
118                 munge.process();
119                 munge.close();
120             }
121         }
122         catch ( final IOException e )
123         {
124             throw new MojoExecutionException( e.toString() );
125         }
126     }
127 }