1   package org.apache.maven.plugin.compiler;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Parameter;
26  import org.apache.maven.plugins.annotations.ResolutionScope;
27  import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
28  import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
29  import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
30  
31  import java.io.File;
32  import java.util.Collections;
33  import java.util.HashSet;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  
38  
39  
40  
41  
42  
43  
44  
45  @org.apache.maven.plugins.annotations.Mojo( name = "compile", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true,
46                                              requiresDependencyResolution = ResolutionScope.COMPILE )
47  public class CompilerMojo
48      extends AbstractCompilerMojo
49  {
50      
51  
52  
53      @Parameter( defaultValue = "${project.compileSourceRoots}", readonly = true, required = true )
54      private List<String> compileSourceRoots;
55  
56      
57  
58  
59      @Parameter( defaultValue = "${project.compileClasspathElements}", readonly = true, required = true )
60      private List<String> classpathElements;
61  
62      
63  
64  
65      @Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true )
66      private File outputDirectory;
67  
68      
69  
70  
71  
72  
73      @Parameter( defaultValue = "${project.artifact}", readonly = true, required = true )
74      private Artifact projectArtifact;
75  
76      
77  
78  
79      @Parameter
80      private Set<String> includes = new HashSet<String>();
81  
82      
83  
84  
85      @Parameter
86      private Set<String> excludes = new HashSet<String>();
87  
88      
89  
90  
91  
92  
93  
94  
95  
96      @Parameter( defaultValue = "${project.build.directory}/generated-sources/annotations" )
97      private File generatedSourcesDirectory;
98  
99      
100 
101 
102 
103     @Parameter ( property = "maven.main.skip" )
104     private boolean skipMain;
105 
106     protected List<String> getCompileSourceRoots()
107     {
108         return compileSourceRoots;
109     }
110 
111     protected List<String> getClasspathElements()
112     {
113         return classpathElements;
114     }
115 
116     protected File getOutputDirectory()
117     {
118         return outputDirectory;
119     }
120 
121     public void execute()
122         throws MojoExecutionException, CompilationFailureException
123     {
124         if ( skipMain )
125         {
126             getLog().info( "Not compiling main sources" );
127             return;
128         }
129         super.execute();
130 
131         if ( outputDirectory.isDirectory() )
132         {
133             projectArtifact.setFile( outputDirectory );
134         }
135     }
136 
137     protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
138     {
139         SourceInclusionScanner scanner = null;
140 
141         if ( includes.isEmpty() && excludes.isEmpty() )
142         {
143             scanner = new StaleSourceScanner( staleMillis );
144         }
145         else
146         {
147             if ( includes.isEmpty() )
148             {
149                 includes.add( "**/*.java" );
150             }
151             scanner = new StaleSourceScanner( staleMillis, includes, excludes );
152         }
153 
154         return scanner;
155     }
156 
157     protected SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding )
158     {
159         SourceInclusionScanner scanner = null;
160 
161         
162         String defaultIncludePattern = "**/*" + ( inputFileEnding.startsWith( "." ) ? "" : "." ) + inputFileEnding;
163 
164         if ( includes.isEmpty() && excludes.isEmpty() )
165         {
166             includes = Collections.singleton( defaultIncludePattern );
167             scanner = new SimpleSourceInclusionScanner( includes, Collections.<String>emptySet() );
168         }
169         else
170         {
171             if ( includes.isEmpty() )
172             {
173                 includes.add( defaultIncludePattern );
174             }
175             scanner = new SimpleSourceInclusionScanner( includes, excludes );
176         }
177 
178         return scanner;
179     }
180 
181     protected String getSource()
182     {
183         return source;
184     }
185 
186     protected String getTarget()
187     {
188         return target;
189     }
190 
191     protected String getCompilerArgument()
192     {
193         return compilerArgument;
194     }
195 
196     protected Map<String, String> getCompilerArguments()
197     {
198         return compilerArguments;
199     }
200 
201     protected File getGeneratedSourcesDirectory()
202     {
203         return generatedSourcesDirectory;
204     }
205 
206 }