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.pre;
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  
27  /**
28   * Augment the modifiable copy DocBook XML sources with common content.
29   * The common content is unpacked from a separate artifact.
30   */
31  public class CommonContent {
32  
33      /**
34       * The Mojo that holds configuration and related methods.
35       */
36      private AbstractDocbkxMojo m;
37  
38      /**
39       * The Executor to run the dependency plugin.
40       */
41      private final Executor executor;
42  
43      /**
44       * Constructor setting the Mojo that holds the configuration.
45       *
46       * @param mojo The Mojo that holds the configuration.
47       */
48      public CommonContent(final AbstractDocbkxMojo mojo) {
49          m = mojo;
50          this.executor = new Executor();
51      }
52  
53      /**
54       * Augment the modifiable copy of DocBook XML sources with common content.
55       *
56       * @throws MojoExecutionException Failed to unpack common content.
57       */
58      public void execute() throws MojoExecutionException {
59  
60          // As shown in https://github.com/markcraig/unpack-test
61          // the maven-dependency-plugin overWrite* options
62          // do not prevent existing files from being overwritten.
63  
64          File sharedContentUnpackDir;
65  
66          if (m.doOverwriteProjectFilesWithSharedContent()) { // Do overwrite
67              sharedContentUnpackDir = m.getDocbkxModifiableSourcesDirectory();
68              executor.unpack(m.path(sharedContentUnpackDir));
69          } else {                                            // Do not overwrite
70              try {
71                  // Unpack without overwriting.
72                  sharedContentUnpackDir = createTemporaryDirectory();
73                  executor.unpack(m.path(sharedContentUnpackDir));
74  
75                  // Now overwrite what was unpacked with the existing files,
76                  // and then replace our copy with the updated unpacked files.
77                  FileUtils.copyDirectory(m.getDocbkxModifiableSourcesDirectory(), sharedContentUnpackDir);
78                  FileUtils.deleteDirectory(m.getDocbkxModifiableSourcesDirectory());
79                  FileUtils.moveDirectory(sharedContentUnpackDir, m.getDocbkxModifiableSourcesDirectory());
80              } catch (IOException e) {
81                  throw new MojoExecutionException("Failed to unpack common content.", e);
82              }
83          }
84      }
85  
86      /**
87       * Try to create a writable temporary directory with a unique name.
88       *
89       * @return The temporary directory.
90       * @throws IOException Failed to create the directory.
91       */
92      private File createTemporaryDirectory() throws IOException {
93          File temporaryDirectory = new File(
94                  m.getBuildDirectory(), Long.toString(System.nanoTime()));
95  
96          if (!temporaryDirectory.mkdir() && temporaryDirectory.canWrite()) {
97              throw new IOException("Failed to create temporary directory: "
98                      + temporaryDirectory.getAbsolutePath());
99          }
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 }