Coverage Report - org.apache.maven.plugins.enforcer.RequireNoRepositories
 
Classes in this File Line Coverage Branch Coverage Complexity
RequireNoRepositories
0%
0/51
0%
0/30
14
 
 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  0
 public class RequireNoRepositories
 46  
     extends AbstractNonCacheableEnforcerRule
 47  
 {
 48  
     /**
 49  
      * Whether to ban non-plugin repositories. By default they are banned.
 50  
      */
 51  0
     public boolean banRepositories = true;
 52  
 
 53  
     /**
 54  
      * Whether to ban plugin repositories. By default they are banned.
 55  
      */
 56  0
     public boolean banPluginRepositories = true;
 57  
 
 58  
     /**
 59  
      * Specify explicitly allowed non-plugin repositories. This is a list of ids.
 60  
      */
 61  0
     public List<String> allowedRepositories = Collections.emptyList();
 62  
 
 63  
     /**
 64  
      * Specify explicitly allowed plugin repositories. This is a list of ids.
 65  
      */
 66  0
     public List<String> allowedPluginRepositories = Collections.emptyList();
 67  
 
 68  
     /**
 69  
      * Whether to allow repositories which only resolve snapshots. By default they are banned.
 70  
      */
 71  0
     public boolean allowSnapshotRepositories = false;
 72  
 
 73  
     /**
 74  
      * Whether to allow plugin repositories which only resolve snapshots. By default they are banned.
 75  
      */
 76  0
     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  0
         EnforcerRuleUtils utils = new EnforcerRuleUtils( helper );
 87  
 
 88  
         MavenProject project;
 89  
         try
 90  
         {
 91  0
             project = (MavenProject) helper.evaluate( "${project}" );
 92  
 
 93  0
             List<Model> models =
 94  
                 utils.getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(),
 95  
                                             new File( project.getBasedir(), "pom.xml" ) );
 96  0
             List<Model> badModels = new ArrayList<Model>();
 97  
 
 98  0
             StringBuffer newMsg = new StringBuffer();
 99  0
             newMsg.append( "Some poms have repositories defined:\n" );
 100  
 
 101  0
             for ( Model model : models )
 102  
             {
 103  0
                 if ( banRepositories )
 104  
                 {
 105  
                     @SuppressWarnings( "unchecked" )
 106  0
                     List<Repository> repos = model.getRepositories();
 107  0
                     if ( repos != null && !repos.isEmpty() )
 108  
                     {
 109  0
                         List<String> bannedRepos =
 110  
                             findBannedRepositories( repos, allowedRepositories, allowSnapshotRepositories );
 111  0
                         if ( !bannedRepos.isEmpty() )
 112  
                         {
 113  0
                             badModels.add( model );
 114  0
                             newMsg.append(
 115  
                                 model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion()
 116  
                                     + " has repositories " + bannedRepos );
 117  
                         }
 118  
                     }
 119  
                 }
 120  0
                 if ( banPluginRepositories )
 121  
                 {
 122  
                     @SuppressWarnings( "unchecked" )
 123  0
                     List<Repository> repos = model.getPluginRepositories();
 124  0
                     if ( repos != null && !repos.isEmpty() )
 125  
                     {
 126  0
                         List<String> bannedRepos =
 127  
                             findBannedRepositories( repos, allowedPluginRepositories, allowSnapshotPluginRepositories );
 128  0
                         if ( !bannedRepos.isEmpty() )
 129  
                         {
 130  0
                             badModels.add( model );
 131  0
                             newMsg.append(
 132  
                                 model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion()
 133  
                                     + " has plugin repositories " + bannedRepos );
 134  
                         }
 135  
                     }
 136  0
                 }
 137  
             }
 138  
 
 139  
             // if anything was found, log it then append the
 140  
             // optional message.
 141  0
             if ( !badModels.isEmpty() )
 142  
             {
 143  0
                 if ( StringUtils.isNotEmpty( message ) )
 144  
                 {
 145  0
                     newMsg.append( message );
 146  
                 }
 147  
 
 148  0
                 throw new EnforcerRuleException( newMsg.toString() );
 149  
             }
 150  
 
 151  
         }
 152  0
         catch ( ExpressionEvaluationException e )
 153  
         {
 154  0
             throw new EnforcerRuleException( e.getLocalizedMessage() );
 155  
         }
 156  0
         catch ( ArtifactResolutionException e )
 157  
         {
 158  0
             throw new EnforcerRuleException( e.getLocalizedMessage() );
 159  
         }
 160  0
         catch ( ArtifactNotFoundException e )
 161  
         {
 162  0
             throw new EnforcerRuleException( e.getLocalizedMessage() );
 163  
         }
 164  0
         catch ( IOException e )
 165  
         {
 166  0
             throw new EnforcerRuleException( e.getLocalizedMessage() );
 167  
         }
 168  0
         catch ( XmlPullParserException e )
 169  
         {
 170  0
             throw new EnforcerRuleException( e.getLocalizedMessage() );
 171  0
         }
 172  0
     }
 173  
 
 174  
     private static List<String> findBannedRepositories( List<Repository> repos, List<String> allowedRepos, boolean allowSnapshots )
 175  
     {
 176  0
         List<String> bannedRepos = new ArrayList<String>( allowedRepos.size() );
 177  0
         for ( Repository r : repos )
 178  
         {
 179  0
             if ( !allowedRepos.contains( r.getId() ) )
 180  
             {
 181  0
                 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  0
                     bannedRepos.add( r.getId() );
 188  
                 }
 189  
             }
 190  
         }
 191  0
         return bannedRepos;
 192  
     }
 193  
 }