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