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