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 resolver. */
44      ArtifactResolver resolver;
45  
46      /** The local. */
47      ArtifactRepository local;
48  
49      /** The remote repositories. */
50      List<ArtifactRepository> remoteRepositories;
51  
52      /** The log. */
53      Log log;
54  
55      /** The project. */
56      MavenProject project;
57  
58      private EnforcerRuleHelper helper;
59  
60      /**
61       * Instantiates a new enforcer rule utils.
62       *
63       * @param theFactory unused
64       * @param theResolver the the resolver
65       * @param theLocal the the local
66       * @param theRemoteRepositories the the remote repositories
67       * @param project the project
68       * @param theLog the the log
69       */
70      public EnforcerRuleUtils( ArtifactFactory theFactory, ArtifactResolver theResolver, ArtifactRepository theLocal,
71                                List<ArtifactRepository> theRemoteRepositories, MavenProject project, Log theLog )
72      {
73          super();
74          this.resolver = theResolver;
75          this.local = theLocal;
76          this.remoteRepositories = theRemoteRepositories;
77          this.log = theLog;
78          this.project = project;
79      }
80  
81      /**
82       * Instantiates a new enforcer rule utils.
83       *
84       * @param helper the helper
85       */
86      public EnforcerRuleUtils( EnforcerRuleHelper helper )
87      {
88  
89          this.helper = helper;
90          // get the various expressions out of the
91          // helper.
92          try
93          {
94              resolver = helper.getComponent( ArtifactResolver.class );
95              local = (ArtifactRepository) helper.evaluate( "${localRepository}" );
96              project = (MavenProject) helper.evaluate( "${project}" );
97              remoteRepositories = project.getRemoteArtifactRepositories();
98          }
99          catch ( ComponentLookupException | ExpressionEvaluationException e )
100         {
101             // TODO Auto-generated catch block
102             e.printStackTrace();
103         }
104     }
105 
106     private void resolve( Plugin plugin )
107     {
108         try
109         {
110             plugin.setGroupId( (String) helper.evaluate( plugin.getGroupId() ) );
111             plugin.setArtifactId( (String) helper.evaluate( plugin.getArtifactId() ) );
112             plugin.setVersion( (String) helper.evaluate( plugin.getVersion() ) );
113         }
114         catch ( ExpressionEvaluationException e )
115         {
116             // this should have gone already before
117         }
118     }
119 
120     private void resolve( ReportPlugin plugin )
121     {
122         try
123         {
124             plugin.setGroupId( (String) helper.evaluate( plugin.getGroupId() ) );
125             plugin.setArtifactId( (String) helper.evaluate( plugin.getArtifactId() ) );
126             plugin.setVersion( (String) helper.evaluate( plugin.getVersion() ) );
127         }
128         catch ( ExpressionEvaluationException e )
129         {
130             // this should have gone already before
131         }
132     }
133 
134     public List<Plugin> resolvePlugins( List<Plugin> plugins )
135     {
136         for ( Plugin plugin : plugins )
137         {
138             resolve( plugin );
139         }
140         return plugins;
141     }
142 
143     public List<ReportPlugin> resolveReportPlugins( List<ReportPlugin> reportPlugins )
144     {
145         for ( ReportPlugin plugin : reportPlugins )
146         {
147             resolve( plugin );
148         }
149         return reportPlugins;
150     }
151 
152 }