diff options
Diffstat (limited to 'framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform')
7 files changed, 734 insertions, 0 deletions
diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/AbstractVersionTransformation.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/AbstractVersionTransformation.java new file mode 100644 index 00000000..3a5c7c6a --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/AbstractVersionTransformation.java @@ -0,0 +1,135 @@ +package org.apache.maven.repository.legacy.resolver.transform; + +/* + * 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.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.DefaultRepositoryRequest; +import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException; +import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.repository.legacy.WagonManager; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.logging.AbstractLogEnabled; + +/** + * Describes a version transformation during artifact resolution. + * + * @author <a href="mailto:brett@apache.org">Brett Porter</a> + * @todo try and refactor to remove abstract methods - not particular happy about current design + */ +public abstract class AbstractVersionTransformation + extends AbstractLogEnabled + implements ArtifactTransformation +{ + @Requirement + protected RepositoryMetadataManager repositoryMetadataManager; + + @Requirement + protected WagonManager wagonManager; + + public void transformForResolve( Artifact artifact, List<ArtifactRepository> remoteRepositories, + ArtifactRepository localRepository ) + throws ArtifactResolutionException, ArtifactNotFoundException + { + RepositoryRequest request = new DefaultRepositoryRequest(); + request.setLocalRepository( localRepository ); + request.setRemoteRepositories( remoteRepositories ); + transformForResolve( artifact, request ); + } + + protected String resolveVersion( Artifact artifact, ArtifactRepository localRepository, + List<ArtifactRepository> remoteRepositories ) + throws RepositoryMetadataResolutionException + { + RepositoryRequest request = new DefaultRepositoryRequest(); + request.setLocalRepository( localRepository ); + request.setRemoteRepositories( remoteRepositories ); + return resolveVersion( artifact, request ); + } + + protected String resolveVersion( Artifact artifact, RepositoryRequest request ) + throws RepositoryMetadataResolutionException + { + RepositoryMetadata metadata; + // Don't use snapshot metadata for LATEST (which isSnapshot returns true for) + if ( !artifact.isSnapshot() || Artifact.LATEST_VERSION.equals( artifact.getBaseVersion() ) ) + { + metadata = new ArtifactRepositoryMetadata( artifact ); + } + else + { + metadata = new SnapshotArtifactRepositoryMetadata( artifact ); + } + + repositoryMetadataManager.resolve( metadata, request ); + + artifact.addMetadata( metadata ); + + Metadata repoMetadata = metadata.getMetadata(); + String version = null; + if ( repoMetadata != null && repoMetadata.getVersioning() != null ) + { + version = constructVersion( repoMetadata.getVersioning(), artifact.getBaseVersion() ); + } + + if ( version == null ) + { + // use the local copy, or if it doesn't exist - go to the remote repo for it + version = artifact.getBaseVersion(); + } + + // TODO: also do this logging for other metadata? + // TODO: figure out way to avoid duplicated message + if ( getLogger().isDebugEnabled() ) + { + if ( !version.equals( artifact.getBaseVersion() ) ) + { + String message = artifact.getArtifactId() + ": resolved to version " + version; + if ( artifact.getRepository() != null ) + { + message += " from repository " + artifact.getRepository().getId(); + } + else + { + message += " from local repository"; + } + getLogger().debug( message ); + } + else + { + // Locally installed file is newer, don't use the resolved version + getLogger().debug( artifact.getArtifactId() + ": using locally installed snapshot" ); + } + } + return version; + } + + protected abstract String constructVersion( Versioning versioning, String baseVersion ); +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ArtifactTransformation.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ArtifactTransformation.java new file mode 100644 index 00000000..42604d75 --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ArtifactTransformation.java @@ -0,0 +1,86 @@ +package org.apache.maven.repository.legacy.resolver.transform; + +/* + * 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.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.deployer.ArtifactDeploymentException; +import org.apache.maven.artifact.installer.ArtifactInstallationException; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; + +/** + * @author <a href="mailto:jason@maven.org">Jason van Zyl </a> + */ +public interface ArtifactTransformation +{ + String ROLE = ArtifactTransformation.class.getName(); + + /** + * Take in a artifact and return the transformed artifact for locating in the remote repository. If no + * transformation has occurred the original artifact is returned. + * + * @param artifact Artifact to be transformed. + * @param request the repositories to check + */ + void transformForResolve( Artifact artifact, RepositoryRequest request ) + throws ArtifactResolutionException, ArtifactNotFoundException; + + /** + * Take in a artifact and return the transformed artifact for locating in the remote repository. If no + * transformation has occurred the original artifact is returned. + * + * @param artifact Artifact to be transformed. + * @param remoteRepositories the repositories to check + * @param localRepository the local repository + */ + void transformForResolve( Artifact artifact, + List<ArtifactRepository> remoteRepositories, + ArtifactRepository localRepository ) + throws ArtifactResolutionException, ArtifactNotFoundException; + + /** + * Take in a artifact and return the transformed artifact for locating in the local repository. If no + * transformation has occurred the original artifact is returned. + * + * @param artifact Artifact to be transformed. + * @param localRepository the local repository it will be stored in + */ + void transformForInstall( Artifact artifact, + ArtifactRepository localRepository ) + throws ArtifactInstallationException; + + /** + * Take in a artifact and return the transformed artifact for distributing to remote repository. If no + * transformation has occurred the original artifact is returned. + * + * @param artifact Artifact to be transformed. + * @param remoteRepository the repository to deploy to + * @param localRepository the local repository + */ + void transformForDeployment( Artifact artifact, + ArtifactRepository remoteRepository, + ArtifactRepository localRepository ) + throws ArtifactDeploymentException; + +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ArtifactTransformationManager.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ArtifactTransformationManager.java new file mode 100644 index 00000000..f0ac9c89 --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ArtifactTransformationManager.java @@ -0,0 +1,82 @@ +package org.apache.maven.repository.legacy.resolver.transform; + +/* + * 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.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.deployer.ArtifactDeploymentException; +import org.apache.maven.artifact.installer.ArtifactInstallationException; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; + +/** Manages multiple ArtifactTransformation instances and applies them in succession. */ +public interface ArtifactTransformationManager +{ + String ROLE = ArtifactTransformationManager.class.getName(); + + /** + * Take in a artifact and return the transformed artifact for locating in the remote repository. If no + * transformation has occurred the original artifact is returned. + * + * @param artifact Artifact to be transformed. + * @param request the repositories to check + */ + void transformForResolve( Artifact artifact, RepositoryRequest request ) + throws ArtifactResolutionException, ArtifactNotFoundException; + + /** + * Take in a artifact and return the transformed artifact for locating in the remote repository. If no + * transformation has occurred the original artifact is returned. + * + * @param artifact Artifact to be transformed. + * @param remoteRepositories the repositories to check + * @param localRepository the local repository + */ + void transformForResolve( Artifact artifact, List<ArtifactRepository> remoteRepositories, + ArtifactRepository localRepository ) + throws ArtifactResolutionException, ArtifactNotFoundException; + + /** + * Take in a artifact and return the transformed artifact for locating in the local repository. If no + * transformation has occurred the original artifact is returned. + * + * @param artifact Artifact to be transformed. + * @param localRepository the local repository it will be stored in + */ + void transformForInstall( Artifact artifact, ArtifactRepository localRepository ) + throws ArtifactInstallationException; + + /** + * Take in a artifact and return the transformed artifact for distributing to a remote repository. If no + * transformation has occurred the original artifact is returned. + * + * @param artifact Artifact to be transformed. + * @param remoteRepository the repository to deploy to + * @param localRepository the local repository the metadata is stored in + */ + void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository, + ArtifactRepository localRepository ) + throws ArtifactDeploymentException; + + List getArtifactTransformations(); +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/DefaultArtifactTransformationManager.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/DefaultArtifactTransformationManager.java new file mode 100644 index 00000000..e9b1afbc --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/DefaultArtifactTransformationManager.java @@ -0,0 +1,86 @@ +package org.apache.maven.repository.legacy.resolver.transform; + +/* + * 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.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.deployer.ArtifactDeploymentException; +import org.apache.maven.artifact.installer.ArtifactInstallationException; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; + +/** + * @author Jason van Zyl + */ +@Component( role = ArtifactTransformationManager.class ) +public class DefaultArtifactTransformationManager + implements ArtifactTransformationManager +{ + @Requirement( role = ArtifactTransformation.class, hints = { "release", "latest", "snapshot" } ) + private List<ArtifactTransformation> artifactTransformations; + + public void transformForResolve( Artifact artifact, RepositoryRequest request ) + throws ArtifactResolutionException, ArtifactNotFoundException + { + for ( ArtifactTransformation transform : artifactTransformations ) + { + transform.transformForResolve( artifact, request ); + } + } + + public void transformForResolve( Artifact artifact, List<ArtifactRepository> remoteRepositories, + ArtifactRepository localRepository ) + throws ArtifactResolutionException, ArtifactNotFoundException + { + for ( ArtifactTransformation transform : artifactTransformations ) + { + transform.transformForResolve( artifact, remoteRepositories, localRepository ); + } + } + + public void transformForInstall( Artifact artifact, ArtifactRepository localRepository ) + throws ArtifactInstallationException + { + for ( ArtifactTransformation transform : artifactTransformations ) + { + transform.transformForInstall( artifact, localRepository ); + } + } + + public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository, + ArtifactRepository localRepository ) + throws ArtifactDeploymentException + { + for ( ArtifactTransformation transform : artifactTransformations ) + { + transform.transformForDeployment( artifact, remoteRepository, localRepository ); + } + } + + public List getArtifactTransformations() + { + return artifactTransformations; + } +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/LatestArtifactTransformation.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/LatestArtifactTransformation.java new file mode 100644 index 00000000..7b0e8513 --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/LatestArtifactTransformation.java @@ -0,0 +1,74 @@ +package org.apache.maven.repository.legacy.resolver.transform; + +/* + * 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 org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.codehaus.plexus.component.annotations.Component; + +@Component( role = ArtifactTransformation.class, hint = "latest" ) +public class LatestArtifactTransformation + extends AbstractVersionTransformation +{ + + public void transformForResolve( Artifact artifact, RepositoryRequest request ) + throws ArtifactResolutionException, ArtifactNotFoundException + { + if ( Artifact.LATEST_VERSION.equals( artifact.getVersion() ) ) + { + try + { + String version = resolveVersion( artifact, request ); + if ( Artifact.LATEST_VERSION.equals( version ) ) + { + throw new ArtifactNotFoundException( "Unable to determine the latest version", artifact ); + } + + artifact.setBaseVersion( version ); + artifact.updateVersion( version, request.getLocalRepository() ); + } + catch ( RepositoryMetadataResolutionException e ) + { + throw new ArtifactResolutionException( e.getMessage(), artifact, e ); + } + } + } + + public void transformForInstall( Artifact artifact, ArtifactRepository localRepository ) + { + // metadata is added via addPluginArtifactMetadata + } + + public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository, + ArtifactRepository localRepository ) + { + // metadata is added via addPluginArtifactMetadata + } + + protected String constructVersion( Versioning versioning, String baseVersion ) + { + return versioning.getLatest(); + } +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ReleaseArtifactTransformation.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ReleaseArtifactTransformation.java new file mode 100644 index 00000000..3714924c --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/ReleaseArtifactTransformation.java @@ -0,0 +1,100 @@ +package org.apache.maven.repository.legacy.resolver.transform; + +/* + * 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 org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.metadata.ArtifactMetadata; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.codehaus.plexus.component.annotations.Component; + +/** + * Change the version <code>RELEASE</code> to the appropriate release version from the remote repository. + * + * @author <a href="mailto:brett@apache.org">Brett Porter</a> + */ +@Component( role = ArtifactTransformation.class, hint = "release" ) +public class ReleaseArtifactTransformation + extends AbstractVersionTransformation +{ + + public void transformForResolve( Artifact artifact, RepositoryRequest request ) + throws ArtifactResolutionException, ArtifactNotFoundException + { + if ( Artifact.RELEASE_VERSION.equals( artifact.getVersion() ) ) + { + try + { + String version = resolveVersion( artifact, request ); + + if ( Artifact.RELEASE_VERSION.equals( version ) ) + { + throw new ArtifactNotFoundException( "Unable to determine the release version", artifact ); + } + + artifact.setBaseVersion( version ); + artifact.updateVersion( version, request.getLocalRepository() ); + } + catch ( RepositoryMetadataResolutionException e ) + { + throw new ArtifactResolutionException( e.getMessage(), artifact, e ); + } + } + } + + public void transformForInstall( Artifact artifact, ArtifactRepository localRepository ) + { + ArtifactMetadata metadata = createMetadata( artifact ); + + artifact.addMetadata( metadata ); + } + + public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository, + ArtifactRepository localRepository ) + { + ArtifactMetadata metadata = createMetadata( artifact ); + + artifact.addMetadata( metadata ); + } + + private ArtifactMetadata createMetadata( Artifact artifact ) + { + Versioning versioning = new Versioning(); + versioning.updateTimestamp(); + versioning.addVersion( artifact.getVersion() ); + + if ( artifact.isRelease() ) + { + versioning.setRelease( artifact.getVersion() ); + } + + return new ArtifactRepositoryMetadata( artifact, versioning ); + } + + protected String constructVersion( Versioning versioning, String baseVersion ) + { + return versioning.getRelease(); + } +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/SnapshotTransformation.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/SnapshotTransformation.java new file mode 100644 index 00000000..94f0ec0b --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/repository/legacy/resolver/transform/SnapshotTransformation.java @@ -0,0 +1,171 @@ +package org.apache.maven.repository.legacy.resolver.transform; + +/* + * 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.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.deployer.ArtifactDeploymentException; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.RepositoryRequest; +import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException; +import org.apache.maven.artifact.repository.metadata.Snapshot; +import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.util.StringUtils; + +/** + * @author <a href="mailto:brett@apache.org">Brett Porter</a> + * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a> + */ +@Component( role = ArtifactTransformation.class, hint = "snapshot" ) +public class SnapshotTransformation + extends AbstractVersionTransformation +{ + private String deploymentTimestamp; + + private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" ); + + private static final String UTC_TIMESTAMP_PATTERN = "yyyyMMdd.HHmmss"; + + public void transformForResolve( Artifact artifact, RepositoryRequest request ) + throws ArtifactResolutionException + { + // Only select snapshots that are unresolved (eg 1.0-SNAPSHOT, not 1.0-20050607.123456) + if ( artifact.isSnapshot() && artifact.getBaseVersion().equals( artifact.getVersion() ) ) + { + try + { + String version = resolveVersion( artifact, request ); + artifact.updateVersion( version, request.getLocalRepository() ); + } + catch ( RepositoryMetadataResolutionException e ) + { + throw new ArtifactResolutionException( e.getMessage(), artifact, e ); + } + } + } + + public void transformForInstall( Artifact artifact, ArtifactRepository localRepository ) + { + if ( artifact.isSnapshot() ) + { + Snapshot snapshot = new Snapshot(); + snapshot.setLocalCopy( true ); + RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot ); + + artifact.addMetadata( metadata ); + } + } + + public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository, + ArtifactRepository localRepository ) + throws ArtifactDeploymentException + { + if ( artifact.isSnapshot() ) + { + Snapshot snapshot = new Snapshot(); + + snapshot.setTimestamp( getDeploymentTimestamp() ); + + // we update the build number anyway so that it doesn't get lost. It requires the timestamp to take effect + try + { + int buildNumber = resolveLatestSnapshotBuildNumber( artifact, localRepository, remoteRepository ); + + snapshot.setBuildNumber( buildNumber + 1 ); + } + catch ( RepositoryMetadataResolutionException e ) + { + throw new ArtifactDeploymentException( "Error retrieving previous build number for artifact '" + + artifact.getDependencyConflictId() + "': " + e.getMessage(), e ); + } + + RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot ); + + artifact.setResolvedVersion( + constructVersion( metadata.getMetadata().getVersioning(), artifact.getBaseVersion() ) ); + + artifact.addMetadata( metadata ); + } + } + + public String getDeploymentTimestamp() + { + if ( deploymentTimestamp == null ) + { + deploymentTimestamp = getUtcDateFormatter().format( new Date() ); + } + return deploymentTimestamp; + } + + protected String constructVersion( Versioning versioning, String baseVersion ) + { + String version = null; + Snapshot snapshot = versioning.getSnapshot(); + if ( snapshot != null ) + { + if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 ) + { + String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber(); + version = StringUtils.replace( baseVersion, Artifact.SNAPSHOT_VERSION, newVersion ); + } + else + { + version = baseVersion; + } + } + return version; + } + + private int resolveLatestSnapshotBuildNumber( Artifact artifact, ArtifactRepository localRepository, + ArtifactRepository remoteRepository ) + throws RepositoryMetadataResolutionException + { + RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact ); + + getLogger().info( "Retrieving previous build number from " + remoteRepository.getId() ); + repositoryMetadataManager.resolveAlways( metadata, localRepository, remoteRepository ); + + int buildNumber = 0; + Metadata repoMetadata = metadata.getMetadata(); + if ( ( repoMetadata != null ) + && ( repoMetadata.getVersioning() != null && repoMetadata.getVersioning().getSnapshot() != null ) ) + { + buildNumber = repoMetadata.getVersioning().getSnapshot().getBuildNumber(); + } + return buildNumber; + } + + public static DateFormat getUtcDateFormatter() + { + DateFormat utcDateFormatter = new SimpleDateFormat( UTC_TIMESTAMP_PATTERN ); + utcDateFormatter.setTimeZone( UTC_TIME_ZONE ); + return utcDateFormatter; + } + +} |