View Javadoc
1   package org.apache.maven.plugins.site.render;
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 java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.archiver.MavenArchiveConfiguration;
26  import org.apache.maven.archiver.MavenArchiver;
27  import org.apache.maven.artifact.DependencyResolutionRequiredException;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.plugins.annotations.Component;
31  import org.apache.maven.plugins.annotations.LifecyclePhase;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.plugins.annotations.ResolutionScope;
35  import org.apache.maven.project.MavenProjectHelper;
36  import org.codehaus.plexus.archiver.Archiver;
37  import org.codehaus.plexus.archiver.ArchiverException;
38  import org.codehaus.plexus.archiver.jar.JarArchiver;
39  import org.codehaus.plexus.archiver.jar.ManifestException;
40  
41  /**
42   * Bundles the site output into a JAR so that it can be deployed to a repository.
43   *
44   * @author <a href="mailto:mbeerman@yahoo.com">Matthew Beermann</a>
45   * @version $Id$
46   * @since 2.0-beta-6
47   */
48  // MSITE-665: requiresDependencyResolution workaround for MPLUGIN-253 
49  @Mojo( name = "jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.TEST,
50         requiresReports = true )
51  public class SiteJarMojo
52      extends SiteMojo
53  {
54      private static final String[] DEFAULT_ARCHIVE_EXCLUDES = new String[]{ };
55  
56      private static final String[] DEFAULT_ARCHIVE_INCLUDES = new String[]{ "**/**" };
57  
58      /**
59       * Specifies the directory where the generated jar file will be put.
60       */
61      @Parameter( property = "project.build.directory", required = true )
62      private String jarOutputDirectory;
63  
64      /**
65       * Specifies the filename that will be used for the generated jar file.
66       * Please note that "-site" will be appended to the file name.
67       */
68      @Parameter( property = "project.build.finalName", required = true )
69      private String finalName;
70  
71      /**
72       * Used for attaching the artifact in the project.
73       */
74      @Component
75      private MavenProjectHelper projectHelper;
76  
77      /**
78       * Specifies whether to attach the generated artifact to the project.
79       */
80      @Parameter( property = "site.attach", defaultValue = "true" )
81      private boolean attach;
82  
83      /**
84       * The Jar archiver.
85       *
86       * @since 3.1
87       */
88      @Component( role = Archiver.class, hint = "jar" )
89      private JarArchiver jarArchiver;
90  
91      /**
92       * The archive configuration to use.
93       * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
94       *
95       * @since 3.1
96       */
97      @Parameter
98      private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
99  
100     /**
101      * List of files to include. Specified as file set patterns which are relative to the input directory whose contents
102      * is being packaged into the JAR.
103      *
104      * @since 3.1
105      */
106     @Parameter
107     private String[] archiveIncludes;
108 
109     /**
110      * List of files to exclude. Specified as file set patterns which are relative to the input directory whose contents
111      * is being packaged into the JAR.
112      *
113      * @since 3.1
114      */
115     @Parameter
116     private String[] archiveExcludes;
117 
118     /**
119      * @see org.apache.maven.plugin.Mojo#execute()
120      */
121     public void execute()
122         throws MojoExecutionException, MojoFailureException
123     {
124         if ( skip )
125         {
126             getLog().info( "maven.site.skip = true: Skipping jar generation" );
127             return;
128         }
129 
130         if ( !outputDirectory.exists() )
131         {
132             super.execute();
133         }
134 
135         try
136         {
137             File outputFile = createArchive( outputDirectory,
138                                              finalName + "-" + getClassifier() + "." + getArtifactType() );
139 
140             if ( attach )
141             {
142                 projectHelper.attachArtifact( project, getArtifactType(), getClassifier(), outputFile );
143             }
144             else
145             {
146                 getLog().info( "NOT adding site jar to the list of attached artifacts." );
147             }
148         }
149         catch ( ArchiverException e )
150         {
151             throw new MojoExecutionException( "Error while creating archive.", e );
152         }
153         catch ( IOException e )
154         {
155             throw new MojoExecutionException( "Error while creating archive.", e );
156         }
157         catch ( ManifestException e )
158         {
159             throw new MojoExecutionException( "Error while creating archive.", e );
160         }
161         catch ( DependencyResolutionRequiredException e )
162         {
163             throw new MojoExecutionException( "Error while creating archive.", e );
164         }
165     }
166 
167     protected String getArtifactType()
168     {
169         return "jar";
170     }
171 
172     protected String getClassifier()
173     {
174         return "site";
175     }
176 
177     /**
178      * Method that creates the jar file.
179      *
180      * @param siteDirectory the directory where the site files are located
181      * @param jarFilename   the filename of the created jar file
182      * @return a File object that contains the created jar file
183      * @throws ArchiverException
184      * @throws IOException
185      * @throws ManifestException
186      * @throws DependencyResolutionRequiredException
187      */
188     private File createArchive( File siteDirectory, String jarFilename )
189         throws ArchiverException, IOException, ManifestException, DependencyResolutionRequiredException
190     {
191         File siteJar = new File( jarOutputDirectory, jarFilename );
192 
193         MavenArchiver archiver = new MavenArchiver();
194 
195         archiver.setArchiver( this.jarArchiver );
196 
197         archiver.setOutputFile( siteJar );
198 
199         if ( !siteDirectory.isDirectory() )
200         {
201             getLog().warn( "JAR will be empty - no content was marked for inclusion !" );
202         }
203         else
204         {
205             archiver.getArchiver().addDirectory( siteDirectory, getArchiveIncludes(), getArchiveExcludes() );
206         }
207 
208         archiver.createArchive( getSession(), getProject(), archive );
209 
210         return siteJar;
211     }
212 
213     private String[] getArchiveIncludes()
214     {
215         if ( this.archiveIncludes != null && this.archiveIncludes.length > 0 )
216         {
217             return this.archiveIncludes;
218         }
219 
220         return DEFAULT_ARCHIVE_INCLUDES;
221     }
222 
223     private String[] getArchiveExcludes()
224     {
225         if ( this.archiveExcludes != null && this.archiveExcludes.length > 0 )
226         {
227             return this.archiveExcludes;
228         }
229         return DEFAULT_ARCHIVE_EXCLUDES;
230     }
231 }