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.codehaus.plexus.util.IOUtil;
25 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
32 import java.util.jar.JarEntry;
33 import java.util.jar.JarFile;
36 * Validate the location of the artifact based on the values indicated
37 * in its pom (both the pom packaged with the artifact & the pom in the
40 * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="artifact-location"
42 public class LocationArtifactReportProcessor
43 implements ArtifactReportProcessor
48 private ArtifactFactory artifactFactory;
51 * Check whether the artifact is in its proper location. The location of the artifact
52 * is validated first against the groupId, artifactId and versionId in the specified model
53 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
54 * included in the package. If a model exists inside the package, then check if the artifact's
55 * location is valid based on the location specified in the pom. Check if the both the location
56 * specified in the file system pom and in the pom included in the package is the same.
58 * @param model Represents the pom in the file system.
63 public void processArtifact( Model model, Artifact artifact, ArtifactReporter reporter,
64 ArtifactRepository repository )
65 throws ReportProcessorException
67 if ( !"file".equals( repository.getProtocol() ) )
69 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
70 throw new UnsupportedOperationException(
71 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
74 //check if the artifact is located in its proper location based on the info
75 //specified in the model object/pom
76 Artifact modelArtifact = artifactFactory.createBuildArtifact( model.getGroupId(), model.getArtifactId(),
77 model.getVersion(), model.getPackaging() );
79 boolean failed = false;
80 String modelPath = repository.pathOf( modelArtifact );
81 String artifactPath = repository.pathOf( artifact );
82 if ( modelPath.equals( artifactPath ) )
84 //get the location of the artifact itself
85 File file = new File( repository.getBasedir(), artifactPath );
89 //unpack the artifact (using the groupId, artifactId & version specified in the artifact object itself
90 //check if the pom is included in the package
91 Model extractedModel = readArtifactModel( file, artifact.getGroupId(), artifact.getArtifactId() );
93 if ( extractedModel != null )
95 Artifact extractedArtifact = artifactFactory.createBuildArtifact( extractedModel.getGroupId(),
96 extractedModel.getArtifactId(),
97 extractedModel.getVersion(),
98 extractedModel.getPackaging() );
99 if ( !repository.pathOf( extractedArtifact ).equals( artifactPath ) )
101 reporter.addFailure( artifact,
102 "The artifact is out of place. It does not match the specified location in the packaged pom." );
109 reporter.addFailure( artifact,
110 "The artifact is out of place. It does not exist at the specified location in the repository pom." );
116 reporter.addFailure( artifact,
117 "The artifact is out of place. It does not match the specified location in the repository pom." );
123 reporter.addSuccess( artifact );
128 * Extract the contents of the artifact/jar file.
134 private Model readArtifactModel( File file, String groupId, String artifactId )
135 throws ReportProcessorException
142 jar = new JarFile( file );
144 //Get the entry and its input stream.
145 JarEntry entry = jar.getJarEntry( "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml" );
147 // If the entry is not null, extract it.
150 model = readModel( jar.getInputStream( entry ) );
153 catch ( IOException e )
155 // TODO: should just warn and continue!
156 throw new ReportProcessorException( "Unable to read artifact to extract model", e );
158 catch ( XmlPullParserException e )
160 // TODO: should just warn and continue!
161 throw new ReportProcessorException( "Unable to read artifact to extract model", e );
167 //noinspection UnusedCatchParameter
172 catch ( IOException e )
181 private Model readModel( InputStream entryStream )
182 throws IOException, XmlPullParserException
184 Reader isReader = new InputStreamReader( entryStream );
189 MavenXpp3Reader pomReader = new MavenXpp3Reader();
190 model = pomReader.read( isReader );
194 IOUtil.close( isReader );