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 944784 2015-03-23 18:41:16Z khmarbaise $
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       * Project classpath.
58       */
59      @Parameter( defaultValue = "${project.compileClasspathElements}", readonly = true, required = true )
60      private List<String> classpathElements;
61  
62      /**
63       * The directory for compiled classes.
64       */
65      @Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true )
66      private File outputDirectory;
67  
68      /**
69       * Projects main artifact.
70       *
71       * @todo this is an export variable, really
72       */
73      @Parameter( defaultValue = "${project.artifact}", readonly = true, required = true )
74      private Artifact projectArtifact;
75  
76      /**
77       * A list of inclusion filters for the compiler.
78       */
79      @Parameter
80      private Set<String> includes = new HashSet<String>();
81  
82      /**
83       * A list of exclusion filters for the compiler.
84       */
85      @Parameter
86      private Set<String> excludes = new HashSet<String>();
87  
88      /**
89       * <p>
90       * Specify where to place generated source files created by annotation processing.
91       * Only applies to JDK 1.6+
92       * </p>
93       *
94       * @since 2.2
95       */
96      @Parameter( defaultValue = "${project.build.directory}/generated-sources/annotations" )
97      private File generatedSourcesDirectory;
98  
99      /**
100      * Set this to 'true' to bypass compilation of main sources.
101      * Its use is NOT RECOMMENDED, but quite convenient on occasion.
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;
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;
160 
161         // it's not defined if we get the ending with or without the dot '.'
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 }