1 package org.apache.maven.archiver;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.PrintWriter;
27 import java.io.StringReader;
28 import java.io.StringWriter;
29 import java.nio.file.Files;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Properties;
34
35 import org.apache.maven.execution.MavenSession;
36 import org.apache.maven.project.MavenProject;
37 import org.codehaus.plexus.archiver.Archiver;
38
39
40
41
42
43
44
45
46 public class PomPropertiesUtil
47 {
48 private Properties loadPropertiesFile( File file )
49 throws IOException
50 {
51 Properties fileProps = new Properties();
52 try ( InputStream istream = Files.newInputStream( file.toPath() ) )
53 {
54 fileProps.load( istream );
55 return fileProps;
56 }
57 }
58
59 private boolean sameContents( Properties props, File file )
60 throws IOException
61 {
62 if ( !file.isFile() )
63 {
64 return false;
65 }
66
67 Properties fileProps = loadPropertiesFile( file );
68 return fileProps.equals( props );
69 }
70
71 private void createPropertiesFile( Properties properties, File outputFile, boolean forceCreation )
72 throws IOException
73 {
74 File outputDir = outputFile.getParentFile();
75 if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
76 {
77 throw new IOException( "Failed to create directory: " + outputDir );
78 }
79 if ( !forceCreation && sameContents( properties, outputFile ) )
80 {
81 return;
82 }
83
84 try ( PrintWriter pw = new PrintWriter( outputFile, "ISO-8859-1" );
85 StringWriter sw = new StringWriter() )
86 {
87
88 properties.store( sw, null );
89
90 List<String> lines = new ArrayList<>();
91 try ( BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) ) )
92 {
93 String line;
94 while ( ( line = r.readLine() ) != null )
95 {
96 if ( !line.startsWith( "#" ) )
97 {
98 lines.add( line );
99 }
100 }
101 }
102
103 Collections.sort( lines );
104 for ( String l : lines )
105 {
106 pw.println( l );
107 }
108 }
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
124 File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
125 throws IOException
126 {
127 final String groupId = project.getGroupId();
128 final String artifactId = project.getArtifactId();
129 final String version = project.getVersion();
130
131 Properties p;
132
133 if ( customPomPropertiesFile != null )
134 {
135 p = loadPropertiesFile( customPomPropertiesFile );
136 }
137 else
138 {
139 p = new Properties();
140 }
141
142 p.setProperty( "groupId", groupId );
143
144 p.setProperty( "artifactId", artifactId );
145
146 p.setProperty( "version", version );
147
148 createPropertiesFile( p, pomPropertiesFile, forceCreation );
149
150 archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
151 }
152 }