1 package org.apache.maven.archiva.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.model.Model;
23 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
24 import org.apache.maven.project.MavenProjectBuilder;
25 import org.codehaus.plexus.util.IOUtil;
26 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.Reader;
33 import java.util.Arrays;
34 import java.util.HashSet;
36 import java.util.jar.JarEntry;
37 import java.util.jar.JarFile;
40 * Validate the location of the artifact based on the values indicated
41 * in its pom (both the pom packaged with the artifact & the pom in the
44 * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="artifact-location"
46 public class LocationArtifactReportProcessor
47 implements ArtifactReportProcessor
52 private ArtifactFactory artifactFactory;
54 // TODO: share with other code with the same
55 private static final Set JAR_FILE_TYPES =
56 new HashSet( Arrays.asList( new String[]{"jar", "war", "par", "ejb", "ear", "rar", "sar"} ) );
61 private MavenProjectBuilder projectBuilder;
63 private static final String POM = "pom";
66 * Check whether the artifact is in its proper location. The location of the artifact
67 * is validated first against the groupId, artifactId and versionId in the specified model
68 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
69 * included in the package. If a model exists inside the package, then check if the artifact's
70 * location is valid based on the location specified in the pom. Check if the both the location
71 * specified in the file system pom and in the pom included in the package is the same.
73 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
75 ArtifactRepository repository = artifact.getRepository();
77 if ( !"file".equals( repository.getProtocol() ) )
79 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
80 throw new UnsupportedOperationException(
81 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
84 String artifactPath = repository.pathOf( artifact );
88 // only check if it is a standalone POM, or an artifact other than a POM
89 // ie, don't check the location of the POM for another artifact matches that of the artifact
90 if ( !POM.equals( artifact.getType() ) || POM.equals( model.getPackaging() ) )
92 //check if the artifact is located in its proper location based on the info
93 //specified in the model object/pom
94 Artifact modelArtifact = artifactFactory.createArtifactWithClassifier( model.getGroupId(),
95 model.getArtifactId(),
98 artifact.getClassifier() );
100 String modelPath = repository.pathOf( modelArtifact );
101 if ( !modelPath.equals( artifactPath ) )
103 reporter.addFailure( artifact,
104 "The artifact is out of place. It does not match the specified location in the repository pom: " +
110 //get the location of the artifact itself
111 File file = new File( repository.getBasedir(), artifactPath );
115 if ( JAR_FILE_TYPES.contains( artifact.getType() ) )
117 //unpack the artifact (using the groupId, artifactId & version specified in the artifact object itself
118 //check if the pom is included in the package
119 Model extractedModel = readArtifactModel( file, artifact, reporter );
121 if ( extractedModel != null )
123 Artifact extractedArtifact = artifactFactory.createBuildArtifact( extractedModel.getGroupId(),
124 extractedModel.getArtifactId(),
125 extractedModel.getVersion(),
126 extractedModel.getPackaging() );
127 if ( !repository.pathOf( extractedArtifact ).equals( artifactPath ) )
129 reporter.addFailure( artifact,
130 "The artifact is out of place. It does not match the specified location in the packaged pom." );
137 throw new IllegalStateException( "Couldn't find artifact " + file );
141 private Model readArtifactModel( File file, Artifact artifact, ReportingDatabase reporter )
148 jar = new JarFile( file );
150 //Get the entry and its input stream.
151 JarEntry entry = jar.getJarEntry(
152 "META-INF/maven/" + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/pom.xml" );
154 // If the entry is not null, extract it.
157 model = readModel( jar.getInputStream( entry ) );
159 if ( model.getGroupId() == null )
161 model.setGroupId( model.getParent().getGroupId() );
163 if ( model.getVersion() == null )
165 model.setVersion( model.getParent().getVersion() );
169 catch ( IOException e )
171 reporter.addWarning( artifact, "Unable to read artifact to extract model: " + e );
173 catch ( XmlPullParserException e )
175 reporter.addWarning( artifact, "Unable to parse extracted model: " + e );
181 //noinspection UnusedCatchParameter
186 catch ( IOException e )
195 private Model readModel( InputStream entryStream )
196 throws IOException, XmlPullParserException
198 Reader isReader = new InputStreamReader( entryStream );
203 MavenXpp3Reader pomReader = new MavenXpp3Reader();
204 model = pomReader.read( isReader );
208 IOUtil.close( isReader );