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