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.List;
24  import java.util.Map;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.plugins.annotations.ResolutionScope;
33  import org.apache.maven.project.MavenProject;
34  
35  /**
36   * Compile Scala and Java test sources
37   * 
38   * @author <a href="mailto:gslowikowski@gmail.com">Grzegorz Slowikowski</a>
39   * @since 1.0.0
40   */
41  @Mojo( name = "testCompile", defaultPhase = LifecyclePhase.TEST_COMPILE, requiresDependencyResolution = ResolutionScope.TEST )
42  public class SBTTestCompileMojo
43      extends AbstractSBTCompileMojo
44  {
45      /**
46       * Set this to 'true' to bypass compilation of test sources. Its use is NOT RECOMMENDED, but quite convenient on
47       * occasion.
48       */
49      @Parameter( property = "maven.test.skip" )
50      private boolean testCompileSkip;
51  
52      /**
53       * The source directories containing the test-source to be compiled.
54       */
55      @Parameter( defaultValue = "${project.testCompileSourceRoots}", readonly = true, required = true )
56      private List<String> compileSourceRoots;
57  
58      /**
59       * Project test classpath.
60       */
61      @Parameter( defaultValue = "${project.testClasspathElements}", required = true, readonly = true )
62      private List<String> classpathElements;
63  
64      /**
65       * The directory where compiled test classes go.
66       */
67      @Parameter( defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = true )
68      private File outputDirectory;
69  
70      @Override
71      protected void internalExecute()
72          throws MojoExecutionException, MojoFailureException, IOException
73      {
74          if ( testCompileSkip )
75          {
76              getLog().info( "Not compiling test sources" );
77          }
78          else
79          {
80              super.internalExecute();
81          }
82      }
83  
84      @Override
85      protected List<String> getCompileSourceRoots()
86      {
87          return compileSourceRoots;
88      }
89  
90      @Override
91      protected List<String> getClasspathElements()
92      {
93          return classpathElements;
94      }
95  
96      @Override
97      protected File getOutputDirectory()
98      {
99          return outputDirectory;
100     }
101 
102     @Override
103     protected File getAnalysisCacheFile()
104     {
105         return defaultTestAnalysisCacheFile( project );
106     }
107 
108     @Override
109     protected Map<File, File> getAnalysisCacheMap()
110     {
111         HashMap<File, File> map = new HashMap<File, File>();
112         for ( MavenProject reactorProject : reactorProjects )
113         {
114             if ( reactorProject != project )
115             {
116                 File analysisCacheFile = defaultAnalysisCacheFile( reactorProject );
117                 if ( analysisCacheFile.isFile() )
118                 {
119                     File reactorProjectArtifactFile = reactorProject.getArtifact().getFile();
120                     if ( reactorProjectArtifactFile != null )
121                     {
122                         map.put( reactorProjectArtifactFile.getAbsoluteFile(), analysisCacheFile.getAbsoluteFile() );
123                     }
124                 }
125                 
126                 File testAnalysisCacheFile = defaultTestAnalysisCacheFile( reactorProject );
127                 if ( testAnalysisCacheFile.isFile() )
128                 {
129                     List<Artifact> reactorProjectattachedArtifacts = reactorProject.getAttachedArtifacts();
130                     for ( Artifact artifact: reactorProjectattachedArtifacts )
131                     {
132                         if ( "tests".equals( artifact.getClassifier() ) )
133                         {
134                             map.put( artifact.getFile().getAbsoluteFile(), testAnalysisCacheFile.getAbsoluteFile() );
135                             break;
136                         }
137                     }
138                 }
139             }
140         }
141         return map;
142     }
143 
144 }