| 1 | |
package org.apache.maven.plugins.enforcer.utils; |
| 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.io.Reader; |
| 25 | |
import java.util.ArrayList; |
| 26 | |
import java.util.List; |
| 27 | |
|
| 28 | |
import org.apache.maven.artifact.Artifact; |
| 29 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
| 30 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
| 31 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
| 32 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
| 33 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
| 34 | |
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; |
| 35 | |
import org.apache.maven.model.Model; |
| 36 | |
import org.apache.maven.model.Parent; |
| 37 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
| 38 | |
import org.apache.maven.plugin.logging.Log; |
| 39 | |
import org.apache.maven.project.MavenProject; |
| 40 | |
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; |
| 41 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
| 42 | |
import org.codehaus.plexus.util.ReaderFactory; |
| 43 | |
import org.codehaus.plexus.util.StringUtils; |
| 44 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public class EnforcerRuleUtils |
| 53 | |
{ |
| 54 | |
|
| 55 | |
|
| 56 | |
ArtifactFactory factory; |
| 57 | |
|
| 58 | |
|
| 59 | |
ArtifactResolver resolver; |
| 60 | |
|
| 61 | |
|
| 62 | |
ArtifactRepository local; |
| 63 | |
|
| 64 | |
|
| 65 | |
List<ArtifactRepository> remoteRepositories; |
| 66 | |
|
| 67 | |
|
| 68 | |
Log log; |
| 69 | |
|
| 70 | |
|
| 71 | |
MavenProject project; |
| 72 | |
|
| 73 | |
private EnforcerRuleHelper helper; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public EnforcerRuleUtils( ArtifactFactory theFactory, ArtifactResolver theResolver, ArtifactRepository theLocal, |
| 86 | |
List<ArtifactRepository> theRemoteRepositories, MavenProject project, Log theLog ) |
| 87 | |
{ |
| 88 | 0 | super(); |
| 89 | 0 | this.factory = theFactory; |
| 90 | 0 | this.resolver = theResolver; |
| 91 | 0 | this.local = theLocal; |
| 92 | 0 | this.remoteRepositories = theRemoteRepositories; |
| 93 | 0 | this.log = theLog; |
| 94 | 0 | this.project = project; |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
@SuppressWarnings( "unchecked" ) |
| 103 | |
public EnforcerRuleUtils( EnforcerRuleHelper helper ) |
| 104 | 21 | { |
| 105 | |
|
| 106 | 21 | this.helper = helper; |
| 107 | |
|
| 108 | |
|
| 109 | |
try |
| 110 | |
{ |
| 111 | 21 | factory = (ArtifactFactory) helper.getComponent( ArtifactFactory.class ); |
| 112 | 21 | resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class ); |
| 113 | 21 | local = (ArtifactRepository) helper.evaluate( "${localRepository}" ); |
| 114 | 21 | project = (MavenProject) helper.evaluate( "${project}" ); |
| 115 | 21 | remoteRepositories = project.getRemoteArtifactRepositories(); |
| 116 | |
} |
| 117 | 0 | catch ( ComponentLookupException e ) |
| 118 | |
{ |
| 119 | |
|
| 120 | 0 | e.printStackTrace(); |
| 121 | |
} |
| 122 | 0 | catch ( ExpressionEvaluationException e ) |
| 123 | |
{ |
| 124 | |
|
| 125 | 0 | e.printStackTrace(); |
| 126 | 21 | } |
| 127 | 21 | } |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
private Model readModel ( File pom ) |
| 140 | |
throws IOException, XmlPullParserException |
| 141 | |
{ |
| 142 | 41 | Reader reader = ReaderFactory.newXmlReader( pom ); |
| 143 | 41 | MavenXpp3Reader xpp3 = new MavenXpp3Reader(); |
| 144 | 41 | Model model = null; |
| 145 | |
try |
| 146 | |
{ |
| 147 | 41 | model = xpp3.read( reader ); |
| 148 | |
} |
| 149 | |
finally |
| 150 | |
{ |
| 151 | 41 | reader.close(); |
| 152 | 41 | reader = null; |
| 153 | 41 | } |
| 154 | 41 | return model; |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
private Model getPomModel ( String groupId, String artifactId, String version, File pom ) |
| 175 | |
throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException |
| 176 | |
{ |
| 177 | 41 | Model model = null; |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | 41 | boolean found = false; |
| 188 | |
try |
| 189 | |
{ |
| 190 | 41 | model = readModel( pom ); |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | 41 | found = checkIfModelMatches( groupId, artifactId, version, model ); |
| 195 | |
} |
| 196 | 0 | catch ( IOException e ) |
| 197 | |
{ |
| 198 | |
|
| 199 | |
|
| 200 | |
} |
| 201 | 0 | catch ( XmlPullParserException e ) |
| 202 | |
{ |
| 203 | |
|
| 204 | |
|
| 205 | 41 | } |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | 41 | if ( !found ) |
| 210 | |
{ |
| 211 | 0 | Artifact pomArtifact = factory.createArtifact( groupId, artifactId, version, null, "pom" ); |
| 212 | 0 | resolver.resolve( pomArtifact, remoteRepositories, local ); |
| 213 | 0 | model = readModel( pomArtifact.getFile() ); |
| 214 | |
} |
| 215 | |
|
| 216 | 41 | return model; |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public List<Model> getModelsRecursively ( String groupId, String artifactId, String version, File pom ) |
| 236 | |
throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException |
| 237 | |
{ |
| 238 | 41 | List<Model> models = null; |
| 239 | 41 | Model model = getPomModel( groupId, artifactId, version, pom ); |
| 240 | |
|
| 241 | 41 | Parent parent = model.getParent(); |
| 242 | |
|
| 243 | |
|
| 244 | 41 | if ( parent != null ) |
| 245 | |
{ |
| 246 | |
|
| 247 | 21 | String relativePath = parent.getRelativePath(); |
| 248 | 21 | if ( StringUtils.isEmpty( relativePath ) ) |
| 249 | |
{ |
| 250 | 0 | relativePath = "../pom.xml"; |
| 251 | |
} |
| 252 | |
|
| 253 | 21 | File parentPom = new File( pom.getParent(), relativePath ); |
| 254 | |
|
| 255 | |
|
| 256 | 21 | if ( parentPom.isDirectory() ) |
| 257 | |
{ |
| 258 | 1 | parentPom = new File( parentPom, "pom.xml" ); |
| 259 | |
} |
| 260 | |
|
| 261 | 21 | models = getModelsRecursively( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), parentPom ); |
| 262 | 21 | } |
| 263 | |
else |
| 264 | |
{ |
| 265 | |
|
| 266 | 20 | models = new ArrayList<Model>(); |
| 267 | |
} |
| 268 | 41 | models.add( model ); |
| 269 | |
|
| 270 | 41 | return models; |
| 271 | |
} |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
protected boolean checkIfModelMatches ( String groupId, String artifactId, String version, Model model ) |
| 284 | |
{ |
| 285 | |
|
| 286 | 46 | String modelGroup = model.getGroupId(); |
| 287 | 46 | String modelVersion = model.getVersion(); |
| 288 | |
|
| 289 | |
try |
| 290 | |
{ |
| 291 | 46 | if ( StringUtils.isEmpty( modelGroup ) ) |
| 292 | |
{ |
| 293 | 23 | modelGroup = model.getParent().getGroupId(); |
| 294 | |
} |
| 295 | |
else |
| 296 | |
{ |
| 297 | |
|
| 298 | 23 | modelGroup = (String) helper.evaluate( modelGroup ); |
| 299 | |
} |
| 300 | |
|
| 301 | 44 | if ( StringUtils.isEmpty( modelVersion ) ) |
| 302 | |
{ |
| 303 | 21 | modelVersion = model.getParent().getVersion(); |
| 304 | |
} |
| 305 | |
else |
| 306 | |
{ |
| 307 | |
|
| 308 | 23 | modelVersion = (String) helper.evaluate( modelVersion ); |
| 309 | |
} |
| 310 | |
} |
| 311 | 2 | catch ( NullPointerException e ) |
| 312 | |
{ |
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
} |
| 319 | 0 | catch ( ExpressionEvaluationException e ) |
| 320 | |
{ |
| 321 | |
|
| 322 | 46 | } |
| 323 | 46 | return ( StringUtils.equals( groupId, modelGroup ) && StringUtils.equals( version, modelVersion ) && StringUtils |
| 324 | |
.equals( artifactId, model.getArtifactId() ) ); |
| 325 | |
} |
| 326 | |
} |