View Javadoc

1   package org.apache.maven.plugins.enforcer;
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.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
29  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
30  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
31  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.Repository;
34  import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtils;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
37  import org.codehaus.plexus.util.StringUtils;
38  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
39  
40  /**
41   * This rule checks that this pom or its parents don't define a repository.
42   *
43   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
44   */
45  public class RequireNoRepositories
46      extends AbstractNonCacheableEnforcerRule
47  {
48      /**
49       * Whether to ban non-plugin repositories. By default they are banned.
50       * 
51       * @deprecated the visibility will be reduced to private with the next major version
52       * @see {@link #setBanRepositories(boolean)}
53       */
54      public boolean banRepositories = true;
55  
56      /**
57       * Whether to ban plugin repositories. By default they are banned.
58       * 
59       * @deprecated the visibility will be reduced to private with the next major version
60       * @see {@link #setBanPluginRepositories(boolean)}
61       */
62      public boolean banPluginRepositories = true;
63  
64      /**
65       * Specify explicitly allowed non-plugin repositories. This is a list of ids.
66       * 
67       * @deprecated the visibility will be reduced to private with the next major version
68       * @see {@link #setAllowedRepositories(List)}
69       */
70      public List<String> allowedRepositories = Collections.emptyList();
71  
72      /**
73       * Specify explicitly allowed plugin repositories. This is a list of ids.
74       * 
75       * @deprecated the visibility will be reduced to private with the next major version
76       * @see {@link #setAllowedPluginRepositories(List)}
77       */
78      public List<String> allowedPluginRepositories = Collections.emptyList();
79  
80      /**
81       * Whether to allow repositories which only resolve snapshots. By default they are banned.
82       * 
83       * @deprecated the visibility will be reduced to private with the next major version
84       * @see {@link #setAllowSnapshotRepositories(boolean)}
85       */
86      public boolean allowSnapshotRepositories = false;
87  
88      /**
89       * Whether to allow plugin repositories which only resolve snapshots. By default they are banned.
90       * 
91       * @deprecated the visibility will be reduced to private with the next major version
92       * @see {@link #setAllowSnapshotPluginRepositories(boolean)}
93       */
94      public boolean allowSnapshotPluginRepositories = false;
95  
96      public final void setBanRepositories( boolean banRepositories )
97      {
98          this.banRepositories = banRepositories;
99      }
100     
101     public final void setBanPluginRepositories( boolean banPluginRepositories )
102     {
103         this.banPluginRepositories = banPluginRepositories;
104     }
105     
106     public final void setAllowedRepositories( List<String> allowedRepositories )
107     {
108         this.allowedRepositories = allowedRepositories;
109     }
110     
111     public final void setAllowedPluginRepositories( List<String> allowedPluginRepositories )
112     {
113         this.allowedPluginRepositories = allowedPluginRepositories;
114     }
115     
116     public final void setAllowSnapshotRepositories( boolean allowSnapshotRepositories )
117     {
118         this.allowSnapshotRepositories = allowSnapshotRepositories;
119     }
120     
121     public final void setAllowSnapshotPluginRepositories( boolean allowSnapshotPluginRepositories )
122     {
123         this.allowSnapshotPluginRepositories = allowSnapshotPluginRepositories;
124     }
125     
126     /*
127      * (non-Javadoc)
128      * @see
129      * org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
130      */
131     public void execute( EnforcerRuleHelper helper )
132         throws EnforcerRuleException
133     {
134         EnforcerRuleUtils utils = new EnforcerRuleUtils( helper );
135 
136         MavenProject project;
137         try
138         {
139             project = (MavenProject) helper.evaluate( "${project}" );
140 
141             List<Model> models =
142                 utils.getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(),
143                                             new File( project.getBasedir(), "pom.xml" ) );
144             List<Model> badModels = new ArrayList<Model>();
145 
146             StringBuffer newMsg = new StringBuffer();
147             newMsg.append( "Some poms have repositories defined:\n" );
148 
149             for ( Model model : models )
150             {
151                 if ( banRepositories )
152                 {
153                     @SuppressWarnings( "unchecked" )
154                     List<Repository> repos = model.getRepositories();
155                     if ( repos != null && !repos.isEmpty() )
156                     {
157                         List<String> bannedRepos =
158                             findBannedRepositories( repos, allowedRepositories, allowSnapshotRepositories );
159                         if ( !bannedRepos.isEmpty() )
160                         {
161                             badModels.add( model );
162                             newMsg.append(
163                                 model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion()
164                                     + " has repositories " + bannedRepos );
165                         }
166                     }
167                 }
168                 if ( banPluginRepositories )
169                 {
170                     @SuppressWarnings( "unchecked" )
171                     List<Repository> repos = model.getPluginRepositories();
172                     if ( repos != null && !repos.isEmpty() )
173                     {
174                         List<String> bannedRepos =
175                             findBannedRepositories( repos, allowedPluginRepositories, allowSnapshotPluginRepositories );
176                         if ( !bannedRepos.isEmpty() )
177                         {
178                             badModels.add( model );
179                             newMsg.append(
180                                 model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion()
181                                     + " has plugin repositories " + bannedRepos );
182                         }
183                     }
184                 }
185             }
186 
187             // if anything was found, log it then append the
188             // optional message.
189             if ( !badModels.isEmpty() )
190             {
191                 String message = getMessage();
192                 if ( StringUtils.isNotEmpty( message ) )
193                 {
194                     newMsg.append( message );
195                 }
196 
197                 throw new EnforcerRuleException( newMsg.toString() );
198             }
199 
200         }
201         catch ( ExpressionEvaluationException e )
202         {
203             throw new EnforcerRuleException( e.getLocalizedMessage() );
204         }
205         catch ( ArtifactResolutionException e )
206         {
207             throw new EnforcerRuleException( e.getLocalizedMessage() );
208         }
209         catch ( ArtifactNotFoundException e )
210         {
211             throw new EnforcerRuleException( e.getLocalizedMessage() );
212         }
213         catch ( IOException e )
214         {
215             throw new EnforcerRuleException( e.getLocalizedMessage() );
216         }
217         catch ( XmlPullParserException e )
218         {
219             throw new EnforcerRuleException( e.getLocalizedMessage() );
220         }
221     }
222 
223     /**
224      * 
225      * @param repos all repositories, never {@code null}
226      * @param allowedRepos allowed repositories, never {@code null}
227      * @param allowSnapshots 
228      * @return
229      */
230     private static List<String> findBannedRepositories( List<Repository> repos, List<String> allowedRepos, boolean allowSnapshots )
231     {
232         List<String> bannedRepos = new ArrayList<String>( allowedRepos.size() );
233         for ( Repository r : repos )
234         {
235             if ( !allowedRepos.contains( r.getId() ) )
236             {
237                 if ( !allowSnapshots || r.getReleases() == null || r.getReleases().isEnabled() )
238                 {
239                     // if we are not allowing snapshots and this repo is enabled for releases
240                     // it is banned.  We don't care whether it is enabled for snapshots
241                     // if you define a repo and don't enable it for anything, then we have nothing 
242                     // to worry about
243                     bannedRepos.add( r.getId() );
244                 }
245             }
246         }
247         return bannedRepos;
248     }
249 }