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.handler.DefaultArtifactHandler;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.model.Model;
24 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
25 import org.apache.maven.project.MavenProjectBuilder;
26 import org.codehaus.plexus.util.IOUtil;
27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.Reader;
34 import java.util.Arrays;
35 import java.util.HashSet;
37 import java.util.jar.JarEntry;
38 import java.util.jar.JarFile;
41 * Validate the location of the artifact based on the values indicated
42 * in its pom (both the pom packaged with the artifact & the pom in the
45 * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="artifact-location"
47 public class LocationArtifactReportProcessor
48 implements ArtifactReportProcessor
53 private ArtifactFactory artifactFactory;
55 // TODO: share with other code with the same
56 private static final Set JAR_FILE_TYPES =
57 new HashSet( Arrays.asList( new String[]{"jar", "war", "par", "ejb", "ear", "rar", "sar"} ) );
62 private MavenProjectBuilder projectBuilder;
64 private static final String POM = "pom";
66 private static final String ROLE_HINT = "artifact-location";
69 * Check whether the artifact is in its proper location. The location of the artifact
70 * is validated first against the groupId, artifactId and versionId in the specified model
71 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
72 * included in the package. If a model exists inside the package, then check if the artifact's
73 * location is valid based on the location specified in the pom. Check if the both the location
74 * specified in the file system pom and in the pom included in the package is the same.
76 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
78 ArtifactRepository repository = artifact.getRepository();
80 if ( !"file".equals( repository.getProtocol() ) )
82 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
83 throw new UnsupportedOperationException(
84 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
87 adjustDistributionArtifactHandler( artifact );
89 String artifactPath = repository.pathOf( artifact );
93 // only check if it is a standalone POM, or an artifact other than a POM
94 // ie, don't check the location of the POM for another artifact matches that of the artifact
95 if ( !POM.equals( artifact.getType() ) || POM.equals( model.getPackaging() ) )
97 //check if the artifact is located in its proper location based on the info
98 //specified in the model object/pom
99 Artifact modelArtifact = artifactFactory.createArtifactWithClassifier( model.getGroupId(),
100 model.getArtifactId(),
103 artifact.getClassifier() );
105 adjustDistributionArtifactHandler( modelArtifact );
106 String modelPath = repository.pathOf( modelArtifact );
107 if ( !modelPath.equals( artifactPath ) )
109 addFailure( reporter, artifact, "repository-pom-location",
110 "The artifact is out of place. It does not match the specified location in the repository pom: " +
116 // get the location of the artifact itself
117 File file = new File( repository.getBasedir(), artifactPath );
121 if ( JAR_FILE_TYPES.contains( artifact.getType() ) )
123 //unpack the artifact (using the groupId, artifactId & version specified in the artifact object itself
124 //check if the pom is included in the package
125 Model extractedModel = readArtifactModel( file, artifact, reporter );
127 if ( extractedModel != null )
129 Artifact extractedArtifact = artifactFactory.createBuildArtifact( extractedModel.getGroupId(),
130 extractedModel.getArtifactId(),
131 extractedModel.getVersion(),
132 extractedModel.getPackaging() );
133 if ( !repository.pathOf( extractedArtifact ).equals( artifactPath ) )
135 addFailure( reporter, artifact, "packaged-pom-location",
136 "The artifact is out of place. It does not match the specified location in the packaged pom." );
143 addFailure( reporter, artifact, "missing-artifact", "The artifact file [" + file + "] cannot be found for metadata." );
147 private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
149 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
150 reporter.addFailure( artifact, ROLE_HINT, problem, reason );
153 private static void adjustDistributionArtifactHandler( Artifact artifact )
155 // need to tweak these as they aren't currently in the known type converters. TODO - add them in Maven
156 if ( "distribution-zip".equals( artifact.getType() ) )
158 artifact.setArtifactHandler( new DefaultArtifactHandler( "zip" ) );
160 else if ( "distribution-tgz".equals( artifact.getType() ) )
162 artifact.setArtifactHandler( new DefaultArtifactHandler( "tar.gz" ) );
166 private Model readArtifactModel( File file, Artifact artifact, ReportingDatabase reporter )
173 jar = new JarFile( file );
175 //Get the entry and its input stream.
176 JarEntry entry = jar.getJarEntry(
177 "META-INF/maven/" + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/pom.xml" );
179 // If the entry is not null, extract it.
182 model = readModel( jar.getInputStream( entry ) );
184 if ( model.getGroupId() == null )
186 model.setGroupId( model.getParent().getGroupId() );
188 if ( model.getVersion() == null )
190 model.setVersion( model.getParent().getVersion() );
194 catch ( IOException e )
196 addWarning( reporter, artifact, "Unable to read artifact to extract model: " + e );
198 catch ( XmlPullParserException e )
200 addWarning( reporter, artifact, "Unable to parse extracted model: " + e );
206 //noinspection UnusedCatchParameter
211 catch ( IOException e )
220 private static void addWarning( ReportingDatabase reporter, Artifact artifact, String reason )
222 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
223 reporter.addWarning( artifact, ROLE_HINT, null, reason );
226 private Model readModel( InputStream entryStream )
227 throws IOException, XmlPullParserException
229 Reader isReader = new InputStreamReader( entryStream );
234 MavenXpp3Reader pomReader = new MavenXpp3Reader();
235 model = pomReader.read( isReader );
239 IOUtil.close( isReader );