001package org.apache.maven.plugins.enforcer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.List;
027
028import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
029import org.apache.maven.artifact.resolver.ArtifactResolutionException;
030import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
031import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
032import org.apache.maven.model.Model;
033import org.apache.maven.model.Repository;
034import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtils;
035import org.apache.maven.project.MavenProject;
036import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
037import org.codehaus.plexus.util.StringUtils;
038import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
039
040/**
041 * This rule checks that this pom or its parents don't define a repository.
042 *
043 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
044 */
045public class RequireNoRepositories
046    extends AbstractNonCacheableEnforcerRule
047{
048    /**
049     * Whether to ban non-plugin repositories. By default they are banned.
050     * 
051     * @see #setBanRepositories(boolean)
052     */
053    private boolean banRepositories = true;
054
055    /**
056     * Whether to ban plugin repositories. By default they are banned.
057     * 
058     * @see #setBanPluginRepositories(boolean)
059     */
060    private boolean banPluginRepositories = true;
061
062    /**
063     * Specify explicitly allowed non-plugin repositories. This is a list of ids.
064     * 
065     * @see #setAllowedRepositories(List)
066     */
067    private List<String> allowedRepositories = Collections.emptyList();
068
069    /**
070     * Specify explicitly allowed plugin repositories. This is a list of ids.
071     * 
072     * @see #setAllowedPluginRepositories(List)
073     */
074    private List<String> allowedPluginRepositories = Collections.emptyList();
075
076    /**
077     * Whether to allow repositories which only resolve snapshots. By default they are banned.
078     * 
079     * @see #setAllowSnapshotRepositories(boolean)
080     */
081    private boolean allowSnapshotRepositories = false;
082
083    /**
084     * Whether to allow plugin repositories which only resolve snapshots. By default they are banned.
085     * 
086     * @see {@link #setAllowSnapshotPluginRepositories(boolean)}
087     */
088    private boolean allowSnapshotPluginRepositories = false;
089
090    public final void setBanRepositories( boolean banRepositories )
091    {
092        this.banRepositories = banRepositories;
093    }
094    
095    public final void setBanPluginRepositories( boolean banPluginRepositories )
096    {
097        this.banPluginRepositories = banPluginRepositories;
098    }
099    
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}