|
20 | 20 | */
|
21 | 21 |
|
22 | 22 | import javax.inject.Inject;
|
| 23 | +import javax.xml.stream.XMLStreamException; |
23 | 24 |
|
24 | 25 | import java.util.ArrayList;
|
25 | 26 | import java.util.Arrays;
|
26 | 27 | import java.util.Collections;
|
27 | 28 | import java.util.List;
|
| 29 | +import java.util.regex.Pattern; |
28 | 30 |
|
29 | 31 | import org.apache.commons.lang3.StringUtils;
|
30 | 32 | import org.apache.maven.artifact.Artifact;
|
|
41 | 43 | import org.apache.maven.repository.RepositorySystem;
|
42 | 44 | import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
|
43 | 45 | import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
|
| 46 | +import org.codehaus.mojo.versions.api.PomHelper; |
| 47 | +import org.codehaus.mojo.versions.recording.ChangeRecorder; |
| 48 | +import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; |
44 | 49 | import org.codehaus.mojo.versions.utils.DependencyBuilder;
|
| 50 | +import org.codehaus.mojo.versions.utils.DependencyComparator; |
45 | 51 |
|
46 | 52 | /**
|
47 | 53 | * Base class for a mojo that updates dependency versions.
|
|
53 | 59 | public abstract class AbstractVersionsDependencyUpdaterMojo
|
54 | 60 | extends AbstractVersionsUpdaterMojo
|
55 | 61 | {
|
56 |
| - |
57 | 62 | private static final String END_RANGE_CHARS = "])";
|
58 | 63 |
|
59 | 64 | private static final String START_RANGE_CHARS = "[(";
|
60 | 65 |
|
| 66 | + /** |
| 67 | + * Pattern to match snapshot versions |
| 68 | + */ |
| 69 | + protected static final Pattern SNAPSHOT_REGEX = Pattern.compile( "^(.+)-((SNAPSHOT)|(\\d{8}\\.\\d{6}-\\d+))$" ); |
| 70 | + |
| 71 | + |
61 | 72 | /**
|
62 | 73 | * A comma separated list of artifact patterns to include. Follows the pattern
|
63 | 74 | * "groupId:artifactId:type:classifier:version". Designed to allow specifying the set of includes from the command
|
@@ -104,15 +115,15 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
|
104 | 115 | * @since 1.0-alpha-3
|
105 | 116 | */
|
106 | 117 | @Parameter( property = "processDependencies", defaultValue = "true" )
|
107 |
| - private boolean processDependencies; |
| 118 | + private boolean processDependencies = true; |
108 | 119 |
|
109 | 120 | /**
|
110 | 121 | * Whether to process the dependencyManagement section of the project.
|
111 | 122 | *
|
112 | 123 | * @since 1.0-alpha-3
|
113 | 124 | */
|
114 | 125 | @Parameter( property = "processDependencyManagement", defaultValue = "true" )
|
115 |
| - private boolean processDependencyManagement; |
| 126 | + private boolean processDependencyManagement = true; |
116 | 127 |
|
117 | 128 | /**
|
118 | 129 | * Whether to process the parent section of the project. If not set will default to false.
|
@@ -142,7 +153,7 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
|
142 | 153 | * @since 1.0-alpha-3
|
143 | 154 | */
|
144 | 155 | @Parameter( property = "excludeReactor", defaultValue = "true" )
|
145 |
| - private boolean excludeReactor; |
| 156 | + private boolean excludeReactor = true; |
146 | 157 |
|
147 | 158 | @Inject
|
148 | 159 | protected AbstractVersionsDependencyUpdaterMojo( RepositorySystem repositorySystem,
|
@@ -266,6 +277,20 @@ protected Artifact toArtifact( Parent model )
|
266 | 277 | .build() );
|
267 | 278 | }
|
268 | 279 |
|
| 280 | + /** |
| 281 | + * Returns the {@link Dependency} instance for the parent project |
| 282 | + * @return {@link Dependency} object for the parent |
| 283 | + */ |
| 284 | + protected Dependency getParentDependency() |
| 285 | + { |
| 286 | + return DependencyBuilder.newBuilder() |
| 287 | + .withGroupId( getProject().getParent().getGroupId() ) |
| 288 | + .withArtifactId( getProject().getParent().getArtifactId() ) |
| 289 | + .withVersion( getProject().getParent().getVersion() ) |
| 290 | + .withType( "pom" ) |
| 291 | + .build(); |
| 292 | + } |
| 293 | + |
269 | 294 | protected String toString( MavenProject project )
|
270 | 295 | {
|
271 | 296 | StringBuilder buf = new StringBuilder();
|
@@ -507,4 +532,58 @@ private int findFirstChar( final String includeString, final String chars )
|
507 | 532 | }
|
508 | 533 | return nextRangeStartDelimiterIndex;
|
509 | 534 | }
|
| 535 | + |
| 536 | + /** |
| 537 | + * Attempts to update the dependency {@code dep} to the given {@code newVersion}. The dependency can either |
| 538 | + * be the parent project or any given dependency. |
| 539 | + * |
| 540 | + * @param pom {@link ModifiedPomXMLEventReader} instance to update the POM XML document |
| 541 | + * @param dep dependency to be updated (can also be a dependency made from the parent) |
| 542 | + * @param newVersion new version to update the dependency to |
| 543 | + * @param changeRecorderTitle title for the {@link ChangeRecorder} log |
| 544 | + * @return {@code true} if an update has been made, {@code false} otherwise |
| 545 | + * @throws XMLStreamException thrown if updating the XML doesn't succeed |
| 546 | + */ |
| 547 | + protected boolean updateDependencyVersion( ModifiedPomXMLEventReader pom, Dependency dep, |
| 548 | + String newVersion, String changeRecorderTitle ) |
| 549 | + throws XMLStreamException |
| 550 | + { |
| 551 | + boolean updated = false; |
| 552 | + if ( isProcessingParent() |
| 553 | + && getProject().getParent() != null |
| 554 | + && DependencyComparator.INSTANCE.compare( dep, DependencyBuilder.newBuilder() |
| 555 | + .withGroupId( getProject().getParentArtifact().getGroupId() ) |
| 556 | + .withArtifactId( getProject().getParentArtifact().getArtifactId() ) |
| 557 | + .withVersion( getProject().getParentArtifact().getVersion() ) |
| 558 | + .build() ) == 0 |
| 559 | + && PomHelper.setProjectParentVersion( pom, newVersion ) ) |
| 560 | + { |
| 561 | + if ( getLog().isDebugEnabled() ) |
| 562 | + { |
| 563 | + getLog().debug( "Made parent update from " + dep.getVersion() + " to " + newVersion ); |
| 564 | + } |
| 565 | + getChangeRecorder().recordUpdate( changeRecorderTitle, |
| 566 | + dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), |
| 567 | + newVersion ); |
| 568 | + updated = true; |
| 569 | + } |
| 570 | + |
| 571 | + if ( PomHelper.setDependencyVersion( pom, |
| 572 | + dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), |
| 573 | + newVersion, getProject().getModel() ) ) |
| 574 | + { |
| 575 | + if ( getLog().isInfoEnabled() ) |
| 576 | + { |
| 577 | + getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); |
| 578 | + } |
| 579 | + getChangeRecorder().recordUpdate( changeRecorderTitle, |
| 580 | + dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), |
| 581 | + newVersion ); |
| 582 | + updated = true; |
| 583 | + } |
| 584 | + |
| 585 | + return updated; |
| 586 | + } |
| 587 | + |
| 588 | + // TODO: add an updatePropertyVersion as well??? (like in CompareDependenciesMojo) |
510 | 589 | }
|
0 commit comments