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 2015 ForgeRock AS
15   */
16  
17  package org.forgerock.doc.maven.pre;
18  
19  import freemarker.template.Configuration;
20  import freemarker.template.Template;
21  import freemarker.template.TemplateException;
22  import freemarker.template.TemplateExceptionHandler;
23  import org.apache.commons.io.FileUtils;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.forgerock.doc.maven.AbstractDocbkxMojo;
26  import org.twdata.maven.mojoexecutor.MojoExecutor;
27  
28  import java.io.File;
29  import java.io.FileOutputStream;
30  import java.io.IOException;
31  import java.io.OutputStreamWriter;
32  import java.io.Writer;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * Build Maven artifact from pre-processed documents.
38   */
39  public class ArtifactBuilder {
40  
41      /**
42       * The Mojo that holds configuration and related methods.
43       */
44      private AbstractDocbkxMojo m;
45  
46      /**
47       * The Executor to run plugins.
48       */
49      private final Executor executor;
50  
51  
52      /**
53       * FreeMarker configuration for the assembly descriptor template.
54       */
55      private Configuration configuration;
56  
57      /**
58       * Constructor setting the Mojo that holds the configuration.
59       *
60       * @param mojo The Mojo that holds the configuration.
61       */
62      public ArtifactBuilder(final AbstractDocbkxMojo mojo) {
63          m = mojo;
64          this.executor = new Executor();
65  
66          // Set up the FreeMarker configuration.
67          configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
68          configuration.setClassForTemplateLoading(ArtifactBuilder.class, "/templates");
69          configuration.setDefaultEncoding("UTF-8");
70          configuration.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
71      }
72  
73      /**
74       * Get the path to the assembly descriptor.
75       *
76       * @return The path to the assembly descriptor.
77       */
78      private String getDescriptorPath(File descriptorFile) throws IOException, TemplateException {
79          Template template = configuration.getTemplate("artifact.ftl");
80  
81          FileOutputStream out = FileUtils.openOutputStream(descriptorFile);
82  
83          Writer writer = new OutputStreamWriter(out);
84          template.process(getModel(), writer);
85          writer.close();
86          out.close();
87  
88          return descriptorFile.getPath();
89      }
90  
91      /**
92       * Get the model used to build the assembly descriptor.
93       *
94       * @return The model used to build the assembly descriptor.
95       */
96      private Map<String, String> getModel() {
97          Map<String, String> map = new HashMap<String, String>();
98          map.put("modifiedSourcesDirectory", m.path(m.getDocbkxModifiableSourcesDirectory()));
99          return map;
100     }
101 
102     /**
103      * Build artifact.
104      *
105      * @throws MojoExecutionException Failed to build artifact.
106      */
107     public void execute() throws MojoExecutionException {
108         executor.build();
109     }
110 
111     /**
112      * Enclose methods to run plugins.
113      */
114     class Executor extends MojoExecutor {
115 
116         /**
117          * Build the Maven artifact.
118          *
119          * @throws MojoExecutionException Failed to build the artifact.
120          */
121         public void build() throws MojoExecutionException {
122 
123             File descriptorFile;
124             String descriptorPath;
125             try {
126                 descriptorFile = File.createTempFile("descriptor", "xml");
127                 descriptorPath = getDescriptorPath(descriptorFile);
128             } catch (Exception e) {
129                 throw new MojoExecutionException("Failed to get assembly descriptor", e);
130             }
131 
132             executeMojo(
133                     plugin(
134                             groupId("org.apache.maven.plugins"),
135                             artifactId("maven-assembly-plugin"),
136                             version(m.getMavenAssemblyVersion())),
137                     goal("single"),
138                     configuration(
139                             element("descriptors",
140                                     element("descriptor", descriptorPath))),
141                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
142 
143             if (descriptorFile.exists()) {
144                 descriptorFile.delete();
145             }
146         }
147     }
148 }