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