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.execution.MavenSession;
31 import org.apache.maven.project.MavenProject;
32 import org.codehaus.plexus.archiver.Archiver;
33 import org.apache.maven.shared.utils.io.IOUtil;
34
35
36
37
38
39
40 public class PomPropertiesUtil
41 {
42 private static final String CREATED_BY_MAVEN = "Created by Apache Maven";
43
44 private Properties loadPropertiesFile( File file )
45 throws IOException
46 {
47 Properties fileProps = new Properties();
48 InputStream istream = null;
49 try
50 {
51 istream = new FileInputStream( file );
52 fileProps.load( istream );
53 istream.close();
54 istream = null;
55 return fileProps;
56 }
57 finally
58 {
59 IOUtil.close( istream );
60 }
61 }
62
63 private boolean sameContents( Properties props, File file )
64 throws IOException
65 {
66 if ( !file.isFile() )
67 {
68 return false;
69 }
70
71 Properties fileProps = loadPropertiesFile( file );
72 return fileProps.equals( props );
73 }
74
75 private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,
76 boolean forceCreation )
77 throws IOException
78 {
79 File outputDir = outputFile.getParentFile();
80 if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
81 {
82 throw new IOException( "Failed to create directory: " + outputDir );
83 }
84 if ( !forceCreation && sameContents( properties, outputFile ) )
85 {
86 return;
87 }
88 OutputStream os = new FileOutputStream( outputFile );
89 try
90 {
91 String createdBy = CREATED_BY_MAVEN;
92 if ( session != null )
93 {
94 String mavenVersion = session.getSystemProperties().getProperty( "maven.version" );
95 if ( mavenVersion != null )
96 {
97 createdBy += " " + mavenVersion;
98 }
99 }
100
101 properties.store( os, createdBy );
102 os.close();
103 os = null;
104 }
105 finally
106 {
107 IOUtil.close( os );
108 }
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122 public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
123 File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
124 throws IOException
125 {
126 final String groupId = project.getGroupId();
127 final String artifactId = project.getArtifactId();
128 final String version = project.getVersion();
129
130 Properties p;
131
132 if ( customPomPropertiesFile != null )
133 {
134 p = loadPropertiesFile( customPomPropertiesFile );
135 }
136 else
137 {
138 p = new Properties();
139 }
140
141 p.setProperty( "groupId", groupId );
142
143 p.setProperty( "artifactId", artifactId );
144
145 p.setProperty( "version", version );
146
147 createPropertiesFile( session, p, pomPropertiesFile, forceCreation );
148
149 archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
150 }
151 }