From 7a52fa2fbf3d80043f35864bfd301c5cee6811be Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Wed, 25 Nov 2009 05:32:18 +0000 Subject: [PATCH] [MRM-1282] handle snapshot versions git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@883981 13f79535-47bb-0310-9956-ffa450edef68 --- .../plugins/maven2-repository/pom.xml | 7 +- .../Maven2RepositoryMetadataResolver.java | 37 +++- .../maven2/MavenRepositoryMetadata.java | 200 ++++++++++++++++++ .../maven2/MavenRepositoryMetadataReader.java | 90 ++++++++ .../Maven2RepositoryMetadataResolverTest.java | 18 +- .../MavenRepositoryMetadataReaderTest.java | 73 +++++++ .../5-SNAPSHOT/apache-5-20080801.151215-1.pom | 113 ++++++++++ .../apache/5-SNAPSHOT/maven-metadata.xml | 14 ++ .../maven-downloader/maven-metadata.xml | 34 +++ pom.xml | 6 + 10 files changed, 576 insertions(+), 16 deletions(-) create mode 100644 archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadata.java create mode 100644 archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReader.java create mode 100644 archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReaderTest.java create mode 100644 archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/apache/5-SNAPSHOT/apache-5-20080801.151215-1.pom create mode 100644 archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/apache/5-SNAPSHOT/maven-metadata.xml create mode 100644 archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/maven/shared/maven-downloader/maven-metadata.xml diff --git a/archiva-modules/plugins/maven2-repository/pom.xml b/archiva-modules/plugins/maven2-repository/pom.xml index 1524507c7..0256af235 100644 --- a/archiva-modules/plugins/maven2-repository/pom.xml +++ b/archiva-modules/plugins/maven2-repository/pom.xml @@ -54,12 +54,15 @@ org.apache.maven maven-model-builder - 3.0-alpha-4 org.apache.archiva archiva-configuration - 1.3-SNAPSHOT + + + + org.apache.archiva + archiva-xml-tools diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java index 4fa121373..c8fed1011 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java @@ -22,12 +22,14 @@ package org.apache.archiva.metadata.repository.storage.maven2; import java.io.File; import java.util.Collection; -import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.model.ProjectMetadata; +import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.repository.MetadataResolver; import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator; +import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.xml.XMLException; import org.apache.maven.model.Model; import org.apache.maven.model.building.DefaultModelBuildingRequest; import org.apache.maven.model.building.ModelBuilder; @@ -60,16 +62,40 @@ public class Maven2RepositoryMetadataResolver throw new UnsupportedOperationException(); } - public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId, String projectVersion ) + public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId, + String projectVersion ) { // TODO: artifactVersion translation ManagedRepositoryConfiguration repositoryConfiguration = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId ); + String artifactVersion = projectVersion; + File basedir = new File( repositoryConfiguration.getLocation() ); - File file = pathTranslator.toFile( basedir, namespace, projectId, projectVersion, projectId + "-" + - projectVersion + ".pom" ); + if ( VersionUtil.isSnapshot( projectVersion ) ) + { + // TODO: need much error handling here for incorrect metadata + try + { + MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( + pathTranslator.toFile( basedir, namespace, projectId, projectVersion, "maven-metadata.xml" ) ); + + artifactVersion = + artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end + MavenRepositoryMetadata.Snapshot snapshotVersion = metadata.getSnapshotVersion(); + artifactVersion = + artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber(); + } + catch ( XMLException e ) + { + // TODO: handle it + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + File file = pathTranslator.toFile( basedir, namespace, projectId, projectVersion, + projectId + "-" + artifactVersion + ".pom" ); ModelBuildingRequest req = new DefaultModelBuildingRequest(); req.setProcessPlugins( false ); @@ -107,7 +133,8 @@ public class Maven2RepositoryMetadataResolver return metadata; } - public Collection getArtifactVersions( String repoId, String namespace, String projectId, String projectVersion ) + public Collection getArtifactVersions( String repoId, String namespace, String projectId, + String projectVersion ) { throw new UnsupportedOperationException(); } diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadata.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadata.java new file mode 100644 index 000000000..b5ef400a4 --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadata.java @@ -0,0 +1,200 @@ +package org.apache.archiva.metadata.repository.storage.maven2; + +/* + * 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.ArrayList; +import java.util.List; + +public class MavenRepositoryMetadata +{ + private String groupId; + + private String artifactId; + + private String version; + + private String lastUpdated; + + private String latestVersion; + + private String releasedVersion; + + private List availableVersions; + + private Snapshot snapshotVersion; + + private List plugins = new ArrayList(); + + public List getPlugins() + { + return plugins; + } + + public void setGroupId( String groupId ) + { + this.groupId = groupId; + } + + public void setArtifactId( String artifactId ) + { + this.artifactId = artifactId; + } + + public void setVersion( String version ) + { + this.version = version; + } + + public void setLastUpdated( String lastUpdated ) + { + this.lastUpdated = lastUpdated; + } + + public void setLatestVersion( String latestVersion ) + { + this.latestVersion = latestVersion; + } + + public void setReleasedVersion( String releasedVersion ) + { + this.releasedVersion = releasedVersion; + } + + public void setAvailableVersions( List availableVersions ) + { + this.availableVersions = availableVersions; + } + + public void setSnapshotVersion( Snapshot snapshotVersion ) + { + this.snapshotVersion = snapshotVersion; + } + + public void addPlugin( Plugin plugin ) + { + this.plugins.add( plugin ); + } + + public String getGroupId() + { + return groupId; + } + + public String getArtifactId() + { + return artifactId; + } + + public String getVersion() + { + return version; + } + + public String getLastUpdated() + { + return lastUpdated; + } + + public String getLatestVersion() + { + return latestVersion; + } + + public String getReleasedVersion() + { + return releasedVersion; + } + + public List getAvailableVersions() + { + return availableVersions; + } + + public Snapshot getSnapshotVersion() + { + return snapshotVersion; + } + + public static class Snapshot + { + private String timestamp; + + private int buildNumber; + + public void setTimestamp( String timestamp ) + { + this.timestamp = timestamp; + } + + public void setBuildNumber( int buildNumber ) + { + this.buildNumber = buildNumber; + } + + public int getBuildNumber() + { + return buildNumber; + } + + public String getTimestamp() + { + return timestamp; + } + } + + public static class Plugin + { + private String prefix; + + private String artifactId; + + private String name; + + public void setPrefix( String prefix ) + { + this.prefix = prefix; + } + + public void setArtifactId( String artifactId ) + { + this.artifactId = artifactId; + } + + public void setName( String name ) + { + this.name = name; + } + + public String getPrefix() + { + return prefix; + } + + public String getArtifactId() + { + return artifactId; + } + + public String getName() + { + return name; + } + } +} diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReader.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReader.java new file mode 100644 index 000000000..a56f0befd --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReader.java @@ -0,0 +1,90 @@ +package org.apache.archiva.metadata.repository.storage.maven2; + +/* + * 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 org.apache.commons.lang.math.NumberUtils; +import org.apache.maven.archiva.xml.XMLException; +import org.apache.maven.archiva.xml.XMLReader; +import org.dom4j.Element; + +/** + * RepositoryMetadataReader - read maven-metadata.xml files. + * + * TODO: we should improve on this, ideally using the Maven standard libraries (which are unfortunately baked into + * maven-core now) + */ +public final class MavenRepositoryMetadataReader +{ + private MavenRepositoryMetadataReader() + { + } + + /** + * Read and return the {@link MavenRepositoryMetadata} object from the provided xml file. + * + * @param metadataFile the maven-metadata.xml file to read. + * @return the archiva repository metadata object that represents the provided file contents. + * @throws org.apache.maven.archiva.xml.XMLException + */ + public static MavenRepositoryMetadata read( File metadataFile ) + throws XMLException + { + XMLReader xml = new XMLReader( "metadata", metadataFile ); + // invoke this to remove namespaces, see MRM-1136 + xml.removeNamespaces(); + + MavenRepositoryMetadata metadata = new MavenRepositoryMetadata(); + + metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) ); + metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) ); + metadata.setVersion( xml.getElementText( "//metadata/version" ) ); + + metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) ); + metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) ); + metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) ); + metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) ); + + Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" ); + if ( snapshotElem != null ) + { + MavenRepositoryMetadata.Snapshot snapshot = new MavenRepositoryMetadata.Snapshot(); + snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) ); + String tmp = snapshotElem.elementTextTrim( "buildNumber" ); + if ( NumberUtils.isNumber( tmp ) ) + { + snapshot.setBuildNumber( NumberUtils.toInt( tmp ) ); + } + metadata.setSnapshotVersion( snapshot ); + } + + for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) ) + { + MavenRepositoryMetadata.Plugin p = new MavenRepositoryMetadata.Plugin(); + p.setPrefix( plugin.elementTextTrim( "prefix" ) ); + p.setArtifactId( plugin.elementTextTrim( "artifactId" ) ); + p.setName( plugin.elementTextTrim( "name" ) ); + metadata.addPlugin( p ); + } + + return metadata; + } +} \ No newline at end of file diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java index 748b3b7ac..b82c95c33 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java @@ -59,13 +59,13 @@ public class Maven2RepositoryMetadataResolverTest // TODO: more testing } -// public void testGetProjectVersionMetadataForTimestampedSnapshot() -// { -// ProjectVersionMetadata metadata = -// resolver.getProjectVersion( TEST_REPO_ID, "org.apache", "apache", "5-SNAPSHOT" ); -// MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID ); -// assertEquals( "jar", facet.getPackaging() ); -// assertEquals( "http://www.apache.org/", metadata.getUrl() ); -// // TODO: more testing -// } + public void testGetProjectVersionMetadataForTimestampedSnapshot() + { + ProjectVersionMetadata metadata = + resolver.getProjectVersion( TEST_REPO_ID, "org.apache", "apache", "5-SNAPSHOT" ); + MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID ); + assertEquals( "pom", facet.getPackaging() ); + assertEquals( "http://www.apache.org/", metadata.getUrl() ); + // TODO: more testing + } } diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReaderTest.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReaderTest.java new file mode 100644 index 000000000..b88036f37 --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReaderTest.java @@ -0,0 +1,73 @@ +package org.apache.archiva.metadata.repository.storage.maven2; + +/* + * 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.util.Arrays; + +import org.apache.maven.archiva.xml.XMLException; +import org.codehaus.plexus.spring.PlexusInSpringTestCase; + +/** + * RepositoryMetadataReaderTest + * + * @version $Id$ + */ +public class MavenRepositoryMetadataReaderTest + extends PlexusInSpringTestCase +{ + public void testProjectMetadata() + throws XMLException + { + File defaultRepoDir = new File( getBasedir(), "src/test/repositories/test" ); + File metadataFile = new File( defaultRepoDir, "org/apache/maven/shared/maven-downloader/maven-metadata.xml" ); + + MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile ); + + assertNotNull( metadata ); + assertEquals( "org.apache.maven.shared", metadata.getGroupId() ); + assertEquals( "maven-downloader", metadata.getArtifactId() ); + assertEquals( "1.1", metadata.getReleasedVersion() ); + assertNull( metadata.getLatestVersion() ); + assertEquals( Arrays.asList( "1.0", "1.1" ), metadata.getAvailableVersions() ); + assertNull( metadata.getSnapshotVersion() ); + assertEquals( "20061212214311", metadata.getLastUpdated() ); + } + + public void testProjectVersionMetadata() + throws XMLException + { + File defaultRepoDir = new File( getBasedir(), "src/test/repositories/test" ); + File metadataFile = new File( defaultRepoDir, "org/apache/apache/5-SNAPSHOT/maven-metadata.xml" ); + + MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile ); + + assertNotNull( metadata ); + assertEquals( "org.apache", metadata.getGroupId() ); + assertEquals( "apache", metadata.getArtifactId() ); + assertNull( metadata.getReleasedVersion() ); + assertNull( metadata.getLatestVersion() ); + assertTrue( metadata.getAvailableVersions().isEmpty() ); + assertNotNull( metadata.getSnapshotVersion() ); + assertEquals( "20080801.151215", metadata.getSnapshotVersion().getTimestamp() ); + assertEquals( 1, metadata.getSnapshotVersion().getBuildNumber() ); + assertEquals( "20080801151215", metadata.getLastUpdated() ); + } +} \ No newline at end of file diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/apache/5-SNAPSHOT/apache-5-20080801.151215-1.pom b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/apache/5-SNAPSHOT/apache-5-20080801.151215-1.pom new file mode 100644 index 000000000..df85cfa77 --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/apache/5-SNAPSHOT/apache-5-20080801.151215-1.pom @@ -0,0 +1,113 @@ + + + + + + 4.0.0 + + + org.apache + apache + 5-SNAPSHOT + pom + The Apache Software Foundation + + The Apache Software Foundation provides support for the Apache community of open-source software projects. + The Apache projects are characterized by a collaborative, consensus based development process, an open and + pragmatic software license, and a desire to create high quality software that leads the way in its field. + We consider ourselves not simply a group of projects sharing a server, but rather a community of developers + and users. + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + The Apache Software Foundation + http://www.apache.org/ + + http://www.apache.org/ + + + apache.snapshots + Apache Snapshot Repository + http://people.apache.org/repo/m2-snapshot-repository + + false + + + + + + + apache.releases + Apache Release Distribution Repository + scp://people.apache.org/www/people.apache.org/repo/m2-ibiblio-rsync-repository + + + apache.snapshots + Apache Development Snapshot Repository + scp://people.apache.org/www/people.apache.org/repo/m2-snapshot-repository + + + + + Apache Announce List + announce-subscribe@apache.org + announce-unsubscribe@apache.org + announce@apache.org + http://mail-archives.apache.org/mod_mbox/www-announce/ + + + + http://www.apache.org/images/asf_logo_wide.gif + + + + scm:svn:http://svn.apache.org/repos/asf/maven/pom/trunk/asf + scm:svn:https://svn.apache.org/repos/asf/maven/pom/trunk/asf + http://svn.apache.org/viewvc/maven/pom/trunk/asf + + + diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/apache/5-SNAPSHOT/maven-metadata.xml b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/apache/5-SNAPSHOT/maven-metadata.xml new file mode 100644 index 000000000..46dc46ee0 --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/apache/5-SNAPSHOT/maven-metadata.xml @@ -0,0 +1,14 @@ + + + + org.apache + apache + 5-SNAPSHOT + + + 1 + 20080801.151215 + + 20080801151215 + + diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/maven/shared/maven-downloader/maven-metadata.xml b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/maven/shared/maven-downloader/maven-metadata.xml new file mode 100644 index 000000000..5a7970a2e --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/maven/shared/maven-downloader/maven-metadata.xml @@ -0,0 +1,34 @@ + + + + + + org.apache.maven.shared + maven-downloader + 1.0 + + 1.1 + + 1.0 + 1.1 + + 20061212214311 + + diff --git a/pom.xml b/pom.xml index 5d647b7f7..f3a2754a5 100644 --- a/pom.xml +++ b/pom.xml @@ -622,6 +622,11 @@ maven-model ${maven.version} + + org.apache.maven + maven-model-builder + ${maven3x.version} + org.apache.maven maven-repository-metadata @@ -1133,6 +1138,7 @@ 2.0.8 + 3.0-alpha-4 1.0-beta-5 1.2.2 6.1.19