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";
67 * Check whether the artifact is in its proper location. The location of the artifact
68 * is validated first against the groupId, artifactId and versionId in the specified model
69 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
70 * included in the package. If a model exists inside the package, then check if the artifact's
71 * location is valid based on the location specified in the pom. Check if the both the location
72 * specified in the file system pom and in the pom included in the package is the same.
74 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
76 ArtifactRepository repository = artifact.getRepository();
78 if ( !"file".equals( repository.getProtocol() ) )
80 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
81 throw new UnsupportedOperationException(
82 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
85 adjustDistributionArtifactHandler( artifact );
87 String artifactPath = repository.pathOf( artifact );
91 // only check if it is a standalone POM, or an artifact other than a POM
92 // ie, don't check the location of the POM for another artifact matches that of the artifact
93 if ( !POM.equals( artifact.getType() ) || POM.equals( model.getPackaging() ) )
95 //check if the artifact is located in its proper location based on the info
96 //specified in the model object/pom
97 Artifact modelArtifact = artifactFactory.createArtifactWithClassifier( model.getGroupId(),
98 model.getArtifactId(),
101 artifact.getClassifier() );
103 adjustDistributionArtifactHandler( modelArtifact );
104 String modelPath = repository.pathOf( modelArtifact );
105 if ( !modelPath.equals( artifactPath ) )
107 reporter.addFailure( artifact,
108 "The artifact is out of place. It does not match the specified location in the repository pom: " +
114 //get the location of the artifact itself
115 File file = new File( repository.getBasedir(), artifactPath );
119 if ( JAR_FILE_TYPES.contains( artifact.getType() ) )
121 //unpack the artifact (using the groupId, artifactId & version specified in the artifact object itself
122 //check if the pom is included in the package
123 Model extractedModel = readArtifactModel( file, artifact, reporter );
125 if ( extractedModel != null )
127 Artifact extractedArtifact = artifactFactory.createBuildArtifact( extractedModel.getGroupId(),
128 extractedModel.getArtifactId(),
129 extractedModel.getVersion(),
130 extractedModel.getPackaging() );
131 if ( !repository.pathOf( extractedArtifact ).equals( artifactPath ) )
133 reporter.addFailure( artifact,
134 "The artifact is out of place. It does not match the specified location in the packaged pom." );
141 throw new IllegalStateException( "Couldn't find artifact " + file );
145 private static void adjustDistributionArtifactHandler( Artifact artifact )
147 // need to tweak these as they aren't currently in the known type converters. TODO - add them in Maven
148 if ( "distribution-zip".equals( artifact.getType() ) )
150 artifact.setArtifactHandler( new DefaultArtifactHandler( "zip" ) );
152 else if ( "distribution-tgz".equals( artifact.getType() ) )
154 artifact.setArtifactHandler( new DefaultArtifactHandler( "tar.gz" ) );
158 private Model readArtifactModel( File file, Artifact artifact, ReportingDatabase reporter )
165 jar = new JarFile( file );
167 //Get the entry and its input stream.
168 JarEntry entry = jar.getJarEntry(
169 "META-INF/maven/" + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/pom.xml" );
171 // If the entry is not null, extract it.
174 model = readModel( jar.getInputStream( entry ) );
176 if ( model.getGroupId() == null )
178 model.setGroupId( model.getParent().getGroupId() );
180 if ( model.getVersion() == null )
182 model.setVersion( model.getParent().getVersion() );
186 catch ( IOException e )
188 reporter.addWarning( artifact, "Unable to read artifact to extract model: " + e );
190 catch ( XmlPullParserException e )
192 reporter.addWarning( artifact, "Unable to parse extracted model: " + e );
198 //noinspection UnusedCatchParameter
203 catch ( IOException e )
212 private Model readModel( InputStream entryStream )
213 throws IOException, XmlPullParserException
215 Reader isReader = new InputStreamReader( entryStream );
220 MavenXpp3Reader pomReader = new MavenXpp3Reader();
221 model = pomReader.read( isReader );
225 IOUtil.close( isReader );