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.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.PrintWriter;
28 import java.io.StringReader;
29 import java.io.StringWriter;
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 public class PomPropertiesUtil
44 {
45 private Properties loadPropertiesFile( File file )
46 throws IOException
47 {
48 Properties fileProps = new Properties();
49 try ( InputStream istream = new FileInputStream( file ) )
50 {
51 fileProps.load( istream );
52 return fileProps;
53 }
54 }
55
56 private boolean sameContents( Properties props, File file )
57 throws IOException
58 {
59 if ( !file.isFile() )
60 {
61 return false;
62 }
63
64 Properties fileProps = loadPropertiesFile( file );
65 return fileProps.equals( props );
66 }
67
68 private void createPropertiesFile( Properties properties, File outputFile, boolean forceCreation )
69 throws IOException
70 {
71 File outputDir = outputFile.getParentFile();
72 if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
73 {
74 throw new IOException( "Failed to create directory: " + outputDir );
75 }
76 if ( !forceCreation && sameContents( properties, outputFile ) )
77 {
78 return;
79 }
80
81 try ( PrintWriter pw = new PrintWriter( outputFile, "ISO-8859-1" );
82 StringWriter sw = new StringWriter() )
83 {
84
85 properties.store( sw, null );
86
87 List<String> lines = new ArrayList<>();
88 try ( BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) ) )
89 {
90 String line;
91 while ( ( line = r.readLine() ) != null )
92 {
93 if ( !line.startsWith( "#" ) )
94 {
95 lines.add( line );
96 }
97 }
98 }
99
100 Collections.sort( lines );
101 for ( String l : lines )
102 {
103 pw.println( l );
104 }
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119 public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
120 File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
121 throws IOException
122 {
123 final String groupId = project.getGroupId();
124 final String artifactId = project.getArtifactId();
125 final String version = project.getVersion();
126
127 Properties p;
128
129 if ( customPomPropertiesFile != null )
130 {
131 p = loadPropertiesFile( customPomPropertiesFile );
132 }
133 else
134 {
135 p = new Properties();
136 }
137
138 p.setProperty( "groupId", groupId );
139
140 p.setProperty( "artifactId", artifactId );
141
142 p.setProperty( "version", version );
143
144 createPropertiesFile( p, pomPropertiesFile, forceCreation );
145
146 archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
147 }
148 }