View Javadoc

1   package org.apache.maven.plugins.mavenone;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  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,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.archiver.MavenArchiveConfiguration;
23  import org.apache.maven.archiver.MavenArchiver;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.project.MavenProjectHelper;
28  import org.codehaus.plexus.archiver.ArchiverException;
29  import org.codehaus.plexus.archiver.jar.JarArchiver;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  import java.io.File;
33  
34  /**
35   * Package a Maven 1 plugin.
36   *
37   * @goal maven-one-plugin
38   * @phase package
39   */
40  public class MavenOnePluginMojo
41      extends AbstractMojo
42  {
43  
44      private static final String[] DEFAULT_EXCLUDES = new String[]{"**/package.html"};
45  
46      private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"};
47  
48      /**
49       * Base directory.
50       *
51       * @parameter expression="${basedir}"
52       * @required
53       * @readonly
54       */
55      private File basedir;
56  
57      /**
58       * Directory containing the generated JAR.
59       *
60       * @parameter expression="${project.build.directory}"
61       * @required
62       * @readonly
63       */
64      private File targetDirectory;
65  
66      /**
67       * Name of the generated JAR.
68       *
69       * @parameter expression="${project.build.finalName}"
70       * @required
71       */
72      private String finalName;
73  
74      /**
75       * The Jar archiver.
76       *
77       * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
78       * @required
79       */
80      private JarArchiver jarArchiver;
81  
82      /**
83       * The maven project.
84       *
85       * @parameter expression="${project}"
86       * @required
87       * @readonly
88       */
89      private MavenProject project;
90  
91      /**
92       * @component
93       * @todo Write Javadoc for this
94       */
95      private MavenProjectHelper projectHelper;
96  
97      /**
98       * Directory that contains the compiled classes to include in the jar.
99       *
100      * @parameter expression="${project.build.outputDirectory}"
101      * @required
102      */
103     private File contentDirectory;
104 
105     /**
106      * Generates the JAR.
107      *
108      * @todo Add license files in META-INF directory.
109      */
110     public File createArchive()
111         throws MojoExecutionException
112     {
113         File jarFile = new File( targetDirectory, finalName + ".jar" );
114 
115         MavenArchiver archiver = new MavenArchiver();
116 
117         archiver.setArchiver( jarArchiver );
118 
119         archiver.setOutputFile( jarFile );
120 
121         try
122         {
123             if ( contentDirectory.exists() )
124             {
125                 archiver.getArchiver().addDirectory( contentDirectory, DEFAULT_INCLUDES, DEFAULT_EXCLUDES );
126             }
127 
128             addFile( archiver, new File( basedir, "plugin.jelly" ) );
129             addFile( archiver, new File( basedir, "plugin.properties" ) );
130             addFile( archiver, new File( basedir, "project.properties" ) );
131             addFile( archiver, new File( basedir, "build.properties" ) );
132             addFile( archiver, new File( basedir, "project.xml" ) );
133             addDirectory( archiver, new File( basedir, "src/plugin-resources" ) );
134 
135             archiver.createArchive( project, new MavenArchiveConfiguration() );
136 
137             return jarFile;
138         }
139         catch ( Exception e )
140         {
141             // TODO: improve error handling
142             throw new MojoExecutionException( "Error assembling JAR", e );
143         }
144     }
145 
146     private static void addDirectory( MavenArchiver archiver, File file )
147         throws ArchiverException
148     {
149         if ( file.exists() )
150         {
151             archiver.getArchiver().addDirectory( file, file.getName() + "/", DEFAULT_INCLUDES,
152                                                  FileUtils.getDefaultExcludes() );
153         }
154     }
155 
156     private static void addFile( MavenArchiver archiver, File file )
157         throws ArchiverException
158     {
159         if ( file.exists() )
160         {
161             archiver.getArchiver().addFile( file, file.getName() );
162         }
163     }
164 
165     /**
166      * Generates the JAR.
167      *
168      * @todo Add license files in META-INF directory.
169      */
170     public void execute()
171         throws MojoExecutionException
172     {
173         File jarFile = createArchive();
174 
175         project.getArtifact().setFile( jarFile );
176     }
177 }