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