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       * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="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       * Used for attaching the artifact in the project.
93       *
94       * @component
95       */
96      private MavenProjectHelper projectHelper;
97  
98      /**
99       * Directory that contains the compiled classes to include in the jar.
100      *
101      * @parameter expression="${project.build.outputDirectory}"
102      * @required
103      */
104     private File contentDirectory;
105 
106     /**
107      * Generates the JAR.
108      *
109      * @todo Add license files in META-INF directory.
110      */
111     public File createArchive()
112         throws MojoExecutionException
113     {
114         File jarFile = new File( targetDirectory, finalName + ".jar" );
115 
116         MavenArchiver archiver = new MavenArchiver();
117 
118         archiver.setArchiver( jarArchiver );
119 
120         archiver.setOutputFile( jarFile );
121 
122         try
123         {
124             if ( contentDirectory.exists() )
125             {
126                 archiver.getArchiver().addDirectory( contentDirectory, DEFAULT_INCLUDES, DEFAULT_EXCLUDES );
127             }
128 
129             addFile( archiver, new File( basedir, "plugin.jelly" ) );
130             addFile( archiver, new File( basedir, "plugin.properties" ) );
131             addFile( archiver, new File( basedir, "project.properties" ) );
132             addFile( archiver, new File( basedir, "build.properties" ) );
133             addFile( archiver, new File( basedir, "project.xml" ) );
134             addDirectory( archiver, new File( basedir, "src/plugin-resources" ) );
135 
136             archiver.createArchive( project, new MavenArchiveConfiguration() );
137 
138             return jarFile;
139         }
140         catch ( Exception e )
141         {
142             // TODO: improve error handling
143             throw new MojoExecutionException( "Error assembling JAR", e );
144         }
145     }
146 
147     private static void addDirectory( MavenArchiver archiver, File file )
148         throws ArchiverException
149     {
150         if ( file.exists() )
151         {
152             archiver.getArchiver().addDirectory( file, file.getName() + "/", DEFAULT_INCLUDES,
153                                                  FileUtils.getDefaultExcludes() );
154         }
155     }
156 
157     private static void addFile( MavenArchiver archiver, File file )
158         throws ArchiverException
159     {
160         if ( file.exists() )
161         {
162             archiver.getArchiver().addFile( file, file.getName() );
163         }
164     }
165 
166     /**
167      * Generates the JAR.
168      *
169      * @todo Add license files in META-INF directory.
170      */
171     public void execute()
172         throws MojoExecutionException
173     {
174         File jarFile = createArchive();
175 
176         project.getArtifact().setFile( jarFile );
177     }
178 }