1 package org.apache.maven.archiva.reporting.processor;
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.apache.maven.archiva.reporting.processor.ArtifactReportProcessor;
27 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
28 import org.apache.commons.io.IOUtils;
29 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.Reader;
36 import java.util.Arrays;
37 import java.util.HashSet;
39 import java.util.jar.JarEntry;
40 import java.util.jar.JarFile;
43 * Validate the location of the artifact based on the values indicated
44 * in its pom (both the pom packaged with the artifact & the pom in the
47 * @plexus.component role="org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor" role-hint="artifact-location"
49 public class LocationArtifactReportProcessor
50 implements ArtifactReportProcessor
55 private ArtifactFactory artifactFactory;
57 // TODO: share with other code with the same
58 private static final Set JAR_FILE_TYPES =
59 new HashSet( Arrays.asList( new String[]{"jar", "war", "par", "ejb", "ear", "rar", "sar"} ) );
64 private MavenProjectBuilder projectBuilder;
66 private static final String POM = "pom";
68 private static final String ROLE_HINT = "artifact-location";
71 * Check whether the artifact is in its proper location. The location of the artifact
72 * is validated first against the groupId, artifactId and versionId in the specified model
73 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
74 * included in the package. If a model exists inside the package, then check if the artifact's
75 * location is valid based on the location specified in the pom. Check if the both the location
76 * specified in the file system pom and in the pom included in the package is the same.
78 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
80 ArtifactRepository repository = artifact.getRepository();
82 if ( !"file".equals( repository.getProtocol() ) )
84 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
85 throw new UnsupportedOperationException(
86 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
89 adjustDistributionArtifactHandler( artifact );
91 String artifactPath = repository.pathOf( artifact );
95 // only check if it is a standalone POM, or an artifact other than a POM
96 // ie, don't check the location of the POM for another artifact matches that of the artifact
97 if ( !POM.equals( artifact.getType() ) || POM.equals( model.getPackaging() ) )
99 //check if the artifact is located in its proper location based on the info
100 //specified in the model object/pom
101 Artifact modelArtifact = artifactFactory.createArtifactWithClassifier( model.getGroupId(),
102 model.getArtifactId(),
105 artifact.getClassifier() );
107 adjustDistributionArtifactHandler( modelArtifact );
108 String modelPath = repository.pathOf( modelArtifact );
109 if ( !modelPath.equals( artifactPath ) )
111 addFailure( reporter, artifact, "repository-pom-location",
112 "The artifact is out of place. It does not match the specified location in the repository pom: " +
118 // get the location of the artifact itself
119 File file = new File( repository.getBasedir(), artifactPath );
123 if ( JAR_FILE_TYPES.contains( artifact.getType() ) )
125 //unpack the artifact (using the groupId, artifactId & version specified in the artifact object itself
126 //check if the pom is included in the package
127 Model extractedModel = readArtifactModel( file, artifact, reporter );
129 if ( extractedModel != null )
131 Artifact extractedArtifact = artifactFactory.createBuildArtifact( extractedModel.getGroupId(),
132 extractedModel.getArtifactId(),
133 extractedModel.getVersion(),
134 extractedModel.getPackaging() );
135 if ( !repository.pathOf( extractedArtifact ).equals( artifactPath ) )
137 addFailure( reporter, artifact, "packaged-pom-location",
138 "The artifact is out of place. It does not match the specified location in the packaged pom." );
145 addFailure( reporter, artifact, "missing-artifact", "The artifact file [" + file + "] cannot be found for metadata." );
149 private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
151 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
152 reporter.addFailure( artifact, ROLE_HINT, problem, reason );
155 private static void adjustDistributionArtifactHandler( Artifact artifact )
157 // need to tweak these as they aren't currently in the known type converters. TODO - add them in Maven
158 if ( "distribution-zip".equals( artifact.getType() ) )
160 artifact.setArtifactHandler( new DefaultArtifactHandler( "zip" ) );
162 else if ( "distribution-tgz".equals( artifact.getType() ) )
164 artifact.setArtifactHandler( new DefaultArtifactHandler( "tar.gz" ) );
168 private Model readArtifactModel( File file, Artifact artifact, ReportingDatabase reporter )
175 jar = new JarFile( file );
177 //Get the entry and its input stream.
178 JarEntry entry = jar.getJarEntry(
179 "META-INF/maven/" + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/pom.xml" );
181 // If the entry is not null, extract it.
184 model = readModel( jar.getInputStream( entry ) );
186 if ( model.getGroupId() == null )
188 model.setGroupId( model.getParent().getGroupId() );
190 if ( model.getVersion() == null )
192 model.setVersion( model.getParent().getVersion() );
196 catch ( IOException e )
198 addWarning( reporter, artifact, "Unable to read artifact to extract model: " + e );
200 catch ( XmlPullParserException e )
202 addWarning( reporter, artifact, "Unable to parse extracted model: " + e );
208 //noinspection UnusedCatchParameter
213 catch ( IOException e )
222 private static void addWarning( ReportingDatabase reporter, Artifact artifact, String reason )
224 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
225 reporter.addWarning( artifact, ROLE_HINT, null, reason );
228 private Model readModel( InputStream entryStream )
229 throws IOException, XmlPullParserException
231 Reader isReader = new InputStreamReader( entryStream );
236 MavenXpp3Reader pomReader = new MavenXpp3Reader();
237 model = pomReader.read( isReader );
241 IOUtils.closeQuietly( isReader );