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.Properties;
31
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.shared.utils.io.IOUtil;
35 import org.codehaus.plexus.archiver.Archiver;
36
37
38
39
40
41
42 public class PomPropertiesUtil
43 {
44 private static final String CREATED_BY_MAVEN = "Created by Apache Maven";
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 String createdBy = CREATED_BY_MAVEN;
94 if ( session != null )
95 {
96 String mavenVersion = session.getSystemProperties().getProperty( "maven.version" );
97 if ( mavenVersion != null )
98 {
99 createdBy += " " + mavenVersion;
100 }
101 }
102
103 StringWriter sw = new StringWriter();
104 properties.store( sw, null );
105
106 BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) );
107
108 pw.println( "#" + createdBy );
109 String line;
110 while ( ( line = r.readLine() ) != null )
111 {
112 if ( !line.startsWith( "#" ) )
113 {
114 pw.println( line );
115 }
116 }
117
118 r.close();
119 r = null;
120 sw.close();
121 sw = null;
122 pw.close();
123 pw = null;
124 }
125 finally
126 {
127 IOUtil.close( pw );
128 }
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142 public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
143 File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
144 throws IOException
145 {
146 final String groupId = project.getGroupId();
147 final String artifactId = project.getArtifactId();
148 final String version = project.getVersion();
149
150 Properties p;
151
152 if ( customPomPropertiesFile != null )
153 {
154 p = loadPropertiesFile( customPomPropertiesFile );
155 }
156 else
157 {
158 p = new Properties();
159 }
160
161 p.setProperty( "groupId", groupId );
162
163 p.setProperty( "artifactId", artifactId );
164
165 p.setProperty( "version", version );
166
167 createPropertiesFile( session, p, pomPropertiesFile, forceCreation );
168
169 archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
170 }
171 }