
Eigenschafts„nderungen: .
___________________________________________________________________
Name: svn:ignore
   - .wtpmodules
*~
*.log
target
dist
*.ipr
*.iws
*.iml
dist
target
.classpath
.project
build.xml

   + .wtpmodules
*~
*.log
target
dist
*.ipr
*.iws
*.iml
dist
target
.classpath
.project
build.xml
.settings


Index: src/test/java/org/apache/maven/archiver/MavenArchiverTest.java
===================================================================
--- src/test/java/org/apache/maven/archiver/MavenArchiverTest.java	(Revision 521402)
+++ src/test/java/org/apache/maven/archiver/MavenArchiverTest.java	(Arbeitskopie)
@@ -19,9 +19,12 @@
 import junit.framework.TestCase;
 
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.model.Build;
 import org.apache.maven.model.Model;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.archiver.jar.JarArchiver;
 import org.codehaus.plexus.archiver.jar.Manifest;
+import org.codehaus.plexus.util.FileUtils;
 
 import java.io.File;
 import java.util.Collections;
@@ -174,4 +177,59 @@
 
     }
 
+    public void testRecreation()
+            throws Exception
+    {
+        File jarFile = new File( "target/test/dummy.jar" );
+        JarArchiver jarArchiver = new JarArchiver();
+        jarArchiver.setDestFile( jarFile );
+
+        MavenArchiver archiver = new MavenArchiver();
+        archiver.setArchiver( jarArchiver );
+        archiver.setOutputFile( jarArchiver.getDestFile() );
+
+        Model model = new Model();
+        model.setGroupId( "org.apache.dummy" );
+        model.setArtifactId( "dummy" );
+        model.setVersion( "0.1" );
+        MavenProject project = new MavenProject( model );
+ 
+        project.setArtifacts( Collections.EMPTY_SET );
+        project.setPluginArtifacts( Collections.EMPTY_SET );
+        project.setReportArtifacts( Collections.EMPTY_SET );
+        project.setExtensionArtifacts( Collections.EMPTY_SET );
+        project.setRemoteArtifactRepositories( Collections.EMPTY_LIST );
+        project.setPluginArtifactRepositories( Collections.EMPTY_LIST );
+        project.setFile( new File( "pom.xml" ) );
+        Build build = new Build();
+        build.setDirectory( "target" );
+        project.setBuild( build );
+
+        MockArtifact artifact = new MockArtifact();
+        artifact.setGroupId( "org.apache.dummy" );
+        artifact.setArtifactId( "dummy" );
+        artifact.setVersion( "0.1" );
+        artifact.setType( "jar" );
+        project.setArtifact( artifact );
+
+        MavenArchiveConfiguration config = new MavenArchiveConfiguration();
+        config.setForced( false );
+        
+        FileUtils.deleteDirectory( "target/maven-archiver" );
+        long timeStamp0 = System.currentTimeMillis();
+        Thread.sleep( 1 ); // Make sure, that System.currentTimeMillis() is different from timeStamp
+        archiver.createArchive( project, config );
+        long timeStamp1 = jarFile.lastModified();
+        assertTrue( timeStamp1 > timeStamp0 );
+
+        Thread.sleep( 1 ); // Make sure, that System.currentTimeMillis() is different from timeStamp
+        archiver.createArchive( project,config );
+        long timeStamp2 = jarFile.lastModified();
+        assertEquals( timeStamp2, timeStamp1 );
+
+        Thread.sleep( 1 ); // Make sure, that System.currentTimeMillis() is different from timeStamp
+        config.setForced( true );
+        archiver.createArchive( project, config );
+        assertTrue( jarFile.lastModified() > timeStamp2 );
+    }
 }
Index: src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java
===================================================================
--- src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java	(Revision 521402)
+++ src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java	(Arbeitskopie)
@@ -47,6 +47,8 @@
 
     private boolean forced = true;
 
+    private File pomPropertiesFile;
+
     public boolean isCompress()
     {
         return compress;
@@ -178,4 +180,24 @@
     {
     	this.forced = forced;
     }
+
+    /**
+     * Returns the location of the "pom.properties" file. 
+     * May be null, in which case a default value is choosen.
+     * @return "pom.properties" location or null.
+     */
+    public File getPomPropertiesFile()
+    {
+        return pomPropertiesFile;
+    }
+
+    /**
+     * Sets the location of the "pom.properties" file. 
+     * May be null, in which case a default value is choosen.
+     * @param pomPropertiesFile "pom.properties" location or null.
+     */
+    public void setPomPropertiesFile( File pPomPropertiesFile )
+    {
+        pomPropertiesFile = pPomPropertiesFile;
+    }
 }
Index: src/main/java/org/apache/maven/archiver/PomPropertiesManager.java
===================================================================
--- src/main/java/org/apache/maven/archiver/PomPropertiesManager.java	(Revision 0)
+++ src/main/java/org/apache/maven/archiver/PomPropertiesManager.java	(Revision 0)
@@ -0,0 +1,198 @@
+package org.apache.maven.archiver;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.util.Properties;
+
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.archiver.Archiver;
+import org.codehaus.plexus.archiver.ArchiverException;
+
+
+/**
+ * This class is responsible for creating the pom.properties
+ * file.
+ */
+public class PomPropertiesManager
+{
+    private static final String GENERATED_BY_MAVEN = "Generated by Maven";
+
+    private boolean isFirstLine( BufferedReader reader )
+            throws IOException
+    {
+        String line = reader.readLine();
+        return line != null  &&  ("#" + GENERATED_BY_MAVEN).equals( line );
+    }
+
+    private boolean isSecondLine( BufferedReader reader )
+            throws IOException
+    {
+        String line = reader.readLine();
+        return line != null  &&  line.startsWith( "#" );
+    }
+
+    private boolean sameContents( Reader reader1, Reader reader2 )
+            throws IOException
+    {
+        for ( ; ; )
+        {
+            int c1 = reader1.read();
+            int c2 = reader2.read();
+            if ( c1 != c2 )
+            {
+                return false;
+            }
+            if ( c1 == -1 )
+            {
+                return true;
+            }
+        }
+    }
+
+    private void closeSilent( Reader reader )
+    {
+        if ( reader != null )
+        {
+            try
+            {
+                reader.close();
+            }
+            catch ( Throwable t )
+            {
+                // Ignore me
+            }
+        }
+    }
+
+    private void closeSilent( OutputStream stream )
+    {
+        if ( stream != null )
+        {
+            try
+            {
+                stream.close();
+            }
+            catch ( Throwable t )
+            {
+                // Ignore me
+            }
+        }
+    }
+
+    /**
+     * This method compares two property files and verifies whether they do
+     * have the same content. The logic is based on the assumption that both
+     * files are created by {@link Properties#store()}. In other words, the
+     * assumption is that both files follow the scheme
+     * <pre>
+     *   # Generated by Maven
+     *   # <timestamp>
+     *   ...
+     * </pre>
+     * where the timestamp is an ignorable value.
+     */
+    private boolean sameContents( InputStream file1, InputStream file2 )
+            throws IOException
+    {
+        BufferedReader reader1 = null;
+        BufferedReader reader2 = null;
+        try {
+            reader1 = new BufferedReader( new InputStreamReader( file1, "8859_1" ) ); 
+            reader2 = new BufferedReader( new InputStreamReader( file2, "8859_1" ) );
+            boolean result = isFirstLine( reader1 )  &&  isFirstLine( reader2 )
+                &&  isSecondLine( reader1 )  &&  isSecondLine( reader2 )
+                &&  sameContents( reader1, reader2 );
+            reader1.close();
+            reader2.close();
+            reader1 = null;
+            reader2 = null;
+            return result;
+        } finally {
+            closeSilent( reader1 );
+            closeSilent( reader2 );
+        }
+    }
+
+    private void createPropertyFile( Properties properties, File outputFile,
+                                     boolean forceCreation )
+        throws IOException
+    {
+        File outputDir = outputFile.getParentFile();
+        if ( !outputDir.isDirectory()  &&  !outputDir.mkdirs() )
+        {
+            throw new IOException( "Failed to create directory: " + outputDir );
+        }
+        OutputStream os;
+        final ByteArrayOutputStream baos;
+        if ( forceCreation || !outputFile.isFile() )
+        {
+            os = new FileOutputStream( outputFile );
+            baos = null;
+        }
+        else
+        {
+            os = baos = new ByteArrayOutputStream();
+        }
+
+        try
+        {
+            properties.store( os, GENERATED_BY_MAVEN );
+            os.close(); // stream is flushed but not closed by Properties.store()
+            os = null;
+        }
+        finally
+        {
+            closeSilent( os );
+        }
+
+        if ( baos != null )
+        {
+            InputStream in = new FileInputStream( outputFile );
+            if ( !sameContents( in, new ByteArrayInputStream( baos.toByteArray() ) ) )
+            {
+                os = new FileOutputStream( outputFile );
+                try
+                {
+                    baos.writeTo( os );
+                    os.close();
+                    os = null;
+                }
+                finally
+                {
+                    closeSilent( os );
+                }
+            }
+        }
+    }
+
+    /**
+     * Creates the pom.properties file.
+     */
+    public void createPomProperties( MavenProject project, Archiver archiver, File pomPropertiesFile, boolean forceCreation )
+            throws ArchiverException, IOException
+    {
+        final String artifactId = project.getArtifactId();
+        final String groupId = project.getGroupId();
+
+        Properties p = new Properties();
+
+        p.setProperty( "groupId", project.getGroupId() );
+
+        p.setProperty( "artifactId", project.getArtifactId() );
+
+        p.setProperty( "version", project.getVersion() );
+
+        createPropertyFile( p, pomPropertiesFile, forceCreation );
+
+        archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
+    }
+}
Index: src/main/java/org/apache/maven/archiver/MavenArchiver.java
===================================================================
--- src/main/java/org/apache/maven/archiver/MavenArchiver.java	(Revision 521402)
+++ src/main/java/org/apache/maven/archiver/MavenArchiver.java	(Arbeitskopie)
@@ -25,14 +25,11 @@
 import org.codehaus.plexus.archiver.jar.ManifestException;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.Set;
 
 /**
@@ -283,8 +280,7 @@
         // without impacting the main project instance...
         MavenProject workingProject = new MavenProject( project );
 
-        File pomPropertiesFile = new File( workingProject.getFile().getParentFile(), "pom.properties" );
-
+        boolean forced = archiveConfiguration.isForced();
         if ( archiveConfiguration.isAddMavenDescriptor() )
         {
             // ----------------------------------------------------------------------
@@ -313,21 +309,13 @@
             // Create pom.properties file
             // ----------------------------------------------------------------------
 
-            Properties p = new Properties();
-
-            p.setProperty( "groupId", workingProject.getGroupId() );
-
-            p.setProperty( "artifactId", workingProject.getArtifactId() );
-
-            p.setProperty( "version", workingProject.getVersion() );
-
-            OutputStream os = new FileOutputStream( pomPropertiesFile );
-
-            p.store( os, "Generated by Maven" );
-
-            os.close(); // stream is flushed but not closed by Properties.store()
-
-            archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
+            File pomPropertiesFile = archiveConfiguration.getPomPropertiesFile();
+            if ( pomPropertiesFile == null )
+            {
+                File dir = new File( workingProject.getBuild().getDirectory(), "maven-archiver" );
+                pomPropertiesFile = new File( dir, "pom.properties" );
+            }
+            new PomPropertiesManager().createPomProperties( workingProject, archiver, pomPropertiesFile, forced );
         }
 
         // ----------------------------------------------------------------------
@@ -390,7 +378,6 @@
             }
         }
 
-        boolean forced = archiveConfiguration.isForced();
         archiver.setForced( forced );
         if ( !archiveConfiguration.isForced()  &&  archiver.isSupportingForced() )
         {
@@ -400,11 +387,5 @@
 
         // create archive
         archiver.createArchive();
-
-        // Cleanup
-        if ( archiveConfiguration.isAddMavenDescriptor() )
-        {
-            pomPropertiesFile.delete();
-        }
     }
 }
