001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015 ForgeRock AS 015 */ 016 017package org.forgerock.doc.maven.pre; 018 019import freemarker.template.Configuration; 020import freemarker.template.Template; 021import freemarker.template.TemplateException; 022import freemarker.template.TemplateExceptionHandler; 023import org.apache.commons.io.FileUtils; 024import org.apache.maven.plugin.MojoExecutionException; 025import org.forgerock.doc.maven.AbstractDocbkxMojo; 026import org.twdata.maven.mojoexecutor.MojoExecutor; 027 028import java.io.File; 029import java.io.FileOutputStream; 030import java.io.IOException; 031import java.io.OutputStreamWriter; 032import java.io.Writer; 033import java.util.HashMap; 034import java.util.Map; 035 036/** 037 * Build Maven artifact from pre-processed documents. 038 */ 039public class ArtifactBuilder { 040 041 /** 042 * The Mojo that holds configuration and related methods. 043 */ 044 private AbstractDocbkxMojo m; 045 046 /** 047 * The Executor to run plugins. 048 */ 049 private final Executor executor; 050 051 052 /** 053 * FreeMarker configuration for the assembly descriptor template. 054 */ 055 private Configuration configuration; 056 057 /** 058 * Constructor setting the Mojo that holds the configuration. 059 * 060 * @param mojo The Mojo that holds the configuration. 061 */ 062 public ArtifactBuilder(final AbstractDocbkxMojo mojo) { 063 m = mojo; 064 this.executor = new Executor(); 065 066 // Set up the FreeMarker configuration. 067 configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); 068 configuration.setClassForTemplateLoading(ArtifactBuilder.class, "/templates"); 069 configuration.setDefaultEncoding("UTF-8"); 070 configuration.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER); 071 } 072 073 /** 074 * Get the path to the assembly descriptor. 075 * 076 * @return The path to the assembly descriptor. 077 */ 078 private String getDescriptorPath(File descriptorFile) throws IOException, TemplateException { 079 Template template = configuration.getTemplate("artifact.ftl"); 080 081 FileOutputStream out = FileUtils.openOutputStream(descriptorFile); 082 083 Writer writer = new OutputStreamWriter(out); 084 template.process(getModel(), writer); 085 writer.close(); 086 out.close(); 087 088 return descriptorFile.getPath(); 089 } 090 091 /** 092 * Get the model used to build the assembly descriptor. 093 * 094 * @return The model used to build the assembly descriptor. 095 */ 096 private Map<String, String> getModel() { 097 Map<String, String> map = new HashMap<String, String>(); 098 map.put("modifiedSourcesDirectory", m.path(m.getDocbkxModifiableSourcesDirectory())); 099 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}