View Javadoc

1   /*
2    * Copyright 2010 SOFTEC sa. All rights reserved.
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, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package searls.jasmine;
17  
18  import java.io.File;
19  import java.io.IOException;
20  
21  import org.apache.maven.artifact.DefaultArtifact;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.apache.maven.project.MavenProject;
25  import org.codehaus.mojo.javascript.archive.JavascriptArtifactManager;
26  import org.codehaus.plexus.archiver.ArchiverException;
27  import org.codehaus.plexus.util.DirectoryScanner;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  /**
31   * Goal which copies scripts to the jasmine target directory.
32   *
33   * @goal prepare-jasmine-tests
34   * @phase test-compile
35   * @requiresDependencyResolution test
36   */
37  public class PrepareJasmineTestsMojo extends AbstractJasmineMojo
38  {
39      /**
40       * The maven project.
41       *
42       * @parameter expression="${project}"
43       * @required
44       * @readonly
45       */
46      private MavenProject project;
47  
48      /**
49       * @component
50       */
51      private JavascriptArtifactManager javascriptArtifactManager;
52      
53      /**
54       * {@inheritDoc}
55       *
56       * @see org.apache.maven.plugin.Mojo#execute()
57       */
58      public void execute()
59          throws MojoExecutionException, MojoFailureException
60      {
61          if(skipTests || !jasmineTestSourceDirectory.exists()) {
62              getLog().info("No Jasmine tests, skipping Jasmine tests preparation.");
63              return;
64          }
65  
66          DirectoryScanner scanner = new DirectoryScanner();
67          scanner.addDefaultExcludes();
68          try
69          {
70              File destDir;
71              String[] files;
72  
73              if( sourceDirectory.exists() ) {
74                  destDir = new File(jasmineTargetDir, srcDirectoryName);
75                  scanner.setBasedir( sourceDirectory );
76                  scanner.scan();
77                  files = scanner.getIncludedFiles();
78                  for ( int i = 0; i < files.length; i++ )
79                  {
80                      File destFile = new File( destDir, files[i] );
81                      destFile.getParentFile().mkdirs();
82                      FileUtils.copyFile( new File( sourceDirectory, files[i] ), destFile );
83                  }
84              }
85  
86              destDir = new File(jasmineTargetDir, specDirectoryName);
87              scanner.setBasedir( jasmineTestSourceDirectory );
88              scanner.scan();
89              files = scanner.getIncludedFiles();
90              for ( int i = 0; i < files.length; i++ )
91              {
92                  File destFile = new File( destDir, files[i] );
93                  destFile.getParentFile().mkdirs();
94                  FileUtils.copyFile( new File( jasmineTestSourceDirectory, files[i] ), destFile );
95              }
96          }
97          catch ( IOException e )
98          {
99              throw new MojoExecutionException( "Failed to copy scripts in " + jasmineTargetDir );
100         }
101 
102         try
103         {
104             javascriptArtifactManager.unpack( project, DefaultArtifact.SCOPE_TEST, new File(
105                 jasmineTargetDir, libsDirectory ), useArtifactId );
106         }
107         catch ( ArchiverException e )
108         {
109             throw new MojoExecutionException( "Failed to unpack javascript dependencies", e );
110         }
111     }
112 }