|
2 | 2 |
|
3 | 3 | import java.io.File;
|
4 | 4 | import java.io.IOException;
|
| 5 | +import java.io.InputStream; |
5 | 6 | import java.net.URI;
|
6 | 7 | import java.net.URL;
|
7 | 8 | import java.util.ArrayList;
|
|
17 | 18 | import java.util.jar.Attributes;
|
18 | 19 | import java.util.jar.JarFile;
|
19 | 20 | import java.util.jar.Manifest;
|
| 21 | +import java.util.zip.ZipEntry; |
| 22 | + |
| 23 | +import junit.framework.TestCase; |
20 | 24 |
|
21 | 25 | import org.apache.maven.artifact.Artifact;
|
22 | 26 | import org.apache.maven.artifact.DependencyResolutionRequiredException;
|
|
34 | 38 | import org.apache.maven.project.MavenProject;
|
35 | 39 | import org.apache.maven.shared.utils.StringUtils;
|
36 | 40 | import org.apache.maven.shared.utils.io.FileUtils;
|
| 41 | +import org.apache.maven.shared.utils.io.IOUtil; |
37 | 42 | import org.codehaus.plexus.PlexusContainer;
|
38 | 43 | import org.codehaus.plexus.archiver.jar.JarArchiver;
|
39 | 44 | import org.codehaus.plexus.archiver.jar.ManifestException;
|
40 | 45 | import org.sonatype.aether.RepositorySystemSession;
|
41 | 46 | import org.sonatype.aether.util.DefaultRepositorySystemSession;
|
42 |
| - |
43 | 47 | /*
|
44 | 48 | * Licensed to the Apache Software Foundation (ASF) under one
|
45 | 49 | * or more contributor license agreements. See the NOTICE file
|
|
59 | 63 | * under the License.
|
60 | 64 | */
|
61 | 65 |
|
62 |
| -import junit.framework.TestCase; |
63 |
| - |
64 | 66 | public class MavenArchiverTest
|
65 | 67 | extends TestCase
|
66 | 68 | {
|
@@ -819,6 +821,76 @@ public void testCustomClassPathValue_WithSnapshotForcingBaseVersion()
|
819 | 821 | assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
|
820 | 822 | }
|
821 | 823 |
|
| 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 | + |
822 | 894 | private JarArchiver getCleanJarArchiver( File jarFile )
|
823 | 895 | {
|
824 | 896 | deleteAndAssertNotPresent( jarFile );
|
@@ -1070,6 +1142,23 @@ private Set<Artifact> getArtifacts( Artifact... artifacts )
|
1070 | 1142 | return result;
|
1071 | 1143 | }
|
1072 | 1144 |
|
| 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 | + |
1073 | 1162 | public Manifest getJarFileManifest( File jarFile )
|
1074 | 1163 | throws IOException
|
1075 | 1164 | {
|
|
0 commit comments