2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the
7
* "License"); you may not use this file except in compliance
8
* with the License. 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,
13
* software distributed under the License is distributed on an
14
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
* KIND, either express or implied. See the License for the
16
* specific language governing permissions and limitations
19
package org.apache.maven.plugins.enforcer.utils;
22
import java.io.IOException;
23
import java.io.Reader;
24
import java.util.ArrayList;
25
import java.util.List;
27
import org.apache.maven.artifact.Artifact;
28
import org.apache.maven.artifact.factory.ArtifactFactory;
29
import org.apache.maven.artifact.repository.ArtifactRepository;
30
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
31
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
32
import org.apache.maven.artifact.resolver.ArtifactResolver;
33
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
34
import org.apache.maven.model.Model;
35
import org.apache.maven.model.Parent;
36
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
37
import org.apache.maven.plugin.logging.Log;
38
import org.apache.maven.project.MavenProject;
39
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
40
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
41
import org.codehaus.plexus.util.ReaderFactory;
42
import org.codehaus.plexus.util.StringUtils;
43
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
45
// TODO: Auto-generated Javadoc
47
* The Class EnforcerRuleUtils.
49
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
51
public class EnforcerRuleUtils
55
ArtifactFactory factory;
58
ArtifactResolver resolver;
61
ArtifactRepository local;
63
/** The remote repositories. */
64
List remoteRepositories;
72
private EnforcerRuleHelper helper;
75
* Instantiates a new enforcer rule utils.
77
* @param theFactory the the factory
78
* @param theResolver the the resolver
79
* @param theLocal the the local
80
* @param theRemoteRepositories the the remote repositories
81
* @param project the project
82
* @param theLog the the log
84
public EnforcerRuleUtils( ArtifactFactory theFactory, ArtifactResolver theResolver, ArtifactRepository theLocal,
85
List theRemoteRepositories, MavenProject project, Log theLog )
88
this.factory = theFactory;
89
this.resolver = theResolver;
90
this.local = theLocal;
91
this.remoteRepositories = theRemoteRepositories;
93
this.project = project;
97
* Instantiates a new enforcer rule utils.
99
* @param helper the helper
101
public EnforcerRuleUtils( EnforcerRuleHelper helper )
104
this.helper = helper;
105
// get the various expressions out of the
109
factory = (ArtifactFactory) helper.getComponent( ArtifactFactory.class );
110
resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class );
111
local = (ArtifactRepository) helper.evaluate( "${localRepository}" );
112
project = (MavenProject) helper.evaluate( "${project}" );
113
remoteRepositories = project.getRemoteArtifactRepositories();
115
catch ( ComponentLookupException e )
117
// TODO Auto-generated catch block
120
catch ( ExpressionEvaluationException e )
122
// TODO Auto-generated catch block
128
* Gets the pom model for this file.
134
* @throws IOException Signals that an I/O exception has occurred.
135
* @throws XmlPullParserException the xml pull parser exception
137
private Model readModel ( File pom )
138
throws IOException, XmlPullParserException
140
Reader reader = ReaderFactory.newXmlReader( pom );
141
MavenXpp3Reader xpp3 = new MavenXpp3Reader();
145
model = xpp3.read( reader );
156
* This method gets the model for the defined artifact.
157
* Looks first in the filesystem, then tries to get it
160
* @param groupId the group id
161
* @param artifactId the artifact id
162
* @param version the version
165
* @return the pom model
167
* @throws ArtifactResolutionException the artifact resolution exception
168
* @throws ArtifactNotFoundException the artifact not found exception
169
* @throws XmlPullParserException the xml pull parser exception
170
* @throws IOException Signals that an I/O exception has occurred.
172
private Model getPomModel ( String groupId, String artifactId, String version, File pom )
173
throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
177
// do we want to look in the reactor like the
178
// project builder? Would require @aggregator goal
179
// which causes problems in maven core right now
180
// because we also need dependency resolution in
184
// look in the location specified by pom first.
185
boolean found = false;
188
model = readModel( pom );
190
// i found a model, lets make sure it's the one
192
found = checkIfModelMatches( groupId, artifactId, version, model );
194
catch ( IOException e )
196
// nothing here, but lets look in the repo
199
catch ( XmlPullParserException e )
201
// nothing here, but lets look in the repo
205
// i didn't find it in the local file system, go
209
Artifact pomArtifact = factory.createArtifact( groupId, artifactId, version, null, "pom" );
210
resolver.resolve( pomArtifact, remoteRepositories, local );
211
model = readModel( pomArtifact.getFile() );
218
* This method loops through all the parents, getting
219
* each pom model and then its parent.
221
* @param groupId the group id
222
* @param artifactId the artifact id
223
* @param version the version
226
* @return the models recursively
228
* @throws ArtifactResolutionException the artifact resolution exception
229
* @throws ArtifactNotFoundException the artifact not found exception
230
* @throws IOException Signals that an I/O exception has occurred.
231
* @throws XmlPullParserException the xml pull parser exception
233
public List getModelsRecursively ( String groupId, String artifactId, String version, File pom )
234
throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
237
Model model = getPomModel( groupId, artifactId, version, pom );
239
Parent parent = model.getParent();
241
// recurse into the parent
242
if ( parent != null )
244
// get the relative path
245
String relativePath = parent.getRelativePath();
246
if ( StringUtils.isEmpty( relativePath ) )
248
relativePath = "../pom.xml";
250
// calculate the recursive path
251
File parentPom = new File( pom.getParent(), relativePath );
253
// if relative path is a directory, append pom.xml
254
if ( parentPom.isDirectory() )
256
parentPom = new File( parentPom, "pom.xml" );
259
models = getModelsRecursively( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), parentPom );
263
// only create it here since I'm not at the top
264
models = new ArrayList();
272
* Make sure the model is the one I'm expecting.
274
* @param groupId the group id
275
* @param artifactId the artifact id
276
* @param version the version
277
* @param model Model being checked.
279
* @return true, if check if model matches
281
protected boolean checkIfModelMatches ( String groupId, String artifactId, String version, Model model )
284
String modelGroup = model.getGroupId();
285
String modelVersion = model.getVersion();
289
if ( StringUtils.isEmpty( modelGroup ) )
291
modelGroup = model.getParent().getGroupId();
295
//MENFORCER-30, handle cases where the value is a property like ${project.parent.groupId}
296
modelGroup = (String) helper.evaluate( modelGroup );
299
if ( StringUtils.isEmpty( modelVersion ) )
301
modelVersion = model.getParent().getVersion();
305
//MENFORCER-30, handle cases where the value is a property like ${project.parent.version}
306
modelVersion = (String) helper.evaluate( modelVersion );
309
catch ( NullPointerException e )
311
// this is probably bad. I don't have a valid
312
// group or version and I can't find a
314
// lets see if it's what we're looking for
317
catch ( ExpressionEvaluationException e )
321
return ( StringUtils.equals( groupId, modelGroup ) && StringUtils.equals( version, modelVersion ) && StringUtils
322
.equals( artifactId, model.getArtifactId() ) );