1 package org.apache.maven.repository.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 public class LocationArtifactReportProcessor
41 implements ArtifactReportProcessor
43 private ArtifactFactory artifactFactory;
46 * Check whether the artifact is in its proper location. The location of the artifact
47 * is validated first against the groupId, artifactId and versionId in the specified model
48 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
49 * included in the package. If a model exists inside the package, then check if the artifact's
50 * location is valid based on the location specified in the pom. Check if the both the location
51 * specified in the file system pom and in the pom included in the package is the same.
53 * @param model Represents the pom in the file system.
58 public void processArtifact( Model model, Artifact artifact, ArtifactReporter reporter,
59 ArtifactRepository repository )
60 throws ReportProcessorException
62 if ( !"file".equals( repository.getProtocol() ) )
64 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
65 throw new UnsupportedOperationException(
66 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
69 //check if the artifact is located in its proper location based on the info
70 //specified in the model object/pom
71 Artifact modelArtifact = artifactFactory.createBuildArtifact( model.getGroupId(), model.getArtifactId(),
72 model.getVersion(), model.getPackaging() );
74 boolean failed = false;
75 String modelPath = repository.pathOf( modelArtifact );
76 String artifactPath = repository.pathOf( artifact );
77 if ( modelPath.equals( artifactPath ) )
79 //get the location of the artifact itself
80 File file = new File( repository.getBasedir(), artifactPath );
84 //unpack the artifact (using the groupId, artifactId & version specified in the artifact object itself
85 //check if the pom is included in the package
86 Model extractedModel = readArtifactModel( file, artifact.getGroupId(), artifact.getArtifactId() );
88 if ( extractedModel != null )
90 Artifact extractedArtifact = artifactFactory.createBuildArtifact( extractedModel.getGroupId(),
91 extractedModel.getArtifactId(),
92 extractedModel.getVersion(),
93 extractedModel.getPackaging() );
94 if ( !repository.pathOf( extractedArtifact ).equals( artifactPath ) )
96 reporter.addFailure( artifact,
97 "The artifact is out of place. It does not match the specified location in the packaged pom." );
104 reporter.addFailure( artifact,
105 "The artifact is out of place. It does not exist at the specified location in the repository pom." );
111 reporter.addFailure( artifact,
112 "The artifact is out of place. It does not match the specified location in the repository pom." );
118 reporter.addSuccess( artifact );
123 * Validate the if the artifact exists in the specified location.
127 private boolean validateArtifactLocation( String filename )
129 return new File( filename ).exists();
133 * Extract the contents of the artifact/jar file.
139 private Model readArtifactModel( File file, String groupId, String artifactId )
140 throws ReportProcessorException
147 jar = new JarFile( file );
149 //Get the entry and its input stream.
150 JarEntry entry = jar.getJarEntry( "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml" );
152 // If the entry is not null, extract it.
155 model = readModel( jar.getInputStream( entry ) );
158 catch ( IOException e )
160 // TODO: should just warn and continue?
161 throw new ReportProcessorException( "Unable to read artifact to extract model", e );
163 catch ( XmlPullParserException e )
165 // TODO: should just warn and continue?
166 throw new ReportProcessorException( "Unable to read artifact to extract model", e );
172 //noinspection UnusedCatchParameter
177 catch ( IOException e )
186 private Model readModel( InputStream entryStream )
187 throws IOException, XmlPullParserException
189 Reader isReader = new InputStreamReader( entryStream );
194 MavenXpp3Reader pomReader = new MavenXpp3Reader();
195 model = pomReader.read( isReader );
199 IOUtil.close( isReader );