View Javadoc

1   /*
2    * Copyright 2013 Grzegorz Slowikowski (gslowikowski at gmail dot com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing,
11   * software distributed under the License is distributed on an
12   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13   * KIND, either express or implied.  See the License for the
14   * specific language governing permissions and limitations
15   * under the License.
16   */
17  
18  package com.google.code.sbt;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.maven.plugins.annotations.LifecyclePhase;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.plugins.annotations.ResolutionScope;
35  import org.apache.maven.project.MavenProject;
36  
37  /**
38   * Compile Scala and Java sources
39   * 
40   * @author <a href="mailto:gslowikowski@gmail.com">Grzegorz Slowikowski</a>
41   * @since 1.0.0
42   */
43  @Mojo( name = "compile", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE )
44  public class SBTCompileMojo
45      extends AbstractSBTCompileMojo
46  {
47      /**
48       * A list of inclusion filters for the compiler.
49       * 
50       * @since 1.0.0
51       */
52      @Parameter
53      private Set<String> includes = new HashSet<String>();
54  
55      /**
56       * A list of exclusion filters for the compiler.
57       * 
58       * @since 1.0.0
59       */
60      @Parameter
61      private Set<String> excludes = new HashSet<String>();
62  
63      /**
64       * Set this to 'true' to bypass compilation of main sources.
65       * Its use is NOT RECOMMENDED, but quite convenient on occasion.
66       */
67      @Parameter ( property = "maven.main.skip" )
68      private boolean skipMain;
69  
70      /**
71       * The source directories containing the sources to be compiled.
72       */
73      @Parameter( defaultValue = "${project.compileSourceRoots}", readonly = true, required = true )
74      private List<String> compileSourceRoots;
75  
76      /**
77       * Project classpath.
78       */
79      @Parameter( defaultValue = "${project.compileClasspathElements}", readonly = true, required = true )
80      private List<String> classpathElements;
81  
82      /**
83       * The directory for compiled classes.
84       */
85      @Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true )
86      private File outputDirectory;
87  
88      /**
89       * Project's main artifact.
90       */
91      @Parameter( defaultValue = "${project.artifact}", readonly = true, required = true )
92      private Artifact projectArtifact;
93  
94      @Override
95      protected void internalExecute()
96          throws MojoExecutionException, MojoFailureException, IOException
97      {
98          if ( skipMain )
99          {
100             getLog().info( "Not compiling main sources" );
101             return;
102         }
103 
104         super.internalExecute();
105 
106         if ( outputDirectory.isDirectory() )
107         {
108             projectArtifact.setFile( outputDirectory );
109         }
110     }
111 
112     @Override
113     protected List<String> getCompileSourceRoots()
114     {
115         return compileSourceRoots;
116     }
117 
118     @Override
119     protected Set<String> getSourceIncludes()
120     {
121         return includes;
122     }
123     
124     @Override
125     protected Set<String> getSourceExcludes()
126     {
127         return excludes;
128     }
129 
130     @Override
131     protected List<String> getClasspathElements()
132     {
133         return classpathElements;
134     }
135 
136     @Override
137     protected File getOutputDirectory()
138     {
139         return outputDirectory;
140     }
141 
142     @Override
143     protected File getAnalysisCacheFile()
144     {
145         return defaultAnalysisCacheFile( project );
146     }
147 
148     @Override
149     protected Map<File, File> getAnalysisCacheMap()
150     {
151         HashMap<File, File> map = new HashMap<File, File>();
152         for ( MavenProject reactorProject : reactorProjects )
153         {
154             if ( reactorProject != project )
155             {
156                 File analysisCacheFile = defaultAnalysisCacheFile( reactorProject );
157                 if ( analysisCacheFile.isFile() )
158                 {
159                     File reactorProjectArtifactFile = reactorProject.getArtifact().getFile();
160                     if ( reactorProjectArtifactFile != null )
161                     {
162                         map.put( reactorProjectArtifactFile.getAbsoluteFile(), analysisCacheFile.getAbsoluteFile() );
163                     }
164                 }
165             }
166         }
167         return map;
168     }
169 
170 }