View Javadoc

1   /*
2    * Derivative Work
3    * Copyright 2010 SOFTEC sa. All rights reserved.
4    *
5    * Original Work
6    * Copyright 2001-2005 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.codehaus.mojo.javascript;
22  
23  import java.io.File;
24  
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.project.MavenProjectHelper;
29  import org.codehaus.mojo.javascript.archive.JavascriptArchiver;
30  import org.codehaus.mojo.javascript.archive.Types;
31  
32  /**
33   * Abstract Goal which packages scripts and resources as a javascript archive to be
34   * installed / deployed in maven repositories.
35   */
36  public abstract class AbstractPackageMojo
37      extends AbstractMojo
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      MavenProjectHelper projectHelper;
52  
53      /**
54       * The filename of the js file.
55       *
56       * @parameter default-value="${project.build.finalName}"
57       */
58      private String finalName;
59  
60      /**
61       * Plexus archiver.
62       *
63       * @component role="org.codehaus.plexus.archiver.Archiver" role-hint="javascript"
64       * @required
65       */
66      private JavascriptArchiver archiver;
67  
68      /**
69       * @parameter
70       */
71      private File manifest;
72  
73      abstract File getOutputDirectory();
74  
75      abstract String getClassifier();
76  
77      abstract File getScriptsDirectory();
78  
79      public void execute()
80          throws MojoExecutionException
81      {
82          File jsarchive = (getClassifier() != null)
83              ? new File( getOutputDirectory(), finalName + "-" + getClassifier() + "." + Types.JAVASCRIPT_EXTENSION )
84              : new File( getOutputDirectory(), finalName + "." + Types.JAVASCRIPT_EXTENSION );
85  
86          try
87          {
88              if ( manifest != null )
89              {
90                  archiver.setManifest( manifest );
91              }
92              else
93              {
94                  archiver.createDefaultManifest( project );
95              }
96              if (getScriptsDirectory() != null && !getScriptsDirectory().exists()) {
97                  getScriptsDirectory().mkdirs();
98              }
99              archiver.addDirectory( getScriptsDirectory() );
100             String groupId = project.getGroupId();
101             String artifactId = project.getArtifactId();
102             archiver.addFile( project.getFile(), "META-INF/maven/" + groupId + "/" + artifactId
103                 + "/pom.xml" );
104             archiver.setDestFile( jsarchive );
105             archiver.createArchive();
106         }
107         catch ( Exception e )
108         {
109             throw new MojoExecutionException( "Failed to create the javascript archive", e );
110         }
111 
112         if ( getClassifier() != null )
113         {
114             projectHelper.attachArtifact( project, Types.JAVASCRIPT_TYPE, getClassifier(), jsarchive );
115         }
116         else
117         {
118             project.getArtifact().setFile( jsarchive );
119         }
120     }
121 }