You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ca...@apache.org on 2007/05/15 22:22:51 UTC
svn commit: r538309 - in /maven/shared/trunk/maven-archiver/src:
main/java/org/apache/maven/archiver/ test/java/org/apache/maven/archiver/
Author: carlos
Date: Tue May 15 13:22:50 2007
New Revision: 538309
URL: http://svn.apache.org/viewvc?view=rev&rev=538309
Log:
[MNG-2854] Recreating pom.properties always breaks the archivers uptodate check
Submitted By: Jochen Wiedmann
Added:
maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java (with props)
Modified:
maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java
maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.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/MavenArchiveConfiguration.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java?view=diff&rev=538309&r1=538308&r2=538309
==============================================================================
--- maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java (original)
+++ maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java Tue May 15 13:22:50 2007
@@ -50,6 +50,8 @@
private boolean forced = true;
+ private File pomPropertiesFile;
+
public boolean isCompress()
{
return compress;
@@ -180,5 +182,25 @@
public void setForced( boolean forced )
{
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 pomPropertiesFile )
+ {
+ this.pomPropertiesFile = pomPropertiesFile;
}
}
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?view=diff&rev=538309&r1=538308&r2=538309
==============================================================================
--- 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 May 15 13:22:50 2007
@@ -28,14 +28,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;
/**
@@ -286,8 +283,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() )
{
// ----------------------------------------------------------------------
@@ -316,21 +312,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 PomPropertiesUtil().createPomProperties( workingProject, archiver, pomPropertiesFile, forced );
}
// ----------------------------------------------------------------------
@@ -393,7 +381,6 @@
}
}
- boolean forced = archiveConfiguration.isForced();
archiver.setForced( forced );
if ( !archiveConfiguration.isForced() && archiver.isSupportingForced() )
{
@@ -403,11 +390,5 @@
// create archive
archiver.createArchive();
-
- // Cleanup
- if ( archiveConfiguration.isAddMavenDescriptor() )
- {
- pomPropertiesFile.delete();
- }
}
}
Added: 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?view=auto&rev=538309
==============================================================================
--- maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java (added)
+++ maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java Tue May 15 13:22:50 2007
@@ -0,0 +1,117 @@
+package org.apache.maven.archiver;
+
+/*
+ * 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.
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Properties;
+
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.archiver.Archiver;
+import org.codehaus.plexus.archiver.ArchiverException;
+import org.codehaus.plexus.util.IOUtil;
+
+
+/**
+ * This class is responsible for creating the pom.properties
+ * file.
+ */
+public class PomPropertiesUtil
+{
+ private static final String GENERATED_BY_MAVEN = "Generated by Maven";
+
+ private boolean sameContents( Properties props, File file )
+ throws IOException
+ {
+ if ( !file.isFile() )
+ {
+ return false;
+ }
+ Properties fileProps = new Properties();
+ InputStream istream = null;
+ try {
+ istream = new FileInputStream( file );
+ fileProps.load( istream );
+ istream.close();
+ istream = null;
+ return fileProps.equals( props );
+ }
+ catch ( IOException e )
+ {
+ return false;
+ }
+ finally
+ {
+ IOUtil.close( istream );
+ }
+ }
+
+ private void createPropertyFile( Properties properties, File outputFile,
+ boolean forceCreation )
+ throws IOException
+ {
+ File outputDir = outputFile.getParentFile();
+ if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
+ {
+ throw new IOException( "Failed to create directory: " + outputDir );
+ }
+ if ( !forceCreation && sameContents( properties, outputFile ) )
+ {
+ return;
+ }
+ OutputStream os = new FileOutputStream( outputFile );
+ try
+ {
+ properties.store( os, GENERATED_BY_MAVEN );
+ os.close(); // stream is flushed but not closed by Properties.store()
+ os = null;
+ }
+ finally
+ {
+ IOUtil.close( 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" );
+ }
+}
Propchange: maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/shared/trunk/maven-archiver/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
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?view=diff&rev=538309&r1=538308&r2=538309
==============================================================================
--- 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 May 15 13:22:50 2007
@@ -22,9 +22,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;
@@ -177,4 +180,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 );
+ }
}