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.java 1410034 2012-11-15 21:40:58Z olamy $
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     protected List<String> getCompileSourceRoots()
101     {
102         return compileSourceRoots;
103     }
104 
105     protected List<String> getClasspathElements()
106     {
107         return classpathElements;
108     }
109 
110     protected File getOutputDirectory()
111     {
112         return outputDirectory;
113     }
114 
115     public void execute()
116         throws MojoExecutionException, CompilationFailureException
117     {
118         super.execute();
119 
120         if ( outputDirectory.isDirectory() )
121         {
122             projectArtifact.setFile( outputDirectory );
123         }
124     }
125 
126     protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
127     {
128         SourceInclusionScanner scanner = null;
129 
130         if ( includes.isEmpty() && excludes.isEmpty() )
131         {
132             scanner = new StaleSourceScanner( staleMillis );
133         }
134         else
135         {
136             if ( includes.isEmpty() )
137             {
138                 includes.add( "**/*.java" );
139             }
140             scanner = new StaleSourceScanner( staleMillis, includes, excludes );
141         }
142 
143         return scanner;
144     }
145 
146     protected SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding )
147     {
148         SourceInclusionScanner scanner = null;
149 
150         // it's not defined if we get the ending with or without the dot '.'
151         String defaultIncludePattern = "**/*" + ( inputFileEnding.startsWith( "." ) ? "" : "." ) + inputFileEnding;
152 
153         if ( includes.isEmpty() && excludes.isEmpty() )
154         {
155             includes = Collections.singleton( defaultIncludePattern );
156             scanner = new SimpleSourceInclusionScanner( includes, Collections.<String>emptySet() );
157         }
158         else
159         {
160             if ( includes.isEmpty() )
161             {
162                 includes.add( defaultIncludePattern );
163             }
164             scanner = new SimpleSourceInclusionScanner( includes, excludes );
165         }
166 
167         return scanner;
168     }
169 
170     protected String getSource()
171     {
172         return source;
173     }
174 
175     protected String getTarget()
176     {
177         return target;
178     }
179 
180     protected String getCompilerArgument()
181     {
182         return compilerArgument;
183     }
184 
185     protected Map<String, String> getCompilerArguments()
186     {
187         return compilerArguments;
188     }
189 
190     protected File getGeneratedSourcesDirectory()
191     {
192         return generatedSourcesDirectory;
193     }
194 
195 }