View Javadoc

1   package org.codehaus.mojo.javascript;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.IOException;
21  
22  import org.apache.maven.artifact.DefaultArtifact;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.mojo.javascript.archive.JavascriptArtifactManager;
28  import org.codehaus.plexus.archiver.ArchiverException;
29  import org.codehaus.plexus.util.DirectoryScanner;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * Goal which copies scripts to the test-script directory.
34   * 
35   * @goal prepare-jsunit-tests
36   * @phase test-compile
37   * @requiresDependencyResolution test
38   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
39   */
40  public class PrepareJsunitTestsMojo
41      extends AbstractMojo
42  {
43  
44      /**
45       * The maven project.
46       * 
47       * @parameter expression="${project}"
48       * @required
49       * @readonly
50       */
51      private MavenProject project;
52  
53      /**
54       * Set this to 'true' to bypass unit tests entirely. Its use is NOT
55       * RECOMMENDED, but quite convenient on occasion.
56       * 
57       * @parameter expression="${maven.test.skip}"
58       */
59      private boolean skipTests;
60  
61      /**
62       * Location of the source files.
63       * 
64       * @parameter default-value="${basedir}/src/main/javascript"
65       */
66      protected File sourceDirectory;
67  
68      /**
69       * Location of the source files.
70       * 
71       * @parameter default-value="${basedir}/src/test/javascript"
72       */
73      protected File jsunitTestSourceDirectory;
74  
75      /**
76       * @parameter default-value="${project.basedir}${file.separator}src${file.separator}test${file.separator}javascript" expression="${testSourceDirectory}"
77       */
78      protected File jasmineTestSourceDirectory;
79  
80      /**
81       * Location of the source files.
82       * 
83       * @parameter default-value="${project.build.directory}/test-scripts"
84       */
85      protected File workDirectory;
86  
87      /**
88       * The folder for javascripts dependencies
89       * 
90       * @parameter expression="${scripts}" default-value="lib"
91       */
92      private String libsDirectory;
93  
94      /**
95       * Use the artifactId as folder
96       * 
97       * @parameter
98       */
99      private boolean useArtifactId;
100 
101     /**
102      * @component
103      */
104     private JavascriptArtifactManager javascriptArtifactManager;
105 
106     /**
107      * {@inheritDoc}
108      * 
109      * @see org.apache.maven.plugin.Mojo#execute()
110      */
111     public void execute()
112         throws MojoExecutionException, MojoFailureException
113     {
114         if(skipTests) {
115             return;
116         }
117 
118         if(jsunitTestSourceDirectory.equals(jasmineTestSourceDirectory) || !jsunitTestSourceDirectory.exists()) {
119             getLog().info("No JsUnit tests, skipping JsUnit tests preparation.");
120             return;
121         }
122 
123         DirectoryScanner scanner = new DirectoryScanner();
124         scanner.addDefaultExcludes();
125         try
126         {
127             scanner.setBasedir( sourceDirectory );
128             scanner.scan();
129             String[] files = scanner.getIncludedFiles();
130             for ( int i = 0; i < files.length; i++ )
131             {
132                 File destFile = new File(workDirectory, files[i] );
133                 destFile.getParentFile().mkdirs();
134                 FileUtils.copyFile( new File( sourceDirectory, files[i] ), destFile );
135             }
136 
137             scanner.setBasedir(jsunitTestSourceDirectory);
138             scanner.scan();
139             files = scanner.getIncludedFiles();
140             for ( int i = 0; i < files.length; i++ )
141             {
142                 File destFile = new File(workDirectory, files[i] );
143                 destFile.getParentFile().mkdirs();
144                 FileUtils.copyFile( new File(jsunitTestSourceDirectory, files[i] ), destFile );
145             }
146         }
147         catch ( IOException e )
148         {
149             throw new MojoExecutionException( "Failed to copy scripts in " + workDirectory);
150         }
151 
152         try
153         {
154             javascriptArtifactManager.unpack( project, DefaultArtifact.SCOPE_TEST, new File(
155                 workDirectory, libsDirectory ), useArtifactId );
156         }
157         catch ( ArchiverException e )
158         {
159             throw new MojoExecutionException( "Failed to unpack javascript dependencies", e );
160         }
161     }
162 }