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 org.apache.maven.model.Resource;
021    import org.apache.maven.plugin.MojoExecutionException;
022    import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
023    import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
024    import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
025    import org.codehaus.plexus.compiler.util.scan.mapping.SingleTargetSourceMapping;
026    
027    import java.io.File;
028    import java.util.HashSet;
029    import java.util.List;
030    import java.util.Set;
031    import java.util.Iterator;
032    
033    /**
034     * @author <a href="mailto:jubu@volny.cz">Juraj Burian</a>
035     * @version $Id: AptMojo.java 949569 2010-05-30 20:44:27Z bommel $
036     * @goal execute
037     * @phase generate-sources
038     * @requiresProject
039     * @requiresDependencyResolution compile
040     * @description generates and/or compiles application sources
041     */
042    public class AptMojo extends AbstractAPTMojo
043    {
044    
045        /**
046         * The source directory containing the generated sources.
047         * 
048         * @parameter default-value="src/main/gen"
049         */
050        private String generated;
051    
052        /**
053         * The source directories containing the sources to be compiled.
054         * 
055         * @parameter expression="${project.compileSourceRoots}"
056         * @required
057         * @readonly
058         */
059        private List compileSourceRoots;
060    
061        /**
062         * The extra source directories containing the sources to be processed.
063         *
064         * @parameter
065         */
066        private List aptSourceRoots;
067    
068        /**
069         * Project classpath.
070         * 
071         * @parameter expression="${project.compileClasspathElements}"
072         * @required
073         * @readonly
074         */
075        private List classpathElements;
076    
077        /**
078         * The directory for compiled classes.
079         * 
080         * @parameter expression="${project.build.outputDirectory}"
081         * @required
082         * @readonly
083         */
084        private File outputDirectory;
085    
086        protected String getGenerated()
087        {
088            return generated;
089        }
090    
091        protected List getCompileSourceRoots()
092        {
093            return compileSourceRoots;
094        }
095    
096        protected List getClasspathElements()
097        {
098            return classpathElements;
099        }
100    
101        protected File getOutputDirectory()
102        {
103            return outputDirectory;
104        }
105    
106        protected List getAptSourceRoots()
107        {
108            return aptSourceRoots;
109        }
110    
111        public void execute() throws MojoExecutionException
112        {
113            super.execute();
114            File absoluteGeneratedPath = new File( getProject().getBasedir(), getGenerated() );
115            getProject().addCompileSourceRoot( absoluteGeneratedPath.getPath() );
116            Resource resource = new Resource();
117            resource.setFiltering( isResourceFiltering() );
118            if ( getResourceTargetPath() != null )
119            {
120                resource.setTargetPath( getResourceTargetPath() );
121            }
122            resource.setDirectory( absoluteGeneratedPath.getPath() );
123            resource.addExclude( "**/*.java" );
124            getProject().addResource( resource );
125        }
126    
127        /**
128         * A list of inclusion filters for the compiler.
129         * 
130         * @parameter
131         */
132        private Set includes = new HashSet();
133    
134        /**
135         * A list of exclusion filters for the compiler.
136         * 
137         * @parameter
138         */
139        private Set excludes = new HashSet();
140    
141        protected SourceInclusionScanner getSourceInclusionScanner()
142        {
143            StaleSourceScanner scanner = null;
144    
145            if ( includes.isEmpty() )
146            {
147                includes.add( "**/*.java" );
148            }
149    
150            if ( isForce() )
151            {
152                return new AllSourcesInclusionScanner( includes, excludes );
153            }
154    
155            scanner = new StaleSourceScanner( getStaleMillis(), includes, excludes );
156            if ( getTargetFiles() != null && getTargetFiles().size() > 0 )
157            {
158                for ( Iterator it = getTargetFiles().iterator() ; it.hasNext() ;)
159                {
160                    String file = (String) it.next();
161                    scanner.addSourceMapping( new SingleTargetSourceMapping( ".java", file ) );
162                }
163            }
164            else
165            {
166                scanner.addSourceMapping( new SuffixMapping( ".java", ".class" ) );
167            }
168            return scanner;
169        }
170    }