Author: jdcasey Date: Fri May 21 22:30:04 2010 New Revision: 947205 URL: http://svn.apache.org/viewvc?rev=947205&view=rev Log: [MCOMPILER-128] Add compile-extras and compile-test-extras mojos
Added: maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileExtrasMojo.java (with props) maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileTestExtrasMojo.java (with props) Added: maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileExtrasMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileExtrasMojo.java?rev=947205&view=auto ============================================================================== --- maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileExtrasMojo.java (added) +++ maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileExtrasMojo.java Fri May 21 22:30:04 2010 @@ -0,0 +1,240 @@ +package org.apache.maven.plugin; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import static org.codehaus.plexus.util.FileUtils.copyDirectory; + +import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner; +import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner; +import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Compiles extra application sources to a separate location, optionally copying the resulting classes + * back into the main compile output directory. This mojo offers the advantage of using different compiler + * arguments or even a separate source tree from that used during the main compilation step. + * + * @author jdcasey + * @version $Id$ + * @since 2.4 + * @goal compile-extras + * @phase compile + * @threadSafe + * @requiresDependencyResolution compile + */ +public class CompileExtrasMojo + extends AbstractCompilerMojo +{ + /** + * The source directories containing the sources to be compiled. + * + * @parameter + * @required + */ + private List<String> sourceRoots; + + /** + * Project classpath. + * + * @parameter default-value="${project.compileClasspathElements}" + * @required + * @readonly + */ + private List<String> classpathElements; + + /** + * The directory for compiled classes. + * + * @parameter + * @required + */ + private File outputDirectory; + + /** + * A list of inclusion filters for the compiler. + * + * @parameter + */ + private Set<String> includes = new HashSet<String>(); + + /** + * A list of exclusion filters for the compiler. + * + * @parameter + */ + private final Set<String> excludes = new HashSet<String>(); + + /** + * <p> + * Specify where to place generated source files created by annotation processing. + * Only applies to JDK 1.6+ + * </p> + * @parameter default-value="${project.build.directory}/generated-sources/annotations" + * @since 2.2 + */ + private File generatedSourcesDirectory; + + /** + * If {...@link #copyOutput} is true, the compiled contents of {...@link #outputDirectory} will be + * copied to this location so it will be available along with the rest of the compiled classes. + * + * @parameter default-value="${project.build.outputDirectory}" + * @required + * @readonly + */ + private File mainOutputDirectory; + + /** + * If true, copy the compiled classes from the {...@link #outputDirectory} to the main compile + * output directory, given by the expression ${project.build.outputDirectory}. + * + * @parameter default-value="true" + */ + private boolean copyOutput; + + private List<String> consolidatedClasspath; + + @Override + protected List<String> getCompileSourceRoots() + { + return sourceRoots; + } + + @Override + protected synchronized List<String> getClasspathElements() + { + if ( consolidatedClasspath == null ) + { + consolidatedClasspath = new ArrayList<String>( classpathElements.size() + 1 ); + consolidatedClasspath.add( outputDirectory.getAbsolutePath() ); + consolidatedClasspath.addAll( classpathElements ); + } + + return consolidatedClasspath; + } + + @Override + protected File getOutputDirectory() + { + return outputDirectory; + } + + @Override + public void execute() + throws MojoExecutionException, CompilationFailureException + { + super.execute(); + + if ( copyOutput ) + { + try + { + copyDirectory( outputDirectory, mainOutputDirectory ); + } + catch ( final IOException e ) + { + throw new MojoExecutionException( "Failed to copy compiled output to main output directory: " + + e.getMessage(), e ); + } + } + } + + @Override + protected SourceInclusionScanner getSourceInclusionScanner( final int staleMillis ) + { + SourceInclusionScanner scanner = null; + + if ( includes.isEmpty() && excludes.isEmpty() ) + { + scanner = new StaleSourceScanner( staleMillis ); + } + else + { + if ( includes.isEmpty() ) + { + includes.add( "**/*.java" ); + } + scanner = new StaleSourceScanner( staleMillis, includes, excludes ); + } + + return scanner; + } + + @Override + protected SourceInclusionScanner getSourceInclusionScanner( final String inputFileEnding ) + { + SourceInclusionScanner scanner = null; + + if ( includes.isEmpty() && excludes.isEmpty() ) + { + includes = Collections.singleton( "**/*." + inputFileEnding ); + scanner = new SimpleSourceInclusionScanner( includes, Collections.EMPTY_SET ); + } + else + { + if ( includes.isEmpty() ) + { + includes.add( "**/*." + inputFileEnding ); + } + scanner = new SimpleSourceInclusionScanner( includes, excludes ); + } + + return scanner; + } + + @Override + protected String getSource() + { + return source; + } + + @Override + protected String getTarget() + { + return target; + } + + @Override + protected String getCompilerArgument() + { + return compilerArgument; + } + + @Override + protected Map<String, String> getCompilerArguments() + { + return compilerArguments; + } + + @Override + protected File getGeneratedSourcesDirectory() + { + return generatedSourcesDirectory; + } + +} \ No newline at end of file Propchange: maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileExtrasMojo.java ------------------------------------------------------------------------------ svn:eol-style = native Added: maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileTestExtrasMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileTestExtrasMojo.java?rev=947205&view=auto ============================================================================== --- maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileTestExtrasMojo.java (added) +++ maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileTestExtrasMojo.java Fri May 21 22:30:04 2010 @@ -0,0 +1,255 @@ +package org.apache.maven.plugin; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import static org.codehaus.plexus.util.FileUtils.copyDirectory; + +import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner; +import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner; +import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Compiles extra application test sources to a separate location, optionally copying the resulting classes + * back into the main test-compile output directory. This mojo offers the advantage of using different compiler + * arguments or even a separate source tree from that used during the main test-compilation step. + * + * @author jdcasey + * @version $Id$ + * @since 2.4 + * @goal compile-test-extras + * @phase test-compile + * @threadSafe + * @requiresDependencyResolution test + */ +public class CompileTestExtrasMojo + extends AbstractCompilerMojo +{ + /** + * Set this to 'true' to bypass unit tests entirely. + * Its use is NOT RECOMMENDED, but quite convenient on occasion. + * + * @parameter expression="${maven.test.skip}" + */ + private boolean skip; + + /** + * The source directories containing the sources to be compiled. + * + * @parameter + * @required + */ + private List<String> sourceRoots; + + /** + * Project classpath. + * + * @parameter default-value="${project.testClasspathElements}" + * @required + * @readonly + */ + private List<String> classpathElements; + + /** + * The directory for compiled classes. + * + * @parameter + * @required + */ + private File outputDirectory; + + /** + * A list of inclusion filters for the compiler. + * + * @parameter + */ + private Set<String> includes = new HashSet<String>(); + + /** + * A list of exclusion filters for the compiler. + * + * @parameter + */ + private final Set<String> excludes = new HashSet<String>(); + + /** + * <p> + * Specify where to place generated source files created by annotation processing. + * Only applies to JDK 1.6+ + * </p> + * @parameter default-value="${project.build.directory}/generated-sources/annotations" + * @since 2.2 + */ + private File generatedSourcesDirectory; + + /** + * If {...@link #copyOutput} is true, the compiled contents of {...@link #outputDirectory} will be + * copied to this location so it will be available along with the rest of the compiled test classes. + * + * @parameter default-value="${project.build.testOutputDirectory}" + * @required + * @readonly + */ + private File testOutputDirectory; + + /** + * If true, copy the compiled classes from the {...@link #outputDirectory} to the normal test-compile + * output directory, given by the expression ${project.build.testOutputDirectory}. + * + * @parameter default-value="true" + */ + private boolean copyOutput; + + private List<String> consolidatedClasspath; + + @Override + protected List<String> getCompileSourceRoots() + { + return sourceRoots; + } + + @Override + protected synchronized List<String> getClasspathElements() + { + if ( consolidatedClasspath == null ) + { + consolidatedClasspath = new ArrayList<String>( classpathElements.size() + 1 ); + consolidatedClasspath.add( outputDirectory.getAbsolutePath() ); + consolidatedClasspath.addAll( classpathElements ); + } + + return consolidatedClasspath; + } + + @Override + protected File getOutputDirectory() + { + return outputDirectory; + } + + @Override + public void execute() + throws MojoExecutionException, CompilationFailureException + { + if ( skip ) + { + getLog().info( "Not compiling test sources" ); + } + else + { + super.execute(); + + if ( copyOutput ) + { + try + { + copyDirectory( outputDirectory, testOutputDirectory ); + } + catch ( final IOException e ) + { + throw new MojoExecutionException( "Failed to copy compiled output to test output directory: " + + e.getMessage(), e ); + } + } + } + } + + @Override + protected SourceInclusionScanner getSourceInclusionScanner( final int staleMillis ) + { + SourceInclusionScanner scanner = null; + + if ( includes.isEmpty() && excludes.isEmpty() ) + { + scanner = new StaleSourceScanner( staleMillis ); + } + else + { + if ( includes.isEmpty() ) + { + includes.add( "**/*.java" ); + } + scanner = new StaleSourceScanner( staleMillis, includes, excludes ); + } + + return scanner; + } + + @Override + protected SourceInclusionScanner getSourceInclusionScanner( final String inputFileEnding ) + { + SourceInclusionScanner scanner = null; + + if ( includes.isEmpty() && excludes.isEmpty() ) + { + includes = Collections.singleton( "**/*." + inputFileEnding ); + scanner = new SimpleSourceInclusionScanner( includes, Collections.EMPTY_SET ); + } + else + { + if ( includes.isEmpty() ) + { + includes.add( "**/*." + inputFileEnding ); + } + scanner = new SimpleSourceInclusionScanner( includes, excludes ); + } + + return scanner; + } + + @Override + protected String getSource() + { + return source; + } + + @Override + protected String getTarget() + { + return target; + } + + @Override + protected String getCompilerArgument() + { + return compilerArgument; + } + + @Override + protected Map<String, String> getCompilerArguments() + { + return compilerArguments; + } + + @Override + protected File getGeneratedSourcesDirectory() + { + return generatedSourcesDirectory; + } + +} \ No newline at end of file Propchange: maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/CompileTestExtrasMojo.java ------------------------------------------------------------------------------ svn:eol-style = native