Skip to content

Commit ca8e277

Browse files
committed
[MSHARED-154] pomPropertiesFile does not actually permit override
git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1726860 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7624aa9 commit ca8e277

File tree

4 files changed

+153
-28
lines changed

4 files changed

+153
-28
lines changed

src/main/java/org/apache/maven/archiver/MavenArchiver.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -572,13 +572,12 @@ public void createArchive( MavenSession session, MavenProject project,
572572
// Create pom.properties file
573573
// ----------------------------------------------------------------------
574574

575-
File pomPropertiesFile = archiveConfiguration.getPomPropertiesFile();
576-
if ( pomPropertiesFile == null )
577-
{
578-
File dir = new File( workingProject.getBuild().getDirectory(), "maven-archiver" );
579-
pomPropertiesFile = new File( dir, "pom.properties" );
580-
}
581-
new PomPropertiesUtil().createPomProperties( session, workingProject, archiver, pomPropertiesFile, forced );
575+
File customPomPropertiesFile = archiveConfiguration.getPomPropertiesFile();
576+
File dir = new File( workingProject.getBuild().getDirectory(), "maven-archiver" );
577+
File pomPropertiesFile = new File( dir, "pom.properties" );
578+
579+
new PomPropertiesUtil().createPomProperties( session, workingProject, archiver,
580+
customPomPropertiesFile, pomPropertiesFile, forced );
582581
}
583582

584583
// ----------------------------------------------------------------------

src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java

+33-18
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ public class PomPropertiesUtil
4141
{
4242
private static final String CREATED_BY_MAVEN = "Created by Apache Maven";
4343

44-
private boolean sameContents( Properties props, File file )
44+
private Properties loadPropertiesFile( File file )
4545
throws IOException
4646
{
47-
if ( !file.isFile() )
48-
{
49-
return false;
50-
}
5147
Properties fileProps = new Properties();
5248
InputStream istream = null;
5349
try
@@ -56,18 +52,26 @@ private boolean sameContents( Properties props, File file )
5652
fileProps.load( istream );
5753
istream.close();
5854
istream = null;
59-
return fileProps.equals( props );
60-
}
61-
catch ( IOException e )
62-
{
63-
return false;
55+
return fileProps;
6456
}
6557
finally
6658
{
6759
IOUtil.close( istream );
6860
}
6961
}
7062

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+
7175
private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,
7276
boolean forceCreation )
7377
throws IOException
@@ -106,28 +110,39 @@ private void createPropertiesFile( MavenSession session, Properties properties,
106110

107111
/**
108112
* Creates the pom.properties file.
109-
* @param session TODO
113+
* @param session {@link MavenSession}
110114
* @param project {@link MavenProject}
111115
* @param archiver {@link Archiver}
116+
* @param customPomPropertiesFile optional custom pom properties file
112117
* @param pomPropertiesFile The pom properties file.
113-
* @param forceCreation force creation true/flas.e
118+
* @param forceCreation force creation true/false
114119
* @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
115120
* @throws IOException IO exception.
116121
*/
117122
public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
118-
File pomPropertiesFile, boolean forceCreation )
123+
File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
119124
throws IOException
120125
{
121-
final String artifactId = project.getArtifactId();
122126
final String groupId = project.getGroupId();
127+
final String artifactId = project.getArtifactId();
128+
final String version = project.getVersion();
129+
130+
Properties p;
123131

124-
Properties p = new Properties();
132+
if ( customPomPropertiesFile != null )
133+
{
134+
p = loadPropertiesFile( customPomPropertiesFile );
135+
}
136+
else
137+
{
138+
p = new Properties();
139+
}
125140

126-
p.setProperty( "groupId", project.getGroupId() );
141+
p.setProperty( "groupId", groupId );
127142

128-
p.setProperty( "artifactId", project.getArtifactId() );
143+
p.setProperty( "artifactId", artifactId );
129144

130-
p.setProperty( "version", project.getVersion() );
145+
p.setProperty( "version", version );
131146

132147
createPropertiesFile( session, p, pomPropertiesFile, forceCreation );
133148

src/test/java/org/apache/maven/archiver/MavenArchiverTest.java

+92-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.io.InputStream;
56
import java.net.URI;
67
import java.net.URL;
78
import java.util.ArrayList;
@@ -17,6 +18,9 @@
1718
import java.util.jar.Attributes;
1819
import java.util.jar.JarFile;
1920
import java.util.jar.Manifest;
21+
import java.util.zip.ZipEntry;
22+
23+
import junit.framework.TestCase;
2024

2125
import org.apache.maven.artifact.Artifact;
2226
import org.apache.maven.artifact.DependencyResolutionRequiredException;
@@ -34,12 +38,12 @@
3438
import org.apache.maven.project.MavenProject;
3539
import org.apache.maven.shared.utils.StringUtils;
3640
import org.apache.maven.shared.utils.io.FileUtils;
41+
import org.apache.maven.shared.utils.io.IOUtil;
3742
import org.codehaus.plexus.PlexusContainer;
3843
import org.codehaus.plexus.archiver.jar.JarArchiver;
3944
import org.codehaus.plexus.archiver.jar.ManifestException;
4045
import org.sonatype.aether.RepositorySystemSession;
4146
import org.sonatype.aether.util.DefaultRepositorySystemSession;
42-
4347
/*
4448
* Licensed to the Apache Software Foundation (ASF) under one
4549
* or more contributor license agreements. See the NOTICE file
@@ -59,8 +63,6 @@
5963
* under the License.
6064
*/
6165

62-
import junit.framework.TestCase;
63-
6466
public class MavenArchiverTest
6567
extends TestCase
6668
{
@@ -819,6 +821,76 @@ public void testCustomClassPathValue_WithSnapshotForcingBaseVersion()
819821
assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
820822
}
821823

824+
public void testDefaultPomProperties()
825+
throws Exception
826+
{
827+
MavenSession session = getDummySession();
828+
MavenProject project = getDummyProject();
829+
File jarFile = new File( "target/test/dummy.jar" );
830+
JarArchiver jarArchiver = getCleanJarArchiver( jarFile );
831+
832+
MavenArchiver archiver = getMavenArchiver( jarArchiver );
833+
834+
MavenArchiveConfiguration config = new MavenArchiveConfiguration();
835+
config.setForced( true );
836+
archiver.createArchive( session, project, config );
837+
assertTrue( jarFile.exists() );
838+
839+
final String groupId = project.getGroupId();
840+
final String artifactId = project.getArtifactId();
841+
final String version = project.getVersion();
842+
843+
JarFile virtJarFile = new JarFile( jarFile );
844+
ZipEntry pomPropertiesEntry = virtJarFile.getEntry( "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
845+
assertNotNull( pomPropertiesEntry );
846+
847+
InputStream is = virtJarFile.getInputStream( pomPropertiesEntry );
848+
Properties p = loadProperties( is );
849+
850+
assertEquals( groupId, p.getProperty( "groupId" ) );
851+
assertEquals( artifactId, p.getProperty( "artifactId" ) );
852+
assertEquals( version, p.getProperty( "version" ) );
853+
854+
virtJarFile.close();
855+
}
856+
857+
public void testCustomPomProperties()
858+
throws Exception
859+
{
860+
MavenSession session = getDummySession();
861+
MavenProject project = getDummyProject();
862+
File jarFile = new File( "target/test/dummy.jar" );
863+
JarArchiver jarArchiver = getCleanJarArchiver( jarFile );
864+
865+
MavenArchiver archiver = getMavenArchiver( jarArchiver );
866+
867+
File customPomPropertiesFile = new File( "src/test/resources/custom-pom.properties" );
868+
MavenArchiveConfiguration config = new MavenArchiveConfiguration();
869+
config.setForced( true );
870+
config.setPomPropertiesFile( customPomPropertiesFile );
871+
archiver.createArchive( session, project, config );
872+
assertTrue( jarFile.exists() );
873+
874+
final String groupId = project.getGroupId();
875+
final String artifactId = project.getArtifactId();
876+
final String version = project.getVersion();
877+
878+
JarFile virtJarFile = new JarFile( jarFile );
879+
ZipEntry pomPropertiesEntry = virtJarFile.getEntry( "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
880+
assertNotNull( pomPropertiesEntry );
881+
882+
InputStream is = virtJarFile.getInputStream( pomPropertiesEntry );
883+
Properties p = loadProperties( is );
884+
885+
assertEquals( groupId, p.getProperty( "groupId" ) );
886+
assertEquals( artifactId, p.getProperty( "artifactId" ) );
887+
assertEquals( version, p.getProperty( "version" ) );
888+
assertEquals( "1337", p.getProperty("build.revision" ) );
889+
assertEquals( "tags/0.1.1", p.getProperty("build.branch" ) );
890+
891+
virtJarFile.close();
892+
}
893+
822894
private JarArchiver getCleanJarArchiver( File jarFile )
823895
{
824896
deleteAndAssertNotPresent( jarFile );
@@ -1070,6 +1142,23 @@ private Set<Artifact> getArtifacts( Artifact... artifacts )
10701142
return result;
10711143
}
10721144

1145+
private Properties loadProperties( InputStream is )
1146+
throws IOException
1147+
{
1148+
Properties p = new Properties();
1149+
try
1150+
{
1151+
p.load( is );
1152+
is.close();
1153+
is = null;
1154+
return p;
1155+
}
1156+
finally
1157+
{
1158+
IOUtil.close( is );
1159+
}
1160+
}
1161+
10731162
public Manifest getJarFileManifest( File jarFile )
10741163
throws IOException
10751164
{
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
groupId = fake
19+
artifactId = bogus
20+
version = -1
21+
build.revision = 1337
22+
build.branch = tags/0.1.1

0 commit comments

Comments
 (0)