View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2012-2014 ForgeRock AS
15   */
16  
17  package org.forgerock.doc.maven.release;
18  
19  import org.apache.commons.io.FileUtils;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.forgerock.doc.maven.AbstractDocbkxMojo;
22  import org.twdata.maven.mojoexecutor.MojoExecutor;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.net.URL;
27  
28  /**
29   * Zip release documents if configured to do so.
30   *
31   * <p>
32   *
33   * This zips the release layout only on one level,
34   * and does not handle assembly of multiple zips
35   * into a single documentation set .zip.
36   */
37  public class Zip {
38  
39      /**
40       * The Mojo that holds configuration and related methods.
41       */
42      private AbstractDocbkxMojo m;
43  
44      /**
45       * The Executor to run the assembly plugin.
46       */
47      private final Executor executor;
48  
49      /**
50       * Constructor setting the Mojo that holds the configuration.
51       *
52       * @param mojo The Mojo that holds the configuration.
53       */
54      public Zip(final AbstractDocbkxMojo mojo) {
55          m = mojo;
56          this.executor = new Executor();
57      }
58  
59      /**
60       * Zip release documents.
61       *
62       * @throws MojoExecutionException Failed to zip documents.
63       */
64      public void execute() throws MojoExecutionException {
65          executor.zip();
66      }
67  
68      /**
69       * Enclose methods to run plugins.
70       */
71      class Executor extends MojoExecutor {
72  
73          /**
74           * Zip release documents.
75           *
76           * @throws MojoExecutionException Failed to zip documents.
77           */
78          public void zip() throws MojoExecutionException {
79  
80              if (!m.doBuildReleaseZip()) {
81                  return;
82              }
83  
84              final URL resource = getClass().getResource("/zip.xml");
85              final File assembly = new File(m.getBuildDirectory(), "assembly.xml");
86  
87              try {
88                  FileUtils.copyURLToFile(resource, assembly);
89              } catch (IOException e) {
90                  throw new MojoExecutionException(e.getMessage(), e);
91              }
92  
93  
94              final String finalName = m.getProjectName() + "-" + m.getReleaseVersion();
95  
96              executeMojo(
97                      plugin(
98                              groupId("org.apache.maven.plugins"),
99                              artifactId("maven-assembly-plugin"),
100                             version(m.getMavenAssemblyVersion())),
101                     goal("single"),
102                     configuration(
103                             element(name("finalName"), finalName),
104                             element(name("descriptors"),
105                                     element(name("descriptor"), m.path(assembly)))),
106                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
107         }
108     }
109 }