You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2016/01/26 20:47:35 UTC

svn commit: r1726860 - in /maven/shared/trunk/maven-archiver/src: main/java/org/apache/maven/archiver/ test/java/org/apache/maven/archiver/ test/resources/

Author: michaelo
Date: Tue Jan 26 19:47:35 2016
New Revision: 1726860

URL: http://svn.apache.org/viewvc?rev=1726860&view=rev
Log:
[MSHARED-154] pomPropertiesFile does not actually permit override

Added:
    maven/shared/trunk/maven-archiver/src/test/resources/custom-pom.properties
Modified:
    maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java
    maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java
    maven/shared/trunk/maven-archiver/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java

Modified: maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java?rev=1726860&r1=1726859&r2=1726860&view=diff
==============================================================================
--- maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java (original)
+++ maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java Tue Jan 26 19:47:35 2016
@@ -572,13 +572,12 @@ public class MavenArchiver
             // Create pom.properties file
             // ----------------------------------------------------------------------
 
-            File pomPropertiesFile = archiveConfiguration.getPomPropertiesFile();
-            if ( pomPropertiesFile == null )
-            {
-                File dir = new File( workingProject.getBuild().getDirectory(), "maven-archiver" );
-                pomPropertiesFile = new File( dir, "pom.properties" );
-            }
-            new PomPropertiesUtil().createPomProperties( session, workingProject, archiver, pomPropertiesFile, forced );
+            File customPomPropertiesFile = archiveConfiguration.getPomPropertiesFile();
+            File dir = new File( workingProject.getBuild().getDirectory(), "maven-archiver" );
+            File pomPropertiesFile = new File( dir, "pom.properties" );
+
+            new PomPropertiesUtil().createPomProperties( session, workingProject, archiver,
+                customPomPropertiesFile, pomPropertiesFile, forced );
         }
 
         // ----------------------------------------------------------------------

Modified: maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java?rev=1726860&r1=1726859&r2=1726860&view=diff
==============================================================================
--- maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java (original)
+++ maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java Tue Jan 26 19:47:35 2016
@@ -41,13 +41,9 @@ public class PomPropertiesUtil
 {
     private static final String CREATED_BY_MAVEN = "Created by Apache Maven";
 
-    private boolean sameContents( Properties props, File file )
+    private Properties loadPropertiesFile( File file )
         throws IOException
     {
-        if ( !file.isFile() )
-        {
-            return false;
-        }
         Properties fileProps = new Properties();
         InputStream istream = null;
         try
@@ -56,11 +52,7 @@ public class PomPropertiesUtil
             fileProps.load( istream );
             istream.close();
             istream = null;
-            return fileProps.equals( props );
-        }
-        catch ( IOException e )
-        {
-            return false;
+            return fileProps;
         }
         finally
         {
@@ -68,6 +60,18 @@ public class PomPropertiesUtil
         }
     }
 
+    private boolean sameContents( Properties props, File file )
+        throws IOException
+    {
+        if ( !file.isFile() )
+        {
+            return false;
+        }
+
+        Properties fileProps = loadPropertiesFile( file );
+        return fileProps.equals( props );
+    }
+
     private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,
                                        boolean forceCreation )
         throws IOException
@@ -106,28 +110,39 @@ public class PomPropertiesUtil
 
     /**
      * Creates the pom.properties file.
-     * @param session TODO
+     * @param session {@link MavenSession}
      * @param project {@link MavenProject}
      * @param archiver {@link Archiver}
+     * @param customPomPropertiesFile optional custom pom properties file
      * @param pomPropertiesFile The pom properties file.
-     * @param forceCreation force creation true/flas.e
+     * @param forceCreation force creation true/false
      * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
      * @throws IOException IO exception.
      */
     public void createPomProperties( MavenSession session, MavenProject project, Archiver archiver,
-                                     File pomPropertiesFile, boolean forceCreation )
+                                     File customPomPropertiesFile, File pomPropertiesFile, boolean forceCreation )
         throws IOException
     {
-        final String artifactId = project.getArtifactId();
         final String groupId = project.getGroupId();
+        final String artifactId = project.getArtifactId();
+        final String version = project.getVersion();
+
+        Properties p;
 
-        Properties p = new Properties();
+        if ( customPomPropertiesFile != null )
+        {
+            p = loadPropertiesFile( customPomPropertiesFile );
+        }
+        else
+        {
+            p = new Properties();
+        }
 
-        p.setProperty( "groupId", project.getGroupId() );
+        p.setProperty( "groupId", groupId );
 
-        p.setProperty( "artifactId", project.getArtifactId() );
+        p.setProperty( "artifactId", artifactId );
 
-        p.setProperty( "version", project.getVersion() );
+        p.setProperty( "version", version );
 
         createPropertiesFile( session, p, pomPropertiesFile, forceCreation );
 

Modified: maven/shared/trunk/maven-archiver/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-archiver/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java?rev=1726860&r1=1726859&r2=1726860&view=diff
==============================================================================
--- maven/shared/trunk/maven-archiver/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java (original)
+++ maven/shared/trunk/maven-archiver/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java Tue Jan 26 19:47:35 2016
@@ -2,6 +2,7 @@ package org.apache.maven.archiver;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
 import java.util.ArrayList;
@@ -17,6 +18,9 @@ import java.util.TreeSet;
 import java.util.jar.Attributes;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+
+import junit.framework.TestCase;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
@@ -34,12 +38,12 @@ import org.apache.maven.model.Organizati
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.shared.utils.StringUtils;
 import org.apache.maven.shared.utils.io.FileUtils;
+import org.apache.maven.shared.utils.io.IOUtil;
 import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 import org.codehaus.plexus.archiver.jar.ManifestException;
 import org.sonatype.aether.RepositorySystemSession;
 import org.sonatype.aether.util.DefaultRepositorySystemSession;
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -59,8 +63,6 @@ import org.sonatype.aether.util.DefaultR
  * under the License.
  */
 
-import junit.framework.TestCase;
-
 public class MavenArchiverTest
     extends TestCase
 {
@@ -819,6 +821,76 @@ public class MavenArchiverTest
         assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
     }
 
+    public void testDefaultPomProperties()
+            throws Exception
+    {
+        MavenSession session = getDummySession();
+        MavenProject project = getDummyProject();
+        File jarFile = new File( "target/test/dummy.jar" );
+        JarArchiver jarArchiver = getCleanJarArchiver( jarFile );
+
+        MavenArchiver archiver = getMavenArchiver( jarArchiver );
+
+        MavenArchiveConfiguration config = new MavenArchiveConfiguration();
+        config.setForced( true );
+        archiver.createArchive( session, project, config );
+        assertTrue( jarFile.exists() );
+
+        final String groupId = project.getGroupId();
+        final String artifactId = project.getArtifactId();
+        final String version = project.getVersion();
+
+        JarFile virtJarFile = new JarFile( jarFile );
+        ZipEntry pomPropertiesEntry = virtJarFile.getEntry( "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
+        assertNotNull( pomPropertiesEntry );
+
+        InputStream is = virtJarFile.getInputStream( pomPropertiesEntry );
+        Properties p = loadProperties( is );
+
+        assertEquals( groupId, p.getProperty( "groupId" ) );
+        assertEquals( artifactId, p.getProperty( "artifactId" ) );
+        assertEquals( version, p.getProperty( "version" ) );
+
+        virtJarFile.close();
+    }
+
+    public void testCustomPomProperties()
+            throws Exception
+    {
+        MavenSession session = getDummySession();
+        MavenProject project = getDummyProject();
+        File jarFile = new File( "target/test/dummy.jar" );
+        JarArchiver jarArchiver = getCleanJarArchiver( jarFile );
+
+        MavenArchiver archiver = getMavenArchiver( jarArchiver );
+
+        File customPomPropertiesFile = new File( "src/test/resources/custom-pom.properties" );
+        MavenArchiveConfiguration config = new MavenArchiveConfiguration();
+        config.setForced( true );
+        config.setPomPropertiesFile( customPomPropertiesFile );
+        archiver.createArchive( session, project, config );
+        assertTrue( jarFile.exists() );
+
+        final String groupId = project.getGroupId();
+        final String artifactId = project.getArtifactId();
+        final String version = project.getVersion();
+
+        JarFile virtJarFile = new JarFile( jarFile );
+        ZipEntry pomPropertiesEntry = virtJarFile.getEntry( "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
+        assertNotNull( pomPropertiesEntry );
+
+        InputStream is = virtJarFile.getInputStream( pomPropertiesEntry );
+        Properties p = loadProperties( is );
+
+        assertEquals( groupId, p.getProperty( "groupId" ) );
+        assertEquals( artifactId, p.getProperty( "artifactId" ) );
+        assertEquals( version, p.getProperty( "version" ) );
+        assertEquals( "1337", p.getProperty("build.revision" ) );
+        assertEquals( "tags/0.1.1", p.getProperty("build.branch" ) );
+
+        virtJarFile.close();
+    }
+
     private JarArchiver getCleanJarArchiver( File jarFile )
     {
         deleteAndAssertNotPresent( jarFile );
@@ -1070,6 +1142,23 @@ public class MavenArchiverTest
         return result;
     }
 
+    private Properties loadProperties( InputStream is )
+        throws IOException
+    {
+        Properties p = new Properties();
+        try
+        {
+            p.load( is );
+            is.close();
+            is = null;
+            return p;
+        }
+        finally
+        {
+            IOUtil.close( is );
+        }
+    }
+
     public Manifest getJarFileManifest( File jarFile )
         throws IOException
     {

Added: maven/shared/trunk/maven-archiver/src/test/resources/custom-pom.properties
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-archiver/src/test/resources/custom-pom.properties?rev=1726860&view=auto
==============================================================================
--- maven/shared/trunk/maven-archiver/src/test/resources/custom-pom.properties (added)
+++ maven/shared/trunk/maven-archiver/src/test/resources/custom-pom.properties Tue Jan 26 19:47:35 2016
@@ -0,0 +1,22 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+groupId = fake
+artifactId = bogus
+version = -1
+build.revision = 1337
+build.branch = tags/0.1.1