View Javadoc

1   package org.apache.maven.plugin;
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.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
24  import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
25  import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
26  
27  import java.io.File;
28  import java.util.Collections;
29  import java.util.HashSet;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  
34  /**
35   * Compiles application sources
36   *
37   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
38   * @version $Id: CompilerMojo.html 816615 2012-05-08 13:06:49Z hboutemy $
39   * @since 2.0
40   * @goal compile
41   * @phase compile
42   * @threadSafe
43   * @requiresDependencyResolution compile
44   */
45  public class CompilerMojo
46      extends AbstractCompilerMojo
47  {
48      /**
49       * The source directories containing the sources to be compiled.
50       *
51       * @parameter default-value="${project.compileSourceRoots}"
52       * @required
53       * @readonly
54       */
55      private List<String> compileSourceRoots;
56  
57      /**
58       * Project classpath.
59       *
60       * @parameter default-value="${project.compileClasspathElements}"
61       * @required
62       * @readonly
63       */
64      private List<String> classpathElements;
65  
66      /**
67       * The directory for compiled classes.
68       *
69       * @parameter default-value="${project.build.outputDirectory}"
70       * @required
71       * @readonly
72       */
73      private File outputDirectory;
74  
75      /**
76       * Project artifacts.
77       *
78       * @parameter default-value="${project.artifact}"
79       * @required
80       * @readonly
81       * @todo this is an export variable, really
82       */
83      private Artifact projectArtifact;
84  
85      /**
86       * A list of inclusion filters for the compiler.
87       *
88       * @parameter
89       */
90      private Set<String> includes = new HashSet<String>();
91  
92      /**
93       * A list of exclusion filters for the compiler.
94       *
95       * @parameter
96       */
97      private Set<String> excludes = new HashSet<String>();
98  
99      /**
100      * <p>
101      * Specify where to place generated source files created by annotation processing.
102      * Only applies to JDK 1.6+
103      * </p>
104      * @parameter default-value="${project.build.directory}/generated-sources/annotations"
105      * @since 2.2
106      */
107     private File generatedSourcesDirectory;
108 
109 
110     protected List<String> getCompileSourceRoots()
111     {
112         return compileSourceRoots;
113     }
114 
115     protected List<String> getClasspathElements()
116     {
117         return classpathElements;
118     }
119 
120     protected File getOutputDirectory()
121     {
122         return outputDirectory;
123     }
124 
125     public void execute()
126         throws MojoExecutionException, CompilationFailureException
127     {
128         super.execute();
129 
130         projectArtifact.setFile( outputDirectory );
131     }
132 
133     protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
134     {
135         SourceInclusionScanner scanner = null;
136 
137         if ( includes.isEmpty() && excludes.isEmpty() )
138         {
139             scanner = new StaleSourceScanner( staleMillis );
140         }
141         else
142         {
143             if ( includes.isEmpty() )
144             {
145                 includes.add( "**/*.java" );
146             }
147             scanner = new StaleSourceScanner( staleMillis, includes, excludes );
148         }
149 
150         return scanner;
151     }
152 
153     protected SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding )
154     {
155         SourceInclusionScanner scanner = null;
156 
157         if ( includes.isEmpty() && excludes.isEmpty() )
158         {
159             includes = Collections.singleton( "**/*." + inputFileEnding );
160             scanner = new SimpleSourceInclusionScanner( includes, Collections.EMPTY_SET );
161         }
162         else
163         {
164             if ( includes.isEmpty() )
165             {
166                 includes.add( "**/*." + inputFileEnding );
167             }
168             scanner = new SimpleSourceInclusionScanner( includes, excludes );
169         }
170 
171         return scanner;
172     }
173 
174     protected String getSource()
175     {
176       return source;
177     }
178 
179     protected String getTarget()
180     {
181       return target;
182     }
183 
184     protected String getCompilerArgument()
185     {
186       return compilerArgument;
187     }
188 
189     protected Map<String, String> getCompilerArguments()
190     {
191       return compilerArguments;
192     }
193 
194     protected File getGeneratedSourcesDirectory()
195     {
196         return generatedSourcesDirectory;
197     }
198 
199 }