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 2012-2014 ForgeRock AS 015 */ 016 017package org.forgerock.doc.maven.pre; 018 019import org.apache.commons.io.FileUtils; 020import org.apache.maven.plugin.MojoExecutionException; 021import org.forgerock.doc.maven.AbstractDocbkxMojo; 022import org.twdata.maven.mojoexecutor.MojoExecutor; 023 024import java.io.File; 025import java.io.IOException; 026 027/** 028 * Augment the modifiable copy DocBook XML sources with common content. 029 * The common content is unpacked from a separate artifact. 030 */ 031public class CommonContent { 032 033 /** 034 * The Mojo that holds configuration and related methods. 035 */ 036 private AbstractDocbkxMojo m; 037 038 /** 039 * The Executor to run the dependency plugin. 040 */ 041 private final Executor executor; 042 043 /** 044 * Constructor setting the Mojo that holds the configuration. 045 * 046 * @param mojo The Mojo that holds the configuration. 047 */ 048 public CommonContent(final AbstractDocbkxMojo mojo) { 049 m = mojo; 050 this.executor = new Executor(); 051 } 052 053 /** 054 * Augment the modifiable copy of DocBook XML sources with common content. 055 * 056 * @throws MojoExecutionException Failed to unpack common content. 057 */ 058 public void execute() throws MojoExecutionException { 059 060 // As shown in https://github.com/markcraig/unpack-test 061 // the maven-dependency-plugin overWrite* options 062 // do not prevent existing files from being overwritten. 063 064 File sharedContentUnpackDir; 065 066 if (m.doOverwriteProjectFilesWithSharedContent()) { // Do overwrite 067 sharedContentUnpackDir = m.getDocbkxModifiableSourcesDirectory(); 068 executor.unpack(m.path(sharedContentUnpackDir)); 069 } else { // Do not overwrite 070 try { 071 // Unpack without overwriting. 072 sharedContentUnpackDir = createTemporaryDirectory(); 073 executor.unpack(m.path(sharedContentUnpackDir)); 074 075 // Now overwrite what was unpacked with the existing files, 076 // and then replace our copy with the updated unpacked files. 077 FileUtils.copyDirectory(m.getDocbkxModifiableSourcesDirectory(), sharedContentUnpackDir); 078 FileUtils.deleteDirectory(m.getDocbkxModifiableSourcesDirectory()); 079 FileUtils.moveDirectory(sharedContentUnpackDir, m.getDocbkxModifiableSourcesDirectory()); 080 } catch (IOException e) { 081 throw new MojoExecutionException("Failed to unpack common content.", e); 082 } 083 } 084 } 085 086 /** 087 * Try to create a writable temporary directory with a unique name. 088 * 089 * @return The temporary directory. 090 * @throws IOException Failed to create the directory. 091 */ 092 private File createTemporaryDirectory() throws IOException { 093 File temporaryDirectory = new File( 094 m.getBuildDirectory(), Long.toString(System.nanoTime())); 095 096 if (!temporaryDirectory.mkdir() && temporaryDirectory.canWrite()) { 097 throw new IOException("Failed to create temporary directory: " 098 + temporaryDirectory.getAbsolutePath()); 099 } 100 101 return temporaryDirectory; 102 } 103 104 /** 105 * Enclose methods to run plugins. 106 */ 107 class Executor extends MojoExecutor { 108 109 /** 110 * Unpack common content from the common content artifact. 111 * 112 * @param outputDir Where to unpack common content 113 * @throws MojoExecutionException Failed to unpack common content. 114 */ 115 public void unpack(final String outputDir) throws MojoExecutionException { 116 117 executeMojo( 118 plugin( 119 groupId("org.apache.maven.plugins"), 120 artifactId("maven-dependency-plugin"), 121 version(m.getMavenDependencyVersion())), 122 goal("unpack"), 123 configuration( 124 element("artifactItems", 125 element("artifactItem", 126 element("groupId", m.getCommonContentGroupId()), 127 element("artifactId", m.getCommonContentArtifactId()), 128 element("version", m.getCommonContentVersion()), 129 element("type", "jar"), 130 element("overWrite", "true"), 131 element("outputDirectory", outputDir), 132 element("includes", "**/*.*")))), 133 executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager())); 134 } 135 } 136}