View Javadoc
1   package org.apache.maven.plugin.compiler;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Compiles application sources
40   *
41   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
42   * @version $Id: CompilerMojo.html 998881 2016-10-04 20:35:50Z hboutemy $
43   * @since 2.0
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       * The source directories containing the sources to be compiled.
52       */
53      @Parameter( defaultValue = "${project.compileSourceRoots}", readonly = true, required = true )
54      private List<String> compileSourceRoots;
55  
56      /**
57       * The directory for compiled classes.
58       */
59      @Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true )
60      private File outputDirectory;
61  
62      /**
63       * Projects main artifact.
64       *
65       * @todo this is an export variable, really
66       */
67      @Parameter( defaultValue = "${project.artifact}", readonly = true, required = true )
68      private Artifact projectArtifact;
69  
70      /**
71       * A list of inclusion filters for the compiler.
72       */
73      @Parameter
74      private Set<String> includes = new HashSet<String>();
75  
76      /**
77       * A list of exclusion filters for the compiler.
78       */
79      @Parameter
80      private Set<String> excludes = new HashSet<String>();
81  
82      /**
83       * <p>
84       * Specify where to place generated source files created by annotation processing.
85       * Only applies to JDK 1.6+
86       * </p>
87       *
88       * @since 2.2
89       */
90      @Parameter( defaultValue = "${project.build.directory}/generated-sources/annotations" )
91      private File generatedSourcesDirectory;
92  
93      /**
94       * Set this to 'true' to bypass compilation of main sources.
95       * Its use is NOT RECOMMENDED, but quite convenient on occasion.
96       */
97      @Parameter ( property = "maven.main.skip" )
98      private boolean skipMain;
99  
100     @Parameter( defaultValue = "${project.compileClasspathElements}", readonly = true, required = true )
101     private List<String> compilePath;
102     
103     private List<String> classpathElements;
104 
105     private List<String> modulepathElements;
106     
107     protected List<String> getCompileSourceRoots()
108     {
109         return compileSourceRoots;
110     }
111 
112     protected List<String> getClasspathElements()
113     {
114         return classpathElements;
115     }
116 
117     @Override
118     protected List<String> getModulepathElements()
119     {
120         return modulepathElements;
121     }
122 
123     protected File getOutputDirectory()
124     {
125         return outputDirectory;
126     }
127 
128     public void execute()
129         throws MojoExecutionException, CompilationFailureException
130     {
131         if ( skipMain )
132         {
133             getLog().info( "Not compiling main sources" );
134             return;
135         }
136 
137         super.execute();
138 
139         if ( outputDirectory.isDirectory() )
140         {
141             projectArtifact.setFile( outputDirectory );
142         }
143     }
144     
145     @Override
146     protected void preparePaths( Set<File> sourceFiles )
147     {
148         assert compilePath != null;
149         
150         boolean hasModuleDescriptor = false;
151         for ( File sourceFile : sourceFiles )
152         {
153             if ( "module-info.java".equals( sourceFile.getName() ) )
154             {
155                 hasModuleDescriptor = true;
156                 break;
157             }
158         }
159 
160         if ( hasModuleDescriptor )
161         {
162             modulepathElements = compilePath;
163             classpathElements = Collections.emptyList();
164         }
165         else
166         {
167             classpathElements = compilePath;
168             modulepathElements = Collections.emptyList();
169         }
170         
171         getLog().debug( "classpathElements: " + getClasspathElements() );
172     }
173 
174     protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
175     {
176         SourceInclusionScanner scanner;
177 
178         if ( includes.isEmpty() && excludes.isEmpty() )
179         {
180             scanner = new StaleSourceScanner( staleMillis );
181         }
182         else
183         {
184             if ( includes.isEmpty() )
185             {
186                 includes.add( "**/*.java" );
187             }
188             scanner = new StaleSourceScanner( staleMillis, includes, excludes );
189         }
190 
191         return scanner;
192     }
193 
194     protected SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding )
195     {
196         SourceInclusionScanner scanner;
197 
198         // it's not defined if we get the ending with or without the dot '.'
199         String defaultIncludePattern = "**/*" + ( inputFileEnding.startsWith( "." ) ? "" : "." ) + inputFileEnding;
200 
201         if ( includes.isEmpty() && excludes.isEmpty() )
202         {
203             includes = Collections.singleton( defaultIncludePattern );
204             scanner = new SimpleSourceInclusionScanner( includes, Collections.<String>emptySet() );
205         }
206         else
207         {
208             if ( includes.isEmpty() )
209             {
210                 includes.add( defaultIncludePattern );
211             }
212             scanner = new SimpleSourceInclusionScanner( includes, excludes );
213         }
214 
215         return scanner;
216     }
217 
218     protected String getSource()
219     {
220         return source;
221     }
222 
223     protected String getTarget()
224     {
225         return target;
226     }
227     
228     @Override
229     protected String getRelease()
230     {
231         return release;
232     }
233     
234 
235     protected String getCompilerArgument()
236     {
237         return compilerArgument;
238     }
239 
240     protected Map<String, String> getCompilerArguments()
241     {
242         return compilerArguments;
243     }
244 
245     protected File getGeneratedSourcesDirectory()
246     {
247         return generatedSourcesDirectory;
248     }
249 
250 }