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.File; |
23 | |
import java.io.FileInputStream; |
24 | |
import java.io.FileOutputStream; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStream; |
27 | |
import java.io.OutputStream; |
28 | |
import java.util.Properties; |
29 | |
|
30 | |
import org.apache.maven.project.MavenProject; |
31 | |
import org.codehaus.plexus.archiver.Archiver; |
32 | |
import org.codehaus.plexus.archiver.ArchiverException; |
33 | |
import org.codehaus.plexus.util.IOUtil; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 18 | public class PomPropertiesUtil |
42 | |
{ |
43 | |
private static final String GENERATED_BY_MAVEN = "Generated by Maven"; |
44 | |
|
45 | |
private boolean sameContents( Properties props, File file ) |
46 | |
throws IOException |
47 | |
{ |
48 | 4 | if ( !file.isFile() ) |
49 | |
{ |
50 | 1 | return false; |
51 | |
} |
52 | 3 | Properties fileProps = new Properties(); |
53 | 3 | InputStream istream = null; |
54 | |
try |
55 | |
{ |
56 | 3 | istream = new FileInputStream( file ); |
57 | 3 | fileProps.load( istream ); |
58 | 3 | istream.close(); |
59 | 3 | istream = null; |
60 | 3 | return fileProps.equals( props ); |
61 | |
} |
62 | 0 | catch ( IOException e ) |
63 | |
{ |
64 | 0 | return false; |
65 | |
} |
66 | |
finally |
67 | |
{ |
68 | 3 | IOUtil.close( istream ); |
69 | |
} |
70 | |
} |
71 | |
|
72 | |
private void createPropertyFile( Properties properties, File outputFile, |
73 | |
boolean forceCreation ) |
74 | |
throws IOException |
75 | |
{ |
76 | 18 | File outputDir = outputFile.getParentFile(); |
77 | 18 | if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() ) |
78 | |
{ |
79 | 0 | throw new IOException( "Failed to create directory: " + outputDir ); |
80 | |
} |
81 | 18 | if ( !forceCreation && sameContents( properties, outputFile ) ) |
82 | |
{ |
83 | 3 | return; |
84 | |
} |
85 | 15 | OutputStream os = new FileOutputStream( outputFile ); |
86 | |
try |
87 | |
{ |
88 | 15 | properties.store( os, GENERATED_BY_MAVEN ); |
89 | 15 | os.close(); |
90 | 15 | os = null; |
91 | |
} |
92 | |
finally |
93 | |
{ |
94 | 15 | IOUtil.close( os ); |
95 | 15 | } |
96 | 15 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public void createPomProperties( MavenProject project, Archiver archiver, File pomPropertiesFile, |
102 | |
boolean forceCreation ) |
103 | |
throws ArchiverException, IOException |
104 | |
{ |
105 | 18 | final String artifactId = project.getArtifactId(); |
106 | 18 | final String groupId = project.getGroupId(); |
107 | |
|
108 | 18 | Properties p = new Properties(); |
109 | |
|
110 | 18 | p.setProperty( "groupId", project.getGroupId() ); |
111 | |
|
112 | 18 | p.setProperty( "artifactId", project.getArtifactId() ); |
113 | |
|
114 | 18 | p.setProperty( "version", project.getVersion() ); |
115 | |
|
116 | 18 | createPropertyFile( p, pomPropertiesFile, forceCreation ); |
117 | |
|
118 | 18 | archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" ); |
119 | 18 | } |
120 | |
} |