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  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugins.annotations.Component;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.project.MavenProject;
27  
28  /**
29   * Add default Scala source roots, if they exist and are not already added.
30   * 
31   * - adds src/main/scala to Maven project as compile source root
32   * - adds src/test/scala to Maven project as test compile source root
33   * 
34   * @author <a href="mailto:gslowikowski@gmail.com">Grzegorz Slowikowski</a>
35   * @since 1.0.0
36   */
37  @Mojo( name = "addScalaSources", defaultPhase = LifecyclePhase.INITIALIZE )
38  public class SBTAddScalaSourcesMojo
39      extends AbstractMojo
40  {
41  
42      /**
43       * <i>Maven Internal</i>: Project to interact with.
44       */
45      @Component
46      protected MavenProject project;
47  
48      /**
49       * Adds Scala sources.
50       */
51      public void execute()
52      {
53          if ( "pom".equals( project.getPackaging() ) )
54          {
55              return;
56          }
57  
58          File baseDir = project.getBasedir();
59  
60          File mainScalaPath = new File( baseDir, "src/main/scala" );
61          if ( mainScalaPath.isDirectory() )
62          {
63              String mainScalaPathStr = mainScalaPath.getAbsolutePath();
64              if ( !project.getCompileSourceRoots().contains( mainScalaPathStr ) )
65              {
66                  project.addCompileSourceRoot( mainScalaPathStr );
67                  getLog().debug( "Added source directory: " + mainScalaPathStr );
68              }
69          }
70  
71          File testScalaPath = new File( baseDir, "src/test/scala" );
72          if ( testScalaPath.isDirectory() )
73          {
74              String testScalaPathStr = testScalaPath.getAbsolutePath();
75              if ( !project.getCompileSourceRoots().contains( testScalaPathStr ) )
76              {
77                  project.addTestCompileSourceRoot( testScalaPathStr );
78                  getLog().debug( "Added test source directory: " + testScalaPathStr );
79              }
80          }
81      }
82  
83  }