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 #setBanRepositories(boolean)
52       */
53      private boolean banRepositories = true;
54  
55      /**
56       * Whether to ban plugin repositories. By default they are banned.
57       * 
58       * @see #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 #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 #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 #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     @Override
121     public void execute( EnforcerRuleHelper helper )
122         throws EnforcerRuleException
123     {
124         EnforcerRuleUtils utils = new EnforcerRuleUtils( helper );
125 
126         MavenProject project;
127         try
128         {
129             project = (MavenProject) helper.evaluate( "${project}" );
130 
131             List<Model> models =
132                 utils.getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(),
133                                             new File( project.getBasedir(), "pom.xml" ) );
134             List<Model> badModels = new ArrayList<Model>();
135 
136             StringBuffer newMsg = new StringBuffer();
137             newMsg.append( "Some poms have repositories defined:\n" );
138 
139             for ( Model model : models )
140             {
141                 if ( banRepositories )
142                 {
143                     List<Repository> repos = model.getRepositories();
144                     if ( repos != null && !repos.isEmpty() )
145                     {
146                         List<String> bannedRepos =
147                             findBannedRepositories( repos, allowedRepositories, allowSnapshotRepositories );
148                         if ( !bannedRepos.isEmpty() )
149                         {
150                             badModels.add( model );
151                             newMsg.append(
152                                 model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion()
153                                     + " has repositories " + bannedRepos );
154                         }
155                     }
156                 }
157                 if ( banPluginRepositories )
158                 {
159                     List<Repository> repos = model.getPluginRepositories();
160                     if ( repos != null && !repos.isEmpty() )
161                     {
162                         List<String> bannedRepos =
163                             findBannedRepositories( repos, allowedPluginRepositories, allowSnapshotPluginRepositories );
164                         if ( !bannedRepos.isEmpty() )
165                         {
166                             badModels.add( model );
167                             newMsg.append(
168                                 model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion()
169                                     + " has plugin repositories " + bannedRepos );
170                         }
171                     }
172                 }
173             }
174 
175             // if anything was found, log it then append the
176             // optional message.
177             if ( !badModels.isEmpty() )
178             {
179                 String message = getMessage();
180                 if ( StringUtils.isNotEmpty( message ) )
181                 {
182                     newMsg.append( message );
183                 }
184 
185                 throw new EnforcerRuleException( newMsg.toString() );
186             }
187 
188         }
189         catch ( ExpressionEvaluationException e )
190         {
191             throw new EnforcerRuleException( e.getLocalizedMessage() );
192         }
193         catch ( ArtifactResolutionException e )
194         {
195             throw new EnforcerRuleException( e.getLocalizedMessage() );
196         }
197         catch ( ArtifactNotFoundException e )
198         {
199             throw new EnforcerRuleException( e.getLocalizedMessage() );
200         }
201         catch ( IOException e )
202         {
203             throw new EnforcerRuleException( e.getLocalizedMessage() );
204         }
205         catch ( XmlPullParserException e )
206         {
207             throw new EnforcerRuleException( e.getLocalizedMessage() );
208         }
209     }
210 
211     /**
212      * 
213      * @param repos all repositories, never {@code null}
214      * @param allowedRepos allowed repositories, never {@code null}
215      * @param allowSnapshots 
216      * @return List of banned repositoreis.
217      */
218     private static List<String> findBannedRepositories( List<Repository> repos, List<String> allowedRepos,
219                                                         boolean allowSnapshots )
220     {
221         List<String> bannedRepos = new ArrayList<String>( allowedRepos.size() );
222         for ( Repository r : repos )
223         {
224             if ( !allowedRepos.contains( r.getId() ) )
225             {
226                 if ( !allowSnapshots || r.getReleases() == null || r.getReleases().isEnabled() )
227                 {
228                     // if we are not allowing snapshots and this repo is enabled for releases
229                     // it is banned.  We don't care whether it is enabled for snapshots
230                     // if you define a repo and don't enable it for anything, then we have nothing 
231                     // to worry about
232                     bannedRepos.add( r.getId() );
233                 }
234             }
235         }
236         return bannedRepos;
237     }
238 }