View Javadoc
1   package org.apache.maven.plugins.enforcer.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.maven.artifact.factory.ArtifactFactory;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.resolver.ArtifactResolver;
27  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
28  import org.apache.maven.model.Plugin;
29  import org.apache.maven.model.ReportPlugin;
30  import org.apache.maven.plugin.logging.Log;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
33  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
34  
35  /**
36   * The Class EnforcerRuleUtils.
37   *
38   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39   */
40  public class EnforcerRuleUtils
41  {
42  
43      /** The factory. */
44      ArtifactFactory factory;
45  
46      /** The resolver. */
47      ArtifactResolver resolver;
48  
49      /** The local. */
50      ArtifactRepository local;
51  
52      /** The remote repositories. */
53      List<ArtifactRepository> remoteRepositories;
54  
55      /** The log. */
56      Log log;
57  
58      /** The project. */
59      MavenProject project;
60  
61      private EnforcerRuleHelper helper;
62  
63      /**
64       * Instantiates a new enforcer rule utils.
65       *
66       * @param theFactory the the factory
67       * @param theResolver the the resolver
68       * @param theLocal the the local
69       * @param theRemoteRepositories the the remote repositories
70       * @param project the project
71       * @param theLog the the log
72       */
73      public EnforcerRuleUtils( ArtifactFactory theFactory, ArtifactResolver theResolver, ArtifactRepository theLocal,
74                                List<ArtifactRepository> theRemoteRepositories, MavenProject project, Log theLog )
75      {
76          super();
77          this.factory = theFactory;
78          this.resolver = theResolver;
79          this.local = theLocal;
80          this.remoteRepositories = theRemoteRepositories;
81          this.log = theLog;
82          this.project = project;
83      }
84  
85      /**
86       * Instantiates a new enforcer rule utils.
87       *
88       * @param helper the helper
89       */
90      public EnforcerRuleUtils( EnforcerRuleHelper helper )
91      {
92  
93          this.helper = helper;
94          // get the various expressions out of the
95          // helper.
96          try
97          {
98              factory = (ArtifactFactory) helper.getComponent( ArtifactFactory.class );
99              resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class );
100             local = (ArtifactRepository) helper.evaluate( "${localRepository}" );
101             project = (MavenProject) helper.evaluate( "${project}" );
102             remoteRepositories = project.getRemoteArtifactRepositories();
103         }
104         catch ( ComponentLookupException e )
105         {
106             // TODO Auto-generated catch block
107             e.printStackTrace();
108         }
109         catch ( ExpressionEvaluationException e )
110         {
111             // TODO Auto-generated catch block
112             e.printStackTrace();
113         }
114     }
115 
116     private void resolve( Plugin plugin )
117     {
118         try
119         {
120             plugin.setGroupId( (String) helper.evaluate( plugin.getGroupId() ) );
121             plugin.setArtifactId( (String) helper.evaluate( plugin.getArtifactId() ) );
122             plugin.setVersion( (String) helper.evaluate( plugin.getVersion() ) );
123         }
124         catch ( ExpressionEvaluationException e )
125         {
126             // this should have gone already before
127         }
128     }
129 
130     private void resolve( ReportPlugin plugin )
131     {
132         try
133         {
134             plugin.setGroupId( (String) helper.evaluate( plugin.getGroupId() ) );
135             plugin.setArtifactId( (String) helper.evaluate( plugin.getArtifactId() ) );
136             plugin.setVersion( (String) helper.evaluate( plugin.getVersion() ) );
137         }
138         catch ( ExpressionEvaluationException e )
139         {
140             // this should have gone already before
141         }
142     }
143 
144     public List<Plugin> resolvePlugins( List<Plugin> plugins )
145     {
146         for ( Plugin plugin : plugins )
147         {
148             resolve( plugin );
149         }
150         return plugins;
151     }
152 
153     public List<ReportPlugin> resolveReportPlugins( List<ReportPlugin> reportPlugins )
154     {
155         for ( ReportPlugin plugin : reportPlugins )
156         {
157             resolve( plugin );
158         }
159         return reportPlugins;
160     }
161 
162 }