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 test sources
39   * 
40   * @author <a href="mailto:gslowikowski@gmail.com">Grzegorz Slowikowski</a>
41   * @since 1.0.0
42   */
43  @Mojo( name = "testCompile", defaultPhase = LifecyclePhase.TEST_COMPILE, requiresDependencyResolution = ResolutionScope.TEST )
44  public class SBTTestCompileMojo
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> testIncludes = 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> testExcludes = new HashSet<String>();
62  
63      /**
64       * Set this to 'true' to bypass compilation of test sources.
65       * Its use is NOT RECOMMENDED, but quite convenient on occasion.
66       * 
67       * @since 1.0.0
68       */
69      @Parameter( property = "maven.test.skip" )
70      private boolean skipTest;
71  
72      /**
73       * The source directories containing the test-source to be compiled.
74       */
75      @Parameter( defaultValue = "${project.testCompileSourceRoots}", readonly = true, required = true )
76      private List<String> compileSourceRoots;
77  
78      /**
79       * Project test classpath.
80       */
81      @Parameter( defaultValue = "${project.testClasspathElements}", required = true, readonly = true )
82      private List<String> classpathElements;
83  
84      /**
85       * The directory where compiled test classes go.
86       */
87      @Parameter( defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = true )
88      private File outputDirectory;
89  
90      @Override
91      protected void internalExecute()
92          throws MojoExecutionException, MojoFailureException, IOException
93      {
94          if ( skipTest )
95          {
96              getLog().info( "Not compiling test sources" );
97              return;
98          }
99          super.internalExecute();
100     }
101 
102     @Override
103     protected List<String> getCompileSourceRoots()
104     {
105         return compileSourceRoots;
106     }
107 
108     @Override
109     protected Set<String> getSourceIncludes()
110     {
111         return testIncludes;
112     }
113     
114     @Override
115     protected Set<String> getSourceExcludes()
116     {
117         return testExcludes;
118     }
119 
120     @Override
121     protected List<String> getClasspathElements()
122     {
123         return classpathElements;
124     }
125 
126     @Override
127     protected File getOutputDirectory()
128     {
129         return outputDirectory;
130     }
131 
132     @Override
133     protected File getAnalysisCacheFile()
134     {
135         return defaultTestAnalysisCacheFile( project );
136     }
137 
138     @Override
139     protected Map<File, File> getAnalysisCacheMap()
140     {
141         HashMap<File, File> map = new HashMap<File, File>();
142         for ( MavenProject reactorProject : reactorProjects )
143         {
144             if ( reactorProject != project )
145             {
146                 File analysisCacheFile = defaultAnalysisCacheFile( reactorProject );
147                 if ( analysisCacheFile.isFile() )
148                 {
149                     File reactorProjectArtifactFile = reactorProject.getArtifact().getFile();
150                     if ( reactorProjectArtifactFile != null )
151                     {
152                         map.put( reactorProjectArtifactFile.getAbsoluteFile(), analysisCacheFile.getAbsoluteFile() );
153                     }
154                 }
155                 
156                 File testAnalysisCacheFile = defaultTestAnalysisCacheFile( reactorProject );
157                 if ( testAnalysisCacheFile.isFile() )
158                 {
159                     List<Artifact> reactorProjectattachedArtifacts = reactorProject.getAttachedArtifacts();
160                     for ( Artifact artifact: reactorProjectattachedArtifacts )
161                     {
162                         if ( "tests".equals( artifact.getClassifier() ) )
163                         {
164                             map.put( artifact.getFile().getAbsoluteFile(), testAnalysisCacheFile.getAbsoluteFile() );
165                             break;
166                         }
167                     }
168                 }
169             }
170         }
171         return map;
172     }
173 
174 }