~ubuntu-branches/ubuntu/wily/maven-enforcer/wily

« back to all changes in this revision

Viewing changes to enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Gabriele Giacone
  • Date: 2010-03-01 02:28:06 UTC
  • Revision ID: james.westby@ubuntu.com-20100301022806-b6rx5ndd26peklkr
Tags: upstream-1.0~beta2
ImportĀ upstreamĀ versionĀ 1.0~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
9
 *
 
10
 *  http://www.apache.org/licenses/LICENSE-2.0
 
11
 *
 
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
 
17
 * under the License.
 
18
 */
 
19
package org.apache.maven.plugins.enforcer.utils;
 
20
 
 
21
import java.io.File;
 
22
import java.io.IOException;
 
23
import java.io.Reader;
 
24
import java.util.ArrayList;
 
25
import java.util.List;
 
26
 
 
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;
 
44
 
 
45
// TODO: Auto-generated Javadoc
 
46
/**
 
47
 * The Class EnforcerRuleUtils.
 
48
 * 
 
49
 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 
50
 */
 
51
public class EnforcerRuleUtils
 
52
{
 
53
    
 
54
    /** The factory. */
 
55
    ArtifactFactory factory;
 
56
 
 
57
    /** The resolver. */
 
58
    ArtifactResolver resolver;
 
59
 
 
60
    /** The local. */
 
61
    ArtifactRepository local;
 
62
 
 
63
    /** The remote repositories. */
 
64
    List remoteRepositories;
 
65
 
 
66
    /** The log. */
 
67
    Log log;
 
68
 
 
69
    /** The project. */
 
70
    MavenProject project;
 
71
 
 
72
    private EnforcerRuleHelper helper;
 
73
 
 
74
    /**
 
75
     * Instantiates a new enforcer rule utils.
 
76
     * 
 
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
 
83
     */
 
84
    public EnforcerRuleUtils( ArtifactFactory theFactory, ArtifactResolver theResolver, ArtifactRepository theLocal,
 
85
                              List theRemoteRepositories, MavenProject project, Log theLog )
 
86
    {
 
87
        super();
 
88
        this.factory = theFactory;
 
89
        this.resolver = theResolver;
 
90
        this.local = theLocal;
 
91
        this.remoteRepositories = theRemoteRepositories;
 
92
        this.log = theLog;
 
93
        this.project = project;
 
94
    }
 
95
 
 
96
    /**
 
97
     * Instantiates a new enforcer rule utils.
 
98
     * 
 
99
     * @param helper the helper
 
100
     */
 
101
    public EnforcerRuleUtils( EnforcerRuleHelper helper )
 
102
    {
 
103
     
 
104
        this.helper = helper;
 
105
        // get the various expressions out of the
 
106
        // helper.
 
107
        try
 
108
        {
 
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();
 
114
        }
 
115
        catch ( ComponentLookupException e )
 
116
        {
 
117
            // TODO Auto-generated catch block
 
118
            e.printStackTrace();
 
119
        }
 
120
        catch ( ExpressionEvaluationException e )
 
121
        {
 
122
            // TODO Auto-generated catch block
 
123
            e.printStackTrace();
 
124
        }
 
125
    }
 
126
 
 
127
    /**
 
128
     * Gets the pom model for this file.
 
129
     * 
 
130
     * @param pom the pom
 
131
     * 
 
132
     * @return the model
 
133
     * 
 
134
     * @throws IOException Signals that an I/O exception has occurred.
 
135
     * @throws XmlPullParserException the xml pull parser exception
 
136
     */
 
137
    private Model readModel ( File pom )
 
138
        throws IOException, XmlPullParserException
 
139
    {
 
140
        Reader reader = ReaderFactory.newXmlReader( pom );
 
141
        MavenXpp3Reader xpp3 = new MavenXpp3Reader();
 
142
        Model model = null;
 
143
        try
 
144
        {
 
145
            model = xpp3.read( reader );
 
146
        }
 
147
        finally
 
148
        {
 
149
            reader.close();
 
150
            reader = null;
 
151
        }
 
152
        return model;
 
153
    }
 
154
 
 
155
    /**
 
156
     * This method gets the model for the defined artifact.
 
157
     * Looks first in the filesystem, then tries to get it
 
158
     * from the repo.
 
159
     * 
 
160
     * @param groupId the group id
 
161
     * @param artifactId the artifact id
 
162
     * @param version the version
 
163
     * @param pom the pom
 
164
     * 
 
165
     * @return the pom model
 
166
     * 
 
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.
 
171
     */
 
172
    private Model getPomModel ( String groupId, String artifactId, String version, File pom )
 
173
        throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
 
174
    {
 
175
        Model model = null;
 
176
 
 
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
 
181
        // other
 
182
        // rules. (MNG-2277)
 
183
 
 
184
        // look in the location specified by pom first.
 
185
        boolean found = false;
 
186
        try
 
187
        {
 
188
            model = readModel( pom );
 
189
 
 
190
            // i found a model, lets make sure it's the one
 
191
            // I want
 
192
            found = checkIfModelMatches( groupId, artifactId, version, model );
 
193
        }
 
194
        catch ( IOException e )
 
195
        {
 
196
            // nothing here, but lets look in the repo
 
197
            // before giving up.
 
198
        }
 
199
        catch ( XmlPullParserException e )
 
200
        {
 
201
            // nothing here, but lets look in the repo
 
202
            // before giving up.
 
203
        }
 
204
 
 
205
        // i didn't find it in the local file system, go
 
206
        // look in the repo
 
207
        if ( !found )
 
208
        {
 
209
            Artifact pomArtifact = factory.createArtifact( groupId, artifactId, version, null, "pom" );
 
210
            resolver.resolve( pomArtifact, remoteRepositories, local );
 
211
            model = readModel( pomArtifact.getFile() );
 
212
        }
 
213
 
 
214
        return model;
 
215
    }
 
216
 
 
217
    /**
 
218
     * This method loops through all the parents, getting
 
219
     * each pom model and then its parent.
 
220
     * 
 
221
     * @param groupId the group id
 
222
     * @param artifactId the artifact id
 
223
     * @param version the version
 
224
     * @param pom the pom
 
225
     * 
 
226
     * @return the models recursively
 
227
     * 
 
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
 
232
     */
 
233
    public List getModelsRecursively ( String groupId, String artifactId, String version, File pom )
 
234
        throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
 
235
    {
 
236
        List models = null;
 
237
        Model model = getPomModel( groupId, artifactId, version, pom );
 
238
 
 
239
        Parent parent = model.getParent();
 
240
 
 
241
        // recurse into the parent
 
242
        if ( parent != null )
 
243
        {
 
244
            // get the relative path
 
245
            String relativePath = parent.getRelativePath();
 
246
            if ( StringUtils.isEmpty( relativePath ) )
 
247
            {
 
248
                relativePath = "../pom.xml";
 
249
            }
 
250
            // calculate the recursive path
 
251
            File parentPom = new File( pom.getParent(), relativePath );
 
252
            
 
253
            // if relative path is a directory, append pom.xml
 
254
            if ( parentPom.isDirectory() )
 
255
            {
 
256
                parentPom = new File( parentPom, "pom.xml" );
 
257
            }
 
258
 
 
259
            models = getModelsRecursively( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), parentPom );
 
260
        }
 
261
        else
 
262
        {
 
263
            // only create it here since I'm not at the top
 
264
            models = new ArrayList();
 
265
        }
 
266
        models.add( model );
 
267
 
 
268
        return models;
 
269
    }
 
270
 
 
271
    /**
 
272
     * Make sure the model is the one I'm expecting.
 
273
     * 
 
274
     * @param groupId the group id
 
275
     * @param artifactId the artifact id
 
276
     * @param version the version
 
277
     * @param model Model being checked.
 
278
     * 
 
279
     * @return true, if check if model matches
 
280
     */
 
281
    protected boolean checkIfModelMatches ( String groupId, String artifactId, String version, Model model )
 
282
    {
 
283
        // try these first.
 
284
        String modelGroup = model.getGroupId();
 
285
        String modelVersion = model.getVersion();
 
286
 
 
287
        try
 
288
        {
 
289
            if ( StringUtils.isEmpty( modelGroup ) )
 
290
            {
 
291
                modelGroup = model.getParent().getGroupId();
 
292
            }
 
293
            else
 
294
            {
 
295
                //MENFORCER-30, handle cases where the value is a property like ${project.parent.groupId}
 
296
                modelGroup = (String) helper.evaluate( modelGroup );
 
297
            }
 
298
 
 
299
            if ( StringUtils.isEmpty( modelVersion ) )
 
300
            {
 
301
                modelVersion = model.getParent().getVersion();
 
302
            }
 
303
            else
 
304
            {
 
305
                //MENFORCER-30, handle cases where the value is a property like ${project.parent.version}
 
306
                modelVersion = (String) helper.evaluate( modelVersion );
 
307
            }
 
308
        }
 
309
        catch ( NullPointerException e )
 
310
        {
 
311
            // this is probably bad. I don't have a valid
 
312
            // group or version and I can't find a
 
313
            // parent????
 
314
            // lets see if it's what we're looking for
 
315
            // anyway.
 
316
        }
 
317
        catch ( ExpressionEvaluationException e )
 
318
        {
 
319
            // as above
 
320
        }
 
321
        return ( StringUtils.equals( groupId, modelGroup ) && StringUtils.equals( version, modelVersion ) && StringUtils
 
322
            .equals( artifactId, model.getArtifactId() ) );
 
323
    }
 
324
}