View Javadoc

1   package org.apache.maven.plugins.site;
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.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.project.MavenProjectHelper;
28  
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.archiver.jar.JarArchiver;
31  
32  /**
33   * Bundles the site output into a JAR so that it can be deployed to a repository.
34   *
35   * @author <a href="mailto:mbeerman@yahoo.com">Matthew Beermann</a>
36   * @version $Id: SiteJarMojo.html 816568 2012-05-08 12:09:38Z hboutemy $
37   * @goal jar
38   * @phase package
39   * @since 2.0-beta-6
40   */
41  public class SiteJarMojo
42      extends SiteMojo
43  {
44      /**
45       * Specifies the directory where the generated jar file will be put.
46       *
47       * @parameter expression="${project.build.directory}"
48       * @required
49       */
50      private String jarOutputDirectory;
51  
52      /**
53       * Specifies the filename that will be used for the generated jar file.
54       * Please note that "-site" will be appended to the file name.
55       *
56       * @parameter expression="${project.build.finalName}"
57       * @required
58       */
59      private String finalName;
60  
61      /**
62       * Used for attaching the artifact in the project.
63       *
64       * @component
65       */
66      private MavenProjectHelper projectHelper;
67  
68      /**
69       * Specifies whether to attach the generated artifact to the project.
70       *
71       * @parameter expression="${site.attach}" default-value="true"
72       */
73      private boolean attach;
74  
75      /**
76       * @see org.apache.maven.plugin.Mojo#execute()
77       */
78      public void execute()
79          throws MojoExecutionException, MojoFailureException
80      {
81          if ( !outputDirectory.exists() )
82          {
83              super.execute();
84          }
85  
86          try
87          {
88              File outputFile = createArchive( outputDirectory,
89                                               finalName + "-" + getClassifier() + "." + getArtifactType() );
90  
91              if ( attach )
92              {
93                  projectHelper.attachArtifact( project, getArtifactType(), getClassifier(), outputFile );
94              }
95              else
96              {
97                  getLog().info( "NOT adding site jar to the list of attached artifacts." );
98              }
99          }
100         catch ( ArchiverException e )
101         {
102             throw new MojoExecutionException( "Error while creating archive.", e );
103         }
104         catch ( IOException e )
105         {
106             throw new MojoExecutionException( "Error while creating archive.", e );
107         }
108     }
109 
110     protected String getArtifactType()
111     {
112         return "jar";
113     }
114 
115     protected String getClassifier()
116     {
117         return "site";
118     }
119 
120     /**
121      * Method that creates the jar file.
122      *
123      * @param siteDirectory the directory where the site files are located
124      * @param jarFilename   the filename of the created jar file
125      * @return a File object that contains the created jar file
126      * @throws ArchiverException
127      * @throws IOException
128      */
129     private File createArchive( File siteDirectory, String jarFilename )
130         throws ArchiverException, IOException
131     {
132         File siteJar = new File( jarOutputDirectory, jarFilename );
133 
134         if ( siteJar.exists() )
135         {
136             siteJar.delete();
137         }
138 
139         JarArchiver archiver = new JarArchiver();
140         archiver.addDirectory( siteDirectory );
141         archiver.setDestFile( siteJar );
142         archiver.createArchive();
143 
144         return siteJar;
145     }
146 }