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