From 3be18bef2e7534ac61f2353903185d5d8f1c9b73 Mon Sep 17 00:00:00 2001 From: brett Date: Fri, 18 Feb 2005 01:42:36 +0000 Subject: [PATCH] use checksum observer for MD5 git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@154226 13f79535-47bb-0310-9956-ffa450edef68 --- .../deployer/DefaultArtifactDeployer.java | 422 +++++++----------- .../maven/artifact/deployer/UploadMeter.java | 6 - 2 files changed, 172 insertions(+), 256 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 38675d41..4c75fd76 100644 --- a/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java +++ b/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java @@ -25,13 +25,13 @@ import org.apache.maven.MavenException; import org.apache.maven.project.Project; import org.apache.maven.repository.ArtifactTypeHandler; import org.apache.maven.repository.DefaultArtifactTypeHandler; -import org.apache.maven.util.MD5Sum; import org.apache.maven.wagon.ConnectionException; import org.apache.maven.wagon.ResourceDoesNotExistException; import org.apache.maven.wagon.TransferFailedException; import org.apache.maven.wagon.Wagon; import org.apache.maven.wagon.authentication.AuthenticationException; import org.apache.maven.wagon.authorization.AuthorizationException; +import org.apache.maven.wagon.observers.ChecksumObserver; import org.apache.maven.wagon.providers.file.FileWagon; import org.apache.maven.wagon.providers.ftp.FtpWagon; import org.apache.maven.wagon.providers.http.HttpWagon; @@ -45,160 +45,137 @@ import java.net.MalformedURLException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.TimeZone; /** - * * Default implementation of Artifact Deployer interface. - * - * - * @author Michal Maczka + * + * @author Michal Maczka * @version $Id$ */ -public class DefaultArtifactDeployer implements ArtifactDeployer +public class DefaultArtifactDeployer + implements ArtifactDeployer { private static final String POM_TYPE = "pom"; - /** - * Indicate if POM of given artifact should be also deployed - * to remote repository*/ + /** + * 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*/ + /** + * 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*/ + /** + * 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*/ + /** + * 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 + * Date/time stamp which is appended to snapshot filenames */ - public final static DateFormat SNAPSHOT_SIGNATURE_FMT = - new SimpleDateFormat("yyyyMMdd.HHmmss"); + public final static DateFormat SNAPSHOT_SIGNATURE_FMT = new SimpleDateFormat( "yyyyMMdd.HHmmss" ); - static { - SNAPSHOT_SIGNATURE_FMT.setTimeZone(TimeZone.getTimeZone("GMT")); + static + { + SNAPSHOT_SIGNATURE_FMT.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); } - private static final Log LOG = LogFactory.getLog(DefaultArtifactDeployer.class); + private static final Log LOG = LogFactory.getLog( DefaultArtifactDeployer.class ); /** * @see ArtifactDeployer#deploy(String, String, Project, ArtifactTypeHandler) */ - public void deploy(String artifact, String type, Project project, ArtifactTypeHandler handler) + public void deploy( String artifact, String type, Project project, ArtifactTypeHandler handler ) throws MavenException { File file; - if(POM_TYPE.equals(type)) + if ( POM_TYPE.equals( type ) ) { file = project.getFile(); } else { - file = getFileForArtifact(artifact); + file = getFileForArtifact( artifact ); } - File md5File = createMD5Checksum(file); - try { - String repositoryPath = - handler.constructRepositoryFullPath(type, project, project.getCurrentVersion()); - List srcFiles = new ArrayList(); - srcFiles.add(file); - srcFiles.add(md5File); - - List destFiles = new ArrayList(); - destFiles.add(repositoryPath); - destFiles.add(repositoryPath + ".md5"); + 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(srcFiles, destFiles, project); - } - finally { - //Delete md5 file - md5File.delete(); + //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 ); } /** * @see DefaultArtifactDeployer#deploySnapshot(String, String, Project, ArtifactTypeHandler) */ - public void deploySnapshot(String artifact, String type, Project project, ArtifactTypeHandler handler) + public void deploySnapshot( String artifact, String type, Project project, ArtifactTypeHandler handler ) throws MavenException { String snapshotSignature = getSnapshotSignature(); File file; - if(POM_TYPE.equals(type)) + if ( POM_TYPE.equals( type ) ) { - file = project.getFile(); + file = project.getFile(); } else { - file = getFileForArtifact(artifact); + file = getFileForArtifact( artifact ); } - File md5File = createMD5Checksum(file); - File snapshotVersionFile = - createSnapshotVersionFile(file, snapshotSignature, project, type); - try { + File snapshotVersionFile = createSnapshotVersionFile( file, snapshotSignature, project, type ); + try + { List srcFiles = new ArrayList(); - srcFiles.add(file); - srcFiles.add(md5File); - srcFiles.add(file); - srcFiles.add(md5File); + srcFiles.add( file ); + srcFiles.add( file ); - String snapshotFilename = - handler.constructRepositoryFullPath( - type, - project, - MavenConstants.SNAPSHOT_SIGNIFIER); - String timestampedFilename = - handler.constructRepositoryFullPath(type, project, snapshotSignature); + 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(snapshotFilename + ".md5"); - destFiles.add(timestampedFilename); - destFiles.add(timestampedFilename + ".md5"); + destFiles.add( snapshotFilename ); + destFiles.add( timestampedFilename ); - srcFiles.add(snapshotVersionFile); + srcFiles.add( snapshotVersionFile ); - String snapshotVersionsFilename = - handler.constructRepositoryDirectoryPath(type, project) - + project.getArtifactId() - + "-snapshot-version"; - destFiles.add(snapshotVersionsFilename); + 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)) + if ( DEPLOY_POM_SNAPSHOT && !POM_TYPE.equals( type ) ) { - deploySnapshot(artifact, POM_TYPE, project, POM_ARTIFACT_TYPE_HANDLER); + deploySnapshot( artifact, POM_TYPE, project, POM_ARTIFACT_TYPE_HANDLER ); } - doDeploy(srcFiles, destFiles, project); + doDeploy( srcFiles, destFiles, project ); } - finally { - // Delete md5 file - md5File.delete(); + finally + { snapshotVersionFile.delete(); } } @@ -206,205 +183,196 @@ public class DefaultArtifactDeployer implements ArtifactDeployer /** * @see ArtifactDeployer#install(String, String, Project, ArtifactTypeHandler) */ - public void install(String artifact, String type, Project project, ArtifactTypeHandler handler) + public void install( String artifact, String type, Project project, ArtifactTypeHandler handler ) throws MavenException { File file; - if(POM_TYPE.equals(type)) + if ( POM_TYPE.equals( type ) ) { - file = project.getFile(); + file = project.getFile(); } else { - file = getFileForArtifact(artifact); + file = getFileForArtifact( artifact ); } - doInstall(file, type, project, project.getCurrentVersion(), handler); + doInstall( file, type, project, project.getCurrentVersion(), handler ); // do not install twice - if (INSTALL_POM && !POM_TYPE.equals(type)) + if ( INSTALL_POM && !POM_TYPE.equals( type ) ) { - doInstall( - project.getFile(), - POM_TYPE, - project, - project.getCurrentVersion(), - POM_ARTIFACT_TYPE_HANDLER); + doInstall( project.getFile(), POM_TYPE, project, project.getCurrentVersion(), POM_ARTIFACT_TYPE_HANDLER ); } } /** * @see ArtifactDeployer#installSnapshot(String, String, Project, ArtifactTypeHandler) */ - public void installSnapshot(String artifact, String type, Project project, ArtifactTypeHandler handler) + public void installSnapshot( String artifact, String type, Project project, ArtifactTypeHandler handler ) throws MavenException { File file; - if(POM_TYPE.equals(type)) + if ( POM_TYPE.equals( type ) ) { - file = project.getFile(); + file = project.getFile(); } else { - file = getFileForArtifact(artifact); + 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)) + 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 ) ) { 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( projectFile, POM_TYPE, project, MavenConstants.SNAPSHOT_SIGNIFIER, POM_ARTIFACT_TYPE_HANDLER ); + doInstall( projectFile, POM_TYPE, project, snapshotSignature, 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 project - * @param version String denominating the version of the artifact + * Install given file in local repository + * + * @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 */ - private void doInstall( - File file, - String type, - Project project, - String version, - ArtifactTypeHandler handler) + private void doInstall( File file, String type, Project project, String version, ArtifactTypeHandler handler ) throws MavenException { try { // TODO: swap for file wagon - File destFile = - new File( - getLocalRepository(project), - handler.constructRepositoryFullPath(type, project, version)); - if (!destFile.getParentFile().exists()) + File destFile = new File( getLocalRepository( project ), + handler.constructRepositoryFullPath( type, project, version ) ); + if ( !destFile.getParentFile().exists() ) { destFile.getParentFile().mkdirs(); } - LOG.info("Copying: from '" + file + "' to: '" + destFile + "'"); - FileUtils.copyFile(file, destFile); + LOG.info( "Copying: from '" + file + "' to: '" + destFile + "'" ); + FileUtils.copyFile( file, destFile ); } - catch (IOException e) + catch ( IOException e ) { - String msg = - "Cannot install file: '" - + file - + "'. Reason: " - + e.getMessage(); - throw new MavenException(msg, e); + String msg = "Cannot install file: '" + file + "'. Reason: " + e.getMessage(); + throw new MavenException( msg, e ); } } - private String findSshIdentity() { + private String findSshIdentity() + { String key = findSshIdentity( System.getProperty( "user.home" ) ); - if ( key != null ) { - return key; + if ( key != null ) + { + return key; } - if ( System.getProperty( "user.home" ).equals( System.getProperty( "user.home.env" ) ) == false ) { + if ( System.getProperty( "user.home" ).equals( System.getProperty( "user.home.env" ) ) == false ) + { key = findSshIdentity( System.getProperty( "user.home.env" ) ); - if ( key != null ) { - return key; + if ( key != null ) + { + return key; } } LOG.warn( "Unable to locate identity id_rsa, id_dsa or identity - set maven.repo.default.privatekey" ); return null; } - private String findSshIdentity( String home ) { - if ( home == null ) { + private String findSshIdentity( String home ) + { + if ( home == null ) + { return null; } File sshHome = new File( home, ".ssh" ); LOG.debug( "Looking for SSH keys in " + sshHome ); File key = new File( sshHome, "id_dsa" ); - if ( key.exists() ) { + if ( key.exists() ) + { LOG.debug( "found " + key ); return key.getAbsolutePath(); } key = new File( sshHome, "id_rsa" ); - if ( key.exists() ) { + if ( key.exists() ) + { LOG.debug( "found " + key ); return key.getAbsolutePath(); } key = new File( sshHome, "identity" ); - if ( key.exists() ) { + if ( key.exists() ) + { LOG.debug( "found " + key ); return key.getAbsolutePath(); } return null; } - private void doDeploy(List srcFiles, List destFiles, Project project) + private void doDeploy( List srcFiles, List destFiles, Project project ) throws MavenException { // trick add special values to context for default repository; - String repoStr = - (String) project.getContext().getVariable("maven.repo.list"); + String repoStr = (String) project.getContext().getVariable( "maven.repo.list" ); - if (repoStr == null || repoStr.trim().length() == 0) + if ( repoStr == null || repoStr.trim().length() == 0 ) { String central = project.getDistributionSite(); String centralDirectory = project.getDistributionDirectory(); - if (central == null || central.trim().length() == 0) { - central = (String) project.getContext().getVariable("maven.repo.central"); - centralDirectory = (String) project.getContext().getVariable("maven.repo.central.directory"); + if ( central == null || central.trim().length() == 0 ) + { + central = (String) project.getContext().getVariable( "maven.repo.central" ); + centralDirectory = (String) project.getContext().getVariable( "maven.repo.central.directory" ); } - if (central != null && central.trim().length() > 0) { + if ( central != null && central.trim().length() > 0 ) + { repoStr = "default"; - project.getContext().setVariable("maven.repo.default", "scp://" + central); - if ( project.getContext().getVariable("maven.repo.default.privatekey") == null ) { - project.getContext().setVariable("maven.repo.default.privatekey", findSshIdentity()); + project.getContext().setVariable( "maven.repo.default", "scp://" + central ); + if ( project.getContext().getVariable( "maven.repo.default.privatekey" ) == null ) + { + project.getContext().setVariable( "maven.repo.default.privatekey", findSshIdentity() ); } - if ( project.getContext().getVariable("maven.repo.default.passphrase") == null ) { + if ( project.getContext().getVariable( "maven.repo.default.passphrase" ) == null ) + { LOG.warn( "WARNING: assuming empty passphrase. Specify maven.repo.default.passphrase if needed" ); - project.getContext().setVariable("maven.repo.default.passphrase", ""); + project.getContext().setVariable( "maven.repo.default.passphrase", "" ); } - project.getContext().setVariable("maven.repo.default.directory", centralDirectory); - project.getContext().setVariable("maven.repo.default.username", project.getContext().getVariable("maven.username")); - project.getContext().setVariable("maven.repo.default.group", project.getContext().getVariable("maven.remote.group")); + project.getContext().setVariable( "maven.repo.default.directory", centralDirectory ); + project.getContext().setVariable( "maven.repo.default.username", + project.getContext().getVariable( "maven.username" ) ); + project.getContext().setVariable( "maven.repo.default.group", + project.getContext().getVariable( "maven.remote.group" ) ); } } - String[] repos = StringUtils.split(repoStr, ","); + String[] repos = StringUtils.split( repoStr, "," ); - LOG.info( "Will deploy to " + repos.length + " repository(ies): " + repoStr); + LOG.info( "Will deploy to " + repos.length + " repository(ies): " + repoStr ); boolean success = false; - for (int i = 0; i < repos.length; i++) + for ( int i = 0; i < repos.length; i++ ) { String repo = repos[i].trim(); - LOG.info("Deploying to repository: " + repo); - Repository repository = RepositoryBuilder.getRepository(project, repo); + LOG.info( "Deploying to repository: " + repo ); + Repository repository = RepositoryBuilder.getRepository( project, repo ); try { deployFiles( repository, srcFiles, destFiles ); success = true; } - catch (Exception e) + catch ( Exception e ) { - String msg = - "Failed to deploy to: " - + repository.getId() - + " Reason: " - + e; - LOG.warn(msg, e); + String msg = "Failed to deploy to: " + repository.getId() + " Reason: " + e; + LOG.warn( msg, e ); // deploy to next repository continue; } } - if ( !success ) { - throw new MavenException("Unable to deploy to any repositories"); + if ( !success ) + { + throw new MavenException( "Unable to deploy to any repositories" ); } } @@ -413,21 +381,22 @@ public class DefaultArtifactDeployer implements ArtifactDeployer AuthorizationException, MalformedURLException { - if (srcFiles.size() != destFiles.size()) + if ( srcFiles.size() != destFiles.size() ) { String msg = "Lengths of the lists should be the same"; - throw new IllegalArgumentException(msg); + throw new IllegalArgumentException( msg ); } Wagon wagon = getWagon( repository.getProtocol() ); - wagon.addTransferListener( new UploadMeter()); + wagon.addTransferListener( new UploadMeter() ); + wagon.addTransferListener( new ChecksumObserver() ); try { wagon.connect( repository ); Iterator srcIterator = srcFiles.iterator(); Iterator destIterator = destFiles.iterator(); - while (srcIterator.hasNext()) + while ( srcIterator.hasNext() ) { File srcFile = (File) srcIterator.next(); String destFile = (String) destIterator.next(); @@ -440,9 +409,9 @@ public class DefaultArtifactDeployer implements ArtifactDeployer { wagon.disconnect(); } - catch (Exception e) + catch ( Exception e ) { - LOG.error("Error cleaning up from the deployer", e); + LOG.error( "Error cleaning up from the deployer", e ); } } } @@ -475,121 +444,74 @@ public class DefaultArtifactDeployer implements ArtifactDeployer /** * Return the local repository path form given project + * * @param project * @return The path to local repoository in local file system */ - private String getLocalRepository(Project project) + private String getLocalRepository( Project project ) { return project.getContext().getMavenRepoLocal(); } /** - * * @return */ private String getSnapshotSignature() { Date now = new Date(); - return SNAPSHOT_SIGNATURE_FMT.format(now); + return SNAPSHOT_SIGNATURE_FMT.format( now ); } /** - * * @param artifact * @return */ - private File getFileForArtifact(String artifact) throws MavenException + private File getFileForArtifact( String artifact ) + throws MavenException { - File file = new File(artifact); - if (!file.exists()) + File file = new File( artifact ); + if ( !file.exists() ) { String msg = "Artifact file: '" + artifact + "' must exist"; - throw new MavenException(msg); + throw new MavenException( msg ); } - if (!file.canRead()) + if ( !file.canRead() ) { String msg = "Artifact file: '" + artifact + "' must be readable"; - throw new MavenException(msg); + throw new MavenException( msg ); } - if (file.isDirectory()) + if ( file.isDirectory() ) { - String msg = - "Artifact file: '" + artifact + "' must not be a directory"; - throw new MavenException(msg); + String msg = "Artifact file: '" + artifact + "' must not be a directory"; + throw new MavenException( msg ); } return file.getAbsoluteFile(); } /** * Create a file which contains timestamp of the latetst snapshot - * + * * @param snapshotVersion * @param project * @param type * @return */ - private File createSnapshotVersionFile( - File artifact, - String snapshotVersion, - Project project, - String type) + private File createSnapshotVersionFile( File artifact, String snapshotVersion, Project project, String type ) throws MavenException { File file = null; - String filename = - project.getArtifactId() + "-" + type + "-snapshot-version"; + String filename = project.getArtifactId() + "-" + type + "-snapshot-version"; try { - file = new File(artifact.getParent(), filename); - FileUtils.fileWrite(file.getAbsolutePath(), snapshotVersion); + file = new File( artifact.getParent(), filename ); + FileUtils.fileWrite( file.getAbsolutePath(), snapshotVersion ); } - catch (Exception e) + catch ( Exception e ) { - throw new MavenException( - "Cannot create snapshot-version file:" + file); + throw new MavenException( "Cannot create snapshot-version file:" + file ); } return file; } - /** - * Create MD5 checksum file for given file. File is created - * in the same folder - * @param file - * @return The File which contatins MD5 checksum - * @throws MavenException when opertaion failed - */ - private File createMD5Checksum(File file) throws MavenException - { - // TODO: use the Observer in Wagon instead - MD5Sum md5Sum = new MD5Sum(); - md5Sum.setFile(file); - try - { - md5Sum.execute(); - } - catch (Exception e) - { - - throw new MavenException( - "MD5 checksum error: " + e.getMessage(), - e); - } - String checksum = md5Sum.getChecksum(); - - File md5ChecksumFile = null; - try - { - md5ChecksumFile = new File(file.getAbsoluteFile() + ".md5"); - FileUtils.fileWrite(md5ChecksumFile.getAbsolutePath(), checksum); - } - catch (Exception e) - { - throw new MavenException( - "Cannot create md5 checksum file: " + md5ChecksumFile); - - } - return md5ChecksumFile; - } - } diff --git a/artifact/src/main/org/apache/maven/artifact/deployer/UploadMeter.java b/artifact/src/main/org/apache/maven/artifact/deployer/UploadMeter.java index 86f6300e..9fc0c727 100755 --- a/artifact/src/main/org/apache/maven/artifact/deployer/UploadMeter.java +++ b/artifact/src/main/org/apache/maven/artifact/deployer/UploadMeter.java @@ -61,9 +61,6 @@ public class UploadMeter implements TransferListener public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length ) { -// TODO: fix -// long total = transferEvent.getResource().getContentLength(); - complete += length; if (total > 0) @@ -79,9 +76,6 @@ public class UploadMeter implements TransferListener public void transferCompleted( TransferEvent transferEvent ) { -// TODO: fix -// long total = transferEvent.getResource().getContentLength(); - System.out.println(" (" + (total/1024) + "K)"); }