From e4f6622846b18c32304e1450afde15ae8df81f23 Mon Sep 17 00:00:00 2001 From: brett Date: Wed, 1 Jun 2005 05:04:14 +0000 Subject: [PATCH] PR: MPARTIFACT-19 Automatically deploy as a snapshot when the version contains SNAPSHOT git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@179356 13f79535-47bb-0310-9956-ffa450edef68 --- .../deployer/DefaultArtifactDeployer.java | 177 +++++++----------- .../artifact/deployer/RepositoryBuilder.java | 10 + 2 files changed, 76 insertions(+), 111 deletions(-) diff --git a/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java b/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java index 16934910..0f7bc06d 100644 --- a/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java +++ b/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java @@ -44,6 +44,7 @@ import org.codehaus.plexus.util.FileUtils; import java.io.File; import java.net.MalformedURLException; +import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -52,7 +53,6 @@ import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.TimeZone; -import java.security.NoSuchAlgorithmException; /** * Default implementation of Artifact Deployer interface. @@ -66,42 +66,12 @@ public class DefaultArtifactDeployer private static final String POM_TYPE = "pom"; - /** - * Indicate if POM of given artifact should be also deployed - * to remote repository - */ - public static final boolean DEPLOY_POM = true; - - /** - * Indicate if POM of given artifact should be also deployed - * in local repository - */ - public static final boolean INSTALL_POM = true; - - /** - * Indicate if snapshot version of - * POM of given artifact should be also deployed - * to remote repository - */ - public static final boolean DEPLOY_POM_SNAPSHOT = true; - - /** - * Indicate if POM of given artifact should be also installed - * in local repository - */ - public static final boolean INSTALL_POM_SNAPSHOT = true; - private static final ArtifactTypeHandler POM_ARTIFACT_TYPE_HANDLER = new DefaultArtifactTypeHandler(); /** * Date/time stamp which is appended to snapshot filenames */ - public final static DateFormat SNAPSHOT_SIGNATURE_FMT = new SimpleDateFormat( "yyyyMMdd.HHmmss" ); - - static - { - SNAPSHOT_SIGNATURE_FMT.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); - } + private final static String SNAPSHOT_FORMAT = "yyyyMMdd.HHmmss"; private static final Log LOG = LogFactory.getLog( DefaultArtifactDeployer.class ); @@ -111,24 +81,7 @@ public class DefaultArtifactDeployer public void deploy( String artifact, String type, Project project, ArtifactTypeHandler handler ) throws MavenException { - File file; - if ( POM_TYPE.equals( type ) ) - { - file = project.getFile(); - } - else - { - file = getFileForArtifact( artifact ); - } - - String repositoryPath = handler.constructRepositoryFullPath( type, project, project.getCurrentVersion() ); - - //do not deploy POM twice - if ( DEPLOY_POM && !POM_TYPE.equals( type ) ) - { - deploy( artifact, POM_TYPE, project, POM_ARTIFACT_TYPE_HANDLER ); - } - doDeploy( Collections.singletonList( file ), Collections.singletonList( repositoryPath ), project ); + handleDeploy( type, project, artifact, handler, project.getCurrentVersion() ); } /** @@ -137,8 +90,13 @@ public class DefaultArtifactDeployer public void deploySnapshot( String artifact, String type, Project project, ArtifactTypeHandler handler ) throws MavenException { + handleDeploy( type, project, artifact, handler, MavenConstants.SNAPSHOT_SIGNIFIER ); + } - String snapshotSignature = getSnapshotSignature(); + private void handleDeploy( String type, Project project, String artifact, ArtifactTypeHandler handler, + String version ) + throws MavenException + { File file; if ( POM_TYPE.equals( type ) ) { @@ -148,39 +106,14 @@ public class DefaultArtifactDeployer { file = getFileForArtifact( artifact ); } - File snapshotVersionFile = createSnapshotVersionFile( file, snapshotSignature, project, type ); - try + + // do not deploy POM twice + if ( !POM_TYPE.equals( type ) ) { - List srcFiles = new ArrayList(); - srcFiles.add( file ); - srcFiles.add( file ); - - String snapshotFilename = handler.constructRepositoryFullPath( type, project, - MavenConstants.SNAPSHOT_SIGNIFIER ); - String timestampedFilename = handler.constructRepositoryFullPath( type, project, snapshotSignature ); - - List destFiles = new ArrayList(); - destFiles.add( snapshotFilename ); - destFiles.add( timestampedFilename ); - - srcFiles.add( snapshotVersionFile ); - - String snapshotVersionsFilename = handler.constructRepositoryDirectoryPath( type, project ) + - project.getArtifactId() + "-snapshot-version"; - destFiles.add( snapshotVersionsFilename ); - - // do not deploy POM twice - if ( DEPLOY_POM_SNAPSHOT && !POM_TYPE.equals( type ) ) - { - deploySnapshot( artifact, POM_TYPE, project, POM_ARTIFACT_TYPE_HANDLER ); - } - - doDeploy( srcFiles, destFiles, project ); - } - finally - { - snapshotVersionFile.delete(); + doDeploy( project.getFile(), project, POM_ARTIFACT_TYPE_HANDLER, version, POM_TYPE ); } + + doDeploy( file, project, handler, version, type ); } /** @@ -189,21 +122,7 @@ public class DefaultArtifactDeployer public void install( String artifact, String type, Project project, ArtifactTypeHandler handler ) throws MavenException { - File file; - if ( POM_TYPE.equals( type ) ) - { - file = project.getFile(); - } - else - { - file = getFileForArtifact( artifact ); - } - doInstall( file, type, project, project.getCurrentVersion(), handler ); - // do not install twice - if ( INSTALL_POM && !POM_TYPE.equals( type ) ) - { - doInstall( project.getFile(), POM_TYPE, project, project.getCurrentVersion(), POM_ARTIFACT_TYPE_HANDLER ); - } + handleInstall( type, project, artifact, handler, project.getCurrentVersion() ); } /** @@ -211,6 +130,13 @@ public class DefaultArtifactDeployer */ public void installSnapshot( String artifact, String type, Project project, ArtifactTypeHandler handler ) throws MavenException + { + handleInstall( type, project, artifact, handler, MavenConstants.SNAPSHOT_SIGNIFIER ); + } + + private void handleInstall( String type, Project project, String artifact, ArtifactTypeHandler handler, + String version ) + throws MavenException { File file; if ( POM_TYPE.equals( type ) ) @@ -221,23 +147,21 @@ public class DefaultArtifactDeployer { file = getFileForArtifact( artifact ); } - String snapshotSignature = getSnapshotSignature(); - LOG.info( "Installing snapshot of:'" + artifact + "''" ); - doInstall( file, type, project, MavenConstants.SNAPSHOT_SIGNIFIER, handler ); - doInstall( file, type, project, snapshotSignature, handler ); - if ( INSTALL_POM_SNAPSHOT && !POM_TYPE.equals( type ) ) + + doInstall( file, type, project, version, handler ); + + // do not install twice + if ( !POM_TYPE.equals( type ) ) { - File projectFile = project.getFile(); - doInstall( projectFile, POM_TYPE, project, MavenConstants.SNAPSHOT_SIGNIFIER, POM_ARTIFACT_TYPE_HANDLER ); - doInstall( projectFile, POM_TYPE, project, snapshotSignature, POM_ARTIFACT_TYPE_HANDLER ); + doInstall( project.getFile(), POM_TYPE, project, version, POM_ARTIFACT_TYPE_HANDLER ); } } /** * Install given file in local repository * - * @param file the artifact file to install - * @param type The type of the artiafct + * @param file the artifact file to install + * @param type The type of the artiafct * @param project * @param version String denominating the version of the artifact * @throws MavenException @@ -245,6 +169,13 @@ public class DefaultArtifactDeployer private void doInstall( File file, String type, Project project, String version, ArtifactTypeHandler handler ) throws MavenException { + if ( version.indexOf( MavenConstants.SNAPSHOT_SIGNIFIER ) >= 0 ) + { + String snapshotSignature = getSnapshotSignature(); + String v = StringUtils.replace( version, MavenConstants.SNAPSHOT_SIGNIFIER, snapshotSignature ); + doInstall( file, type, project, v, handler ); + } + try { Repository repository = new Repository( "local", "file:" + project.getContext().getMavenRepoLocal() ); @@ -306,9 +237,32 @@ public class DefaultArtifactDeployer return null; } - private void doDeploy( List srcFiles, List destFiles, Project project ) + private void doDeploy( File file, Project project, ArtifactTypeHandler handler, String version, String type ) throws MavenException { + List srcFiles = new ArrayList( 3 ); + List destFiles = new ArrayList( 3 ); + + srcFiles.add( file ); + destFiles.add( handler.constructRepositoryFullPath( type, project, version ) ); + + if ( version.indexOf( MavenConstants.SNAPSHOT_SIGNIFIER ) >= 0 ) + { + String snapshotSignature = getSnapshotSignature(); + String v = StringUtils.replace( version, MavenConstants.SNAPSHOT_SIGNIFIER, snapshotSignature ); + + File snapshotVersionFile = createSnapshotVersionFile( file, v, project, type ); + + String snapshotVersionsFilename = handler.constructRepositoryDirectoryPath( type, project ) + + project.getArtifactId() + "-snapshot-version"; + + srcFiles.add( snapshotVersionFile ); + destFiles.add( snapshotVersionsFilename ); + + srcFiles.add( file ); + destFiles.add( handler.constructRepositoryFullPath( type, project, v ) ); + } + // trick add special values to context for default repository; String repoStr = (String) project.getContext().getVariable( "maven.repo.list" ); @@ -461,8 +415,9 @@ public class DefaultArtifactDeployer */ private String getSnapshotSignature() { - Date now = new Date(); - return SNAPSHOT_SIGNATURE_FMT.format( now ); + DateFormat fmt = new SimpleDateFormat( SNAPSHOT_FORMAT ); + fmt.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); + return fmt.format( new Date() ); } /** @@ -508,7 +463,7 @@ public class DefaultArtifactDeployer { file = new File( artifact.getParent(), filename ); FileUtils.fileWrite( file.getAbsolutePath(), snapshotVersion ); - + file.deleteOnExit(); } catch ( Exception e ) { diff --git a/artifact/src/main/org/apache/maven/artifact/deployer/RepositoryBuilder.java b/artifact/src/main/org/apache/maven/artifact/deployer/RepositoryBuilder.java index 6748c1d9..b27012c8 100755 --- a/artifact/src/main/org/apache/maven/artifact/deployer/RepositoryBuilder.java +++ b/artifact/src/main/org/apache/maven/artifact/deployer/RepositoryBuilder.java @@ -23,6 +23,7 @@ import org.apache.maven.wagon.repository.RepositoryPermissions; import org.apache.maven.wagon.authentication.AuthenticationInfo; import org.apache.maven.wagon.proxy.ProxyInfo; import org.apache.maven.MavenConstants; +import org.apache.maven.MavenException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -39,13 +40,22 @@ public class RepositoryBuilder private static final Log LOG = LogFactory.getLog( RepositoryBuilder.class ); public static Repository getRepository( Project project, String id ) + throws MavenException { String url = (String) project.getContext().getVariable( "maven.repo." + id ); + if ( url == null ) + { + throw new MavenException( "URL not specified in property: maven.repo." + id ); + } Repository repository = new Repository( id, url ); String dir = (String) project.getContext().getVariable( "maven.repo." + id + ".directory" ); repository.setBasedir( dir ); + if ( dir == null ) + { + throw new MavenException( "Directory not specified in property: maven.repo." + id + ".directory" ); + } String port = (String) project.getContext().getVariable( "maven.repo." + id + ".port" ); if ( port != null )