View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.devops.conga.tooling.maven.plugin;
21  
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStream;
26  import java.nio.file.Files;
27  import java.util.Properties;
28  
29  import org.apache.maven.plugin.AbstractMojo;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  import org.apache.maven.plugins.annotations.LifecyclePhase;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.project.MavenProject;
36  
37  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
38  import io.wcm.devops.conga.generator.GeneratorOptions;
39  import io.wcm.devops.conga.generator.util.FileUtil;
40  import io.wcm.devops.conga.tooling.maven.plugin.util.VersionInfoUtil;
41  
42  /**
43   * Generates a file with information CONGA maven plugin version and CONGA plugins versions used to generate this
44   * artifact.
45   */
46  @Mojo(name = "generate-version-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresProject = true, threadSafe = true)
47  public class GenerateVersionInfoMojo extends AbstractMojo {
48  
49    /**
50     * Target path for the prepared definition files.
51     */
52    @Parameter(defaultValue = "${project.build.directory}/definitions")
53    private String definitionTarget;
54  
55    @Parameter(property = "project", required = true, readonly = true)
56    private MavenProject project;
57  
58    @Override
59    @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
60    public void execute() throws MojoExecutionException, MojoFailureException {
61      File outputDir = new File(definitionTarget, GeneratorOptions.CLASSPATH_PREFIX);
62      if (!outputDir.exists()) {
63        outputDir.mkdirs();
64      }
65  
66      File propsFile = new File(outputDir, BuildConstants.FILE_VERSION_INFO);
67      if (propsFile.exists()) {
68        try {
69          Files.delete(propsFile.toPath());
70        }
71        catch (IOException ex) {
72          throw new MojoExecutionException("Unable to delete file: " + FileUtil.getCanonicalPath(propsFile), ex);
73        }
74      }
75      Properties versionInfo = VersionInfoUtil.getVersionInfoProperties(project);
76      try (OutputStream os = new FileOutputStream(propsFile)) {
77        versionInfo.store(os, "CONGA Version Info");
78      }
79      catch (IOException ex) {
80        throw new MojoExecutionException("Error generating version info to "
81            + FileUtil.getCanonicalPath(propsFile) + ": " + ex.getMessage(), ex);
82      }
83    }
84  
85  }