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