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