View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.archiver;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.PrintWriter;
26  import java.io.StringReader;
27  import java.io.StringWriter;
28  import java.nio.file.Files;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.Properties;
33  
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.archiver.Archiver;
37  
38  /**
39   * This class is responsible for creating the <code>pom.properties</code> file
40   * in <code>META-INF/maven/${groupId}/${artifactId}</code>.
41   *
42   * @author slachiewicz
43   * @version $Id: $Id
44   */
45  public class PomPropertiesUtil {
46      private Properties loadPropertiesFile(File file) throws IOException {
47          Properties fileProps = new Properties();
48          try (InputStream istream = Files.newInputStream(file.toPath())) {
49              fileProps.load(istream);
50              return fileProps;
51          }
52      }
53  
54      private boolean sameContents(Properties props, File file) throws IOException {
55          if (!file.isFile()) {
56              return false;
57          }
58  
59          Properties fileProps = loadPropertiesFile(file);
60          return fileProps.equals(props);
61      }
62  
63      private void createPropertiesFile(Properties properties, File outputFile, boolean forceCreation)
64              throws IOException {
65          File outputDir = outputFile.getParentFile();
66          if (outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs()) {
67              throw new IOException("Failed to create directory: " + outputDir);
68          }
69          if (!forceCreation && sameContents(properties, outputFile)) {
70              return;
71          }
72  
73          try (PrintWriter pw = new PrintWriter(outputFile, "ISO-8859-1");
74                  StringWriter sw = new StringWriter()) {
75  
76              properties.store(sw, null);
77  
78              List<String> lines = new ArrayList<>();
79              try (BufferedReader r = new BufferedReader(new StringReader(sw.toString()))) {
80                  String line;
81                  while ((line = r.readLine()) != null) {
82                      if (!line.startsWith("#")) {
83                          lines.add(line);
84                      }
85                  }
86              }
87  
88              Collections.sort(lines);
89              for (String l : lines) {
90                  pw.println(l);
91              }
92          }
93      }
94  
95      /**
96       * Creates the pom.properties file.
97       *
98       * @param session {@link org.apache.maven.execution.MavenSession}
99       * @param project {@link org.apache.maven.project.MavenProject}
100      * @param archiver {@link org.codehaus.plexus.archiver.Archiver}
101      * @param customPomPropertiesFile optional custom pom properties file
102      * @param pomPropertiesFile The pom properties file.
103      * @param forceCreation force creation true/false
104      * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
105      * @throws java.io.IOException IO exception.
106      * @deprecated please use {@link #createPomProperties(MavenProject, Archiver, File, File, boolean)}
107      */
108     @Deprecated
109     public void createPomProperties(
110             MavenSession session,
111             MavenProject project,
112             Archiver archiver,
113             File customPomPropertiesFile,
114             File pomPropertiesFile,
115             boolean forceCreation)
116             throws IOException {
117         createPomProperties(project, archiver, customPomPropertiesFile, pomPropertiesFile, forceCreation);
118     }
119 
120     /**
121      * Creates the pom.properties file.
122      *
123      * @param project                 {@link org.apache.maven.project.MavenProject}
124      * @param archiver                {@link org.codehaus.plexus.archiver.Archiver}
125      * @param customPomPropertiesFile optional custom pom properties file
126      * @param pomPropertiesFile       The pom properties file.
127      * @param forceCreation           force creation true/false
128      * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
129      * @throws java.io.IOException                            IO exception.
130      */
131     public void createPomProperties(
132             MavenProject project,
133             Archiver archiver,
134             File customPomPropertiesFile,
135             File pomPropertiesFile,
136             boolean forceCreation)
137             throws IOException {
138         final String groupId = project.getGroupId();
139         final String artifactId = project.getArtifactId();
140         final String version = project.getVersion();
141 
142         Properties p;
143 
144         if (customPomPropertiesFile != null) {
145             p = loadPropertiesFile(customPomPropertiesFile);
146         } else {
147             p = new Properties();
148         }
149 
150         p.setProperty("groupId", groupId);
151 
152         p.setProperty("artifactId", artifactId);
153 
154         p.setProperty("version", version);
155 
156         createPropertiesFile(p, pomPropertiesFile, forceCreation);
157 
158         archiver.addFile(pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties");
159     }
160 }