001    package org.apache.myfaces.maven.plugin;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import java.io.File;
021    import java.util.HashSet;
022    import java.util.List;
023    import java.util.Set;
024    import java.util.Iterator;
025    
026    import org.apache.maven.model.Resource;
027    import org.apache.maven.plugin.MojoExecutionException;
028    import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
029    import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
030    import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
031    import org.codehaus.plexus.compiler.util.scan.mapping.SingleTargetSourceMapping;
032    
033    /**
034     * @author <a href="mailto:jubu@volny.cz">Juraj Burian</a>
035     * @version $Id: TestAptMojo.java 949569 2010-05-30 20:44:27Z bommel $
036     * @goal testExecute
037     * @phase generate-sources
038     * @requiresDependencyResolution test
039     * @description Generats and/or compiles test sources
040     */
041    public class TestAptMojo extends AbstractAPTMojo
042    {
043    
044        /**
045         * The source directory containing the generated sources to be compiled.
046         * 
047         * @parameter default-value="src/test/gen"
048         */
049        private String testGenerated;
050    
051        /**
052         * Set this to 'true' to bypass unit tests entirely. Its use is NOT
053         * RECOMMENDED, but quite convenient on occasion.
054         * 
055         * @parameter expression="${maven.test.skip}"
056         */
057        private boolean skip;
058    
059        /**
060         * The source directories containing the test-source to be compiled.
061         * 
062         * @parameter expression="${project.testCompileSourceRoots}"
063         * @required
064         * @readonly
065         */
066        private List compileSourceRoots;
067    
068        /**
069         * The extra source directories containing the test-source to be processed.
070         *
071         * @parameter
072         */
073        private List aptSourceRoots;
074    
075        /**
076         * Project test classpath.
077         * 
078         * @parameter expression="${project.testClasspathElements}"
079         * @required
080         * @readonly
081         */
082        private List testClasspathElements;
083    
084        /**
085         * The directory where compiled test classes go.
086         * 
087         * @parameter expression="${project.build.testOutputDirectory}"
088         * @required
089         * @readonly
090         */
091        private File outputDirectory;
092    
093        /**
094         * A list of inclusion filters for the compiler.
095         * 
096         * @parameter
097         */
098        private Set testIncludes = new HashSet();
099    
100        /**
101         * A list of exclusion filters for the compiler.
102         * 
103         * @parameter
104         */
105        private Set testExcludes = new HashSet();
106    
107        public void execute() throws MojoExecutionException
108        {
109            if ( skip )
110            {
111                //getLog().info( "Not executing test sources" );
112                return;
113            }
114            else
115            {
116                super.execute();
117                File absoluteGeneratedPath = new File( getProject().getBasedir(), getGenerated() );
118                getProject().addTestCompileSourceRoot( absoluteGeneratedPath.getPath() );
119                Resource resource = new Resource();
120                resource.setFiltering( isResourceFiltering() );
121                if ( getResourceTargetPath() != null )
122                {
123                    resource.setTargetPath( getResourceTargetPath() );
124                }
125                resource.setDirectory( absoluteGeneratedPath.getPath() );
126                resource.addExclude( "**/*.java" );
127                getProject().addTestResource( resource );
128            }
129        }
130    
131        protected String getGenerated()
132        {
133            return testGenerated;
134        }
135    
136        protected List getCompileSourceRoots()
137        {
138            return compileSourceRoots;
139        }
140    
141        protected List getClasspathElements()
142        {
143            return testClasspathElements;
144        }
145    
146        protected File getOutputDirectory()
147        {
148            return outputDirectory;
149        }
150    
151        protected List getAptSourceRoots()
152        {
153            return aptSourceRoots;
154        }
155    
156        protected SourceInclusionScanner getSourceInclusionScanner()
157        {
158            StaleSourceScanner scanner = null;
159    
160            if ( testIncludes.isEmpty() )
161            {
162                testIncludes.add( "**/*.java" );
163    
164            }
165            if ( isForce() )
166            {
167                return new AllSourcesInclusionScanner( testIncludes, testExcludes );
168            }
169            scanner = new StaleSourceScanner( getStaleMillis(), testIncludes, testExcludes );
170            if ( getTargetFiles() != null && getTargetFiles().size() > 0 )
171            {
172                for ( Iterator it = getTargetFiles().iterator() ; it.hasNext() ;)
173                {
174                    String file = (String) it.next();
175                    scanner.addSourceMapping( new SingleTargetSourceMapping( ".java", file ) );
176                }
177            }
178            else
179            {
180                scanner.addSourceMapping( new SuffixMapping( ".java", ".class" ) );
181            }
182            return scanner;
183        }
184    }