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.repository.ArtifactRepository;
21 import org.apache.maven.model.Model;
22 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
23 import org.codehaus.plexus.util.FileUtils;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
33 import java.util.jar.JarEntry;
34 import java.util.jar.JarFile;
37 * Validate the location of the artifact based on the values indicated
38 * in its pom (both the pom packaged with the artifact & the pom in the
41 public class LocationArtifactReportProcessor
42 implements ArtifactReportProcessor
44 private boolean isLocal = true;
46 private InputStream is;
49 * Check whether the artifact is in its proper location. The location of the artifact
50 * is validated first against the groupId, artifactId and versionId in the specified model
51 * object (pom in the file system). Then unpack the artifact (jar file) and get the model (pom)
52 * included in the package. If a model exists inside the package, then check if the artifact's
53 * location is valid based on the location specified in the pom. Check if the both the location
54 * specified in the file system pom and in the pom included in the package is the same.
56 * @param model Represents the pom in the file system.
61 public void processArtifact( Model model, Artifact artifact, ArtifactReporter reporter,
62 ArtifactRepository repository )
64 boolean fsPomLocation = false, pkgPomLocation = false;
65 String repositoryUrl = "", modelArtifactLocation = "";
67 if ( !repository.getProtocol().equals( "file" ) )
70 repositoryUrl = repository.getUrl();
74 repositoryUrl = repository.getBasedir();
77 //check if the artifact is located in its proper location based on the info
78 //specified in the model object/pom
79 modelArtifactLocation = repositoryUrl + model.getGroupId() + "/" + model.getArtifactId() + "/" +
80 model.getVersion() + "/" + model.getArtifactId() + "-" + model.getVersion() + "." + model.getPackaging();
81 fsPomLocation = validateArtifactLocation( modelArtifactLocation );
83 //get the location of the artifact itself
84 String artifactLocation = repositoryUrl + artifact.getGroupId() + "/" + artifact.getArtifactId() + "/" +
85 artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." +
88 //unpack the artifact (using the groupId, artifactId & version specified in the artifact object itself
89 //check if the pom is included in the package
90 Model extractedModel =
91 unpackArtifact( artifactLocation, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
93 if ( extractedModel != null )
96 String pkgPomArtifactLocation = repositoryUrl + extractedModel.getGroupId() + "/" +
97 extractedModel.getArtifactId() + "/" + extractedModel.getVersion() + "/" +
98 extractedModel.getArtifactId() + "-" + extractedModel.getVersion() + "." +
99 extractedModel.getPackaging();
100 pkgPomLocation = validateArtifactLocation( pkgPomArtifactLocation );
102 //check the conditions
103 if ( fsPomLocation == true && pkgPomLocation == true )
105 reporter.addSuccess( artifact );
108 else if ( fsPomLocation == false && pkgPomLocation == true )
111 .addFailure( artifact,
112 "The artifact is out of place. It does not match the specified location in the file system pom." );
115 else if ( fsPomLocation == true && pkgPomLocation == false )
118 .addFailure( artifact,
119 "The artifact is out of place. It does not match the specified location in the packaged pom." );
122 else if ( fsPomLocation == false && pkgPomLocation == false )
124 reporter.addFailure( artifact, "The artifact is out of place." );
133 reporter.addSuccess( artifact );
138 reporter.addFailure( artifact, "The artifact is out of place." );
144 * Validate the if the artifact exists in the specified location.
149 private boolean validateArtifactLocation( String filename )
155 is = new FileInputStream( filename );
159 URL url = new URL( filename );
160 is = url.openStream();
165 catch ( Exception e )
173 * Extract the contents of the artifact/jar file.
180 private Model unpackArtifact( String filename, String groupId, String artifactId, String version )
183 Model modelObj = null;
185 basedir = System.getProperty( "basedir" );
186 File f = new File( basedir + "/" + "temp" );
187 boolean b = f.mkdirs();
191 JarFile jar = new JarFile( filename );
195 //Get the entry and its input stream.
196 JarEntry entry = jar.getJarEntry( "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml" );
198 // If the entry is not null, extract it.
201 InputStream entryStream = jar.getInputStream( entry );
206 //Create the output file (clobbering the file if it exists).
207 FileOutputStream file = new FileOutputStream( basedir + "/temp/pom.xml" );
211 byte[] buffer = new byte[1024];
214 while ( ( bytesRead = entryStream.read( buffer ) ) != -1 )
216 file.write( buffer, 0, bytesRead );
224 InputStream inputStream = new FileInputStream( basedir + "/temp/pom.xml" );
225 Reader isReader = new InputStreamReader( inputStream );
229 MavenXpp3Reader pomReader = new MavenXpp3Reader();
230 modelObj = pomReader.read( isReader );
256 catch ( Exception e )
265 FileUtils.deleteDirectory( new File( basedir + "/temp" ) );
267 catch ( IOException ie )