From 27f12f85c1cce7857d2981a1bbfb707ea55be630 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Tue, 26 Apr 2005 23:19:21 +0000 Subject: [PATCH] restore deleted files, and fix package on ProjectArtifactMetadata git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@164909 13f79535-47bb-0310-9956-ffa450edef68 --- .../project/artifact/MavenMetadataSource.java | 163 ++++++++++++++++++ .../artifact/ProjectArtifactMetadata.java | 114 ++++++++++++ 2 files changed, 277 insertions(+) create mode 100644 maven-project/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java create mode 100644 maven-project/src/main/java/org/apache/maven/project/artifact/ProjectArtifactMetadata.java diff --git a/maven-project/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-project/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java new file mode 100644 index 0000000000..7c64b379f8 --- /dev/null +++ b/maven-project/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -0,0 +1,163 @@ +package org.apache.maven.project.artifact; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.factory.ArtifactFactory; +import org.apache.maven.artifact.factory.DefaultArtifactFactory; +import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.wagon.util.IoUtils; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/** + * @author Jason van Zyl + * @version $Id$ + */ +public class MavenMetadataSource + implements ArtifactMetadataSource +{ + private MavenProjectBuilder mavenProjectBuilder; + + private ArtifactResolver artifactResolver; + + // TODO: configure? + protected ArtifactFactory artifactFactory = new DefaultArtifactFactory(); + + /** + * @todo remove. + */ + private MavenXpp3Reader reader = new MavenXpp3Reader(); + + public MavenMetadataSource( ArtifactResolver artifactResolver ) + { + this.artifactResolver = artifactResolver; + this.mavenProjectBuilder = null; + } + + public MavenMetadataSource( ArtifactResolver artifactResolver, MavenProjectBuilder projectBuilder ) + { + this.artifactResolver = artifactResolver; + this.mavenProjectBuilder = projectBuilder; + } + + public Set retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) + throws ArtifactMetadataRetrievalException + { + // TODO: only metadata is really needed - resolve as metadata + artifact = artifactFactory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), + artifact.getVersion(), artifact.getScope(), "pom" ); + + List dependencies = null; + + // Use the ProjectBuilder, to enable post-processing and inheritance calculation before retrieving the + // associated artifacts. + if ( mavenProjectBuilder != null ) + { + try + { + MavenProject p = mavenProjectBuilder.buildFromRepository( artifact, remoteRepositories, + localRepository ); + dependencies = p.getDependencies(); + } + catch ( ProjectBuildingException e ) + { + throw new ArtifactMetadataRetrievalException( "Unable to read the metadata file", e ); + } + } + else + { + // there is code in plexus that uses this (though it shouldn't) so we + // need to be able to not have a project builder + // TODO: remove - which then makes this a very thin wrapper around a project builder - is it needed? + + try + { + artifactResolver.resolve( artifact, remoteRepositories, localRepository ); + } + catch ( ArtifactResolutionException e ) + { + throw new ArtifactMetadataRetrievalException( "Error while resolving metadata artifact", e ); + } + + FileReader reader = null; + try + { +// String path = localRepository.pathOfMetadata( new ProjectArtifactMetadata( artifact, null ) ); +// File file = new File( localRepository.getBasedir(), path ); + File file = artifact.getFile(); + reader = new FileReader( file ); + Model model = this.reader.read( reader ); + dependencies = model.getDependencies(); + } + catch ( FileNotFoundException e ) + { + throw new ArtifactMetadataRetrievalException( "Unable to find the metadata file", e ); + } + catch ( IOException e ) + { + throw new ArtifactMetadataRetrievalException( "Unable to read the metadata file", e ); + } + catch ( XmlPullParserException e ) + { + throw new ArtifactMetadataRetrievalException( "Unable to parse the metadata file", e ); + } + finally + { + IoUtils.close( reader ); + } + } + return createArtifacts( dependencies, artifact.getScope() ); + } + + protected Set createArtifacts( List dependencies, String inheritedScope ) + { + Set projectArtifacts = new HashSet(); + + for ( Iterator i = dependencies.iterator(); i.hasNext(); ) + { + Dependency d = (Dependency) i.next(); + + Artifact artifact = artifactFactory.createArtifact( d.getGroupId(), d.getArtifactId(), d.getVersion(), + d.getScope(), d.getType(), inheritedScope ); + if ( artifact != null ) + { + projectArtifacts.add( artifact ); + } + } + + return projectArtifacts; + } +} diff --git a/maven-project/src/main/java/org/apache/maven/project/artifact/ProjectArtifactMetadata.java b/maven-project/src/main/java/org/apache/maven/project/artifact/ProjectArtifactMetadata.java new file mode 100644 index 0000000000..54f58e97bc --- /dev/null +++ b/maven-project/src/main/java/org/apache/maven/project/artifact/ProjectArtifactMetadata.java @@ -0,0 +1,114 @@ +package org.apache.maven.project.artifact; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.manager.WagonManager; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.layout.ArtifactPathFormatException; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +/** + * Attach a POM to an artifact. + * + * @author Brett Porter + * @version $Id$ + */ +public class ProjectArtifactMetadata + extends AbstractArtifactMetadata +{ + private final File file; + + public ProjectArtifactMetadata( Artifact artifact, File file ) + { + super( artifact, null ); + this.file = file; + } + + public String getFilename() + { + return getArtifact().getArtifactId() + "-" + getArtifact().getVersion() + ".pom"; + } + + public void storeInLocalRepository( ArtifactRepository localRepository ) + throws ArtifactMetadataRetrievalException + { + File destination; + try + { + destination = new File( localRepository.getBasedir(), localRepository.pathOfMetadata( this ) ); + } + catch ( ArtifactPathFormatException e ) + { + throw new ArtifactMetadataRetrievalException( "Unable to install POM", e ); + } + + destination.getParentFile().mkdirs(); + + FileReader reader = null; + FileWriter writer = null; + try + { + reader = new FileReader( file ); + writer = new FileWriter( destination ); + + MavenXpp3Reader modelReader = new MavenXpp3Reader(); + Model model = modelReader.read( reader ); + model.setVersion( getArtifact().getVersion() ); + + MavenXpp3Writer modelWriter = new MavenXpp3Writer(); + modelWriter.write( writer, model ); + } + catch ( FileNotFoundException e ) + { + throw new ArtifactMetadataRetrievalException( "Error rewriting POM", e ); + } + catch ( IOException e ) + { + throw new ArtifactMetadataRetrievalException( "Error rewriting POM", e ); + } + catch ( XmlPullParserException e ) + { + throw new ArtifactMetadataRetrievalException( "Error rewriting POM", e ); + } + finally + { + IOUtil.close( reader ); + IOUtil.close( writer ); + } + } + + public void retrieveFromRemoteRepository( ArtifactRepository remoteRepository, WagonManager wagonManager ) + { + // not used - TODO: again indicates bad design? + } + + public String toString() + { + return "project information for " + artifact.getArtifactId() + " " + artifact.getVersion(); + } +}