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 static org.twdata.maven.mojoexecutor.MojoExecutor.element;
20  import static org.twdata.maven.mojoexecutor.MojoExecutor.name;
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.forgerock.doc.maven.AbstractDocbkxMojo;
25  import org.forgerock.doc.maven.AbstractDocbkxMojo.Format;
26  import org.twdata.maven.mojoexecutor.MojoExecutor;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Lay out built documents,
35   * by default under {@code ${project.build.directory}/release/version}.
36   *
37   * <p>
38   *
39   * Currently only HTML and PDF are released.
40   */
41  public class Layout {
42  
43      /**
44       * The Mojo that holds configuration and related methods.
45       */
46      private AbstractDocbkxMojo m;
47  
48      /**
49       * The Executor to run the resources plugin.
50       */
51      private final Executor executor;
52  
53      /**
54       * Constructor setting the Mojo that holds the configuration.
55       *
56       * @param mojo The Mojo that holds the configuration.
57       */
58      public Layout(final AbstractDocbkxMojo mojo) {
59          m = mojo;
60          this.executor = new Executor();
61      }
62  
63      /**
64       * Lay out built documents.
65       *
66       * @throws MojoExecutionException Failed to layout site.
67       */
68      public void execute() throws MojoExecutionException {
69          executor.layout();
70      }
71  
72      /**
73       * Get element specifying built documents to copy to the release directory.
74       *
75       * <p>
76       *
77       * Currently only HTML and PDF are released.
78       *
79       * @return Compound element specifying built documents to copy.
80       * @throws MojoExecutionException Something went wrong getting document names.
81       */
82      private MojoExecutor.Element getResources() throws MojoExecutionException {
83  
84          ArrayList<MojoExecutor.Element> r = new ArrayList<MojoExecutor.Element>();
85          final List<Format> formats = m.getFormats();
86          final String outputDir = m.path(m.getDocbkxOutputDirectory());
87  
88          if (formats.contains(Format.html)) {
89              r.add(element(name("resource"),
90                      element(name("directory"), outputDir + "/html/")));
91          }
92  
93          if (formats.contains(Format.pdf)) {
94              r.add(element(name("resource"),
95                      element(name("directory"), outputDir + "/pdf/"),
96                      element(name("includes"),
97                              element(name("include"), "**/*.pdf"))));
98          }
99  
100         return element("resources", r.toArray(new MojoExecutor.Element[r.size()]));
101     }
102 
103     /**
104      * Enclose methods to run plugins.
105      */
106     class Executor extends MojoExecutor {
107 
108         /**
109          * Lay out built documents.
110          *
111          * @throws MojoExecutionException Failed to lay out documents.
112          */
113         public void layout() throws MojoExecutionException {
114             final File outputDir = new File(m.getReleaseDirectory(), m.getReleaseVersion());
115 
116             executeMojo(
117                     plugin(
118                             groupId("org.apache.maven.plugins"),
119                             artifactId("maven-resources-plugin"),
120                             version(m.getMavenResourcesVersion())),
121                     goal("copy-resources"),
122                     configuration(
123                             element(name("encoding"), "UTF-8"),
124                             element(name("outputDirectory"), m.path(outputDir)),
125                             getResources()),
126                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
127 
128 
129             // Optionally copy an entire directory of arbitrary resources, too.
130             if (m.doCopyResourceFiles() && m.getResourcesDirectory().exists()) {
131                 try {
132                     FileUtils.copyDirectoryToDirectory(m.getResourcesDirectory(), outputDir);
133                 } catch (IOException e) {
134                     throw new MojoExecutionException("Failed to copy resources", e);
135                 }
136             }
137         }
138     }
139 }