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