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 816619 2012-05-08 13:09:26Z 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         if ( outputDirectory.isDirectory() )
131         {
132             projectArtifact.setFile( outputDirectory );
133         }
134     }
135 
136     protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
137     {
138         SourceInclusionScanner scanner = null;
139 
140         if ( includes.isEmpty() && excludes.isEmpty() )
141         {
142             scanner = new StaleSourceScanner( staleMillis );
143         }
144         else
145         {
146             if ( includes.isEmpty() )
147             {
148                 includes.add( "**/*.java" );
149             }
150             scanner = new StaleSourceScanner( staleMillis, includes, excludes );
151         }
152 
153         return scanner;
154     }
155 
156     protected SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding )
157     {
158         SourceInclusionScanner scanner = null;
159 
160         if ( includes.isEmpty() && excludes.isEmpty() )
161         {
162             includes = Collections.singleton( "**/*." + inputFileEnding );
163             scanner = new SimpleSourceInclusionScanner( includes, Collections.EMPTY_SET );
164         }
165         else
166         {
167             if ( includes.isEmpty() )
168             {
169                 includes.add( "**/*." + inputFileEnding );
170             }
171             scanner = new SimpleSourceInclusionScanner( includes, excludes );
172         }
173 
174         return scanner;
175     }
176 
177     protected String getSource()
178     {
179       return source;
180     }
181 
182     protected String getTarget()
183     {
184       return target;
185     }
186 
187     protected String getCompilerArgument()
188     {
189       return compilerArgument;
190     }
191 
192     protected Map<String, String> getCompilerArguments()
193     {
194       return compilerArguments;
195     }
196 
197     protected File getGeneratedSourcesDirectory()
198     {
199         return generatedSourcesDirectory;
200     }
201 
202 }