| 1 | |
package org.apache.maven.plugins.enforcer; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 16 | public class RequireNoRepositories |
| 46 | |
extends AbstractNonCacheableEnforcerRule |
| 47 | |
{ |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 16 | public boolean banRepositories = true; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 16 | public boolean banPluginRepositories = true; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 16 | public List<String> allowedRepositories = Collections.emptyList(); |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | 16 | public List<String> allowedPluginRepositories = Collections.emptyList(); |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | 16 | public boolean allowSnapshotRepositories = false; |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | 16 | public boolean allowSnapshotPluginRepositories = false; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public void execute( EnforcerRuleHelper helper ) |
| 84 | |
throws EnforcerRuleException |
| 85 | |
{ |
| 86 | 14 | EnforcerRuleUtils utils = new EnforcerRuleUtils( helper ); |
| 87 | |
|
| 88 | |
MavenProject project; |
| 89 | |
try |
| 90 | |
{ |
| 91 | 14 | project = (MavenProject) helper.evaluate( "${project}" ); |
| 92 | |
|
| 93 | 14 | List<Model> models = |
| 94 | |
utils.getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(), |
| 95 | |
new File( project.getBasedir(), "pom.xml" ) ); |
| 96 | 14 | List<Model> badModels = new ArrayList<Model>(); |
| 97 | |
|
| 98 | 14 | StringBuffer newMsg = new StringBuffer(); |
| 99 | 14 | newMsg.append( "Some poms have repositories defined:\n" ); |
| 100 | |
|
| 101 | 14 | for ( Model model : models ) |
| 102 | |
{ |
| 103 | 28 | if ( banRepositories ) |
| 104 | |
{ |
| 105 | |
@SuppressWarnings( "unchecked" ) |
| 106 | 22 | List<Repository> repos = model.getRepositories(); |
| 107 | 22 | if ( repos != null && !repos.isEmpty() ) |
| 108 | |
{ |
| 109 | 5 | List<String> bannedRepos = |
| 110 | |
findBannedRepositories( repos, allowedRepositories, allowSnapshotRepositories ); |
| 111 | 5 | if ( !bannedRepos.isEmpty() ) |
| 112 | |
{ |
| 113 | 3 | badModels.add( model ); |
| 114 | 3 | newMsg.append( |
| 115 | |
model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion() |
| 116 | |
+ " has repositories " + bannedRepos ); |
| 117 | |
} |
| 118 | |
} |
| 119 | |
} |
| 120 | 28 | if ( banPluginRepositories ) |
| 121 | |
{ |
| 122 | |
@SuppressWarnings( "unchecked" ) |
| 123 | 22 | List<Repository> repos = model.getPluginRepositories(); |
| 124 | 22 | if ( repos != null && !repos.isEmpty() ) |
| 125 | |
{ |
| 126 | 4 | List<String> bannedRepos = |
| 127 | |
findBannedRepositories( repos, allowedPluginRepositories, allowSnapshotPluginRepositories ); |
| 128 | 4 | if ( !bannedRepos.isEmpty() ) |
| 129 | |
{ |
| 130 | 2 | badModels.add( model ); |
| 131 | 2 | newMsg.append( |
| 132 | |
model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion() |
| 133 | |
+ " has plugin repositories " + bannedRepos ); |
| 134 | |
} |
| 135 | |
} |
| 136 | 28 | } |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | 14 | if ( !badModels.isEmpty() ) |
| 142 | |
{ |
| 143 | 5 | if ( StringUtils.isNotEmpty( message ) ) |
| 144 | |
{ |
| 145 | 5 | newMsg.append( message ); |
| 146 | |
} |
| 147 | |
|
| 148 | 5 | 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 | 9 | } |
| 172 | 9 | } |
| 173 | |
|
| 174 | |
private static List<String> findBannedRepositories( List<Repository> repos, List<String> allowedRepos, boolean allowSnapshots ) |
| 175 | |
{ |
| 176 | 9 | List<String> bannedRepos = new ArrayList<String>( allowedRepos.size() ); |
| 177 | 9 | for ( Repository r : repos ) |
| 178 | |
{ |
| 179 | 9 | if ( !allowedRepos.contains( r.getId() ) ) |
| 180 | |
{ |
| 181 | 7 | if ( !allowSnapshots || r.getReleases().isEnabled() ) |
| 182 | |
{ |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | 5 | bannedRepos.add( r.getId() ); |
| 188 | |
} |
| 189 | |
} |
| 190 | |
} |
| 191 | 9 | return bannedRepos; |
| 192 | |
} |
| 193 | |
} |