Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2103,4 +2103,27 @@ void doesNotDowngradeBuildscriptDependencyVersionInSettingsGradleExt() {
)
);
}

@Test
void upgradeAnnotationProcessors() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("org.mapstruct", "mapstruct*", "1.6.x", null)),
buildGradle(
//language=groovy
"""
plugins { id 'java' }
repositories { mavenCentral() }
dependencies {
implementation 'org.mapstruct:mapstruct:1.4.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.1.Final'
}
""",
spec -> spec.after(actual ->
assertThat(actual)
.doesNotContain("1.4.1.Final")
.containsPattern("1.6.\\d+(.\\d+)?")
.actual())
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class MavenVisitor<P> extends XmlVisitor<P> {
static final XPathMatcher PROFILE_MANAGED_DEPENDENCY_MATCHER = new XPathMatcher("/project/profiles/profile/dependencyManagement/dependencies/dependency");
static final XPathMatcher PROPERTY_MATCHER = new XPathMatcher("/project/properties/*");
static final XPathMatcher PLUGIN_MATCHER = new XPathMatcher("//plugins/plugin");
static final XPathMatcher ANNOTATION_PROCESSORS_PATH_MATCHER = new XPathMatcher("//annotationProcessorPaths/path");
static final XPathMatcher MANAGED_PLUGIN_MATCHER = new XPathMatcher("//pluginManagement/plugins/plugin");
static final XPathMatcher PARENT_MATCHER = new XPathMatcher("/project/parent");
static final XPathMatcher PROJECT_MATCHER = new XPathMatcher("/project");
Expand Down Expand Up @@ -277,7 +278,7 @@ public boolean isProjectTag() {
return isTag("project") && PROJECT_MATCHER.matches(getCursor());
}

private boolean isTag(String name) {
protected boolean isTag(String name) {
// `XPathMatcher` is still a bit expensive
return getCursor().getValue() instanceof Xml.Tag && name.equals(getCursor().<Xml.Tag>getValue().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
maybeUpdateModel();
}
}
} else if (isPluginDependencyTag(groupId, artifactId)) {
t = upgradePluginDependency(ctx, t);
} else if (isPluginDependencyTag(groupId, artifactId) || isAnnotationProcessorPathTag(groupId, artifactId)) {
t = upgradeTag(ctx, t);
}
} catch (MavenDownloadingException e) {
return e.warn(t);
Expand Down Expand Up @@ -329,7 +329,7 @@ private Xml.Tag upgradeDependency(ExecutionContext ctx, Xml.Tag t) throws MavenD
return null;
}

private Xml.Tag upgradePluginDependency(ExecutionContext ctx, Xml.Tag t) throws MavenDownloadingException {
private Xml.Tag upgradeTag(ExecutionContext ctx, Xml.Tag t) throws MavenDownloadingException {
String groupId = t.getChildValue("groupId").orElse(null);
String artifactId = t.getChildValue("artifactId").orElse(null);
String version = t.getChildValue("version").orElse(null);
Expand Down Expand Up @@ -373,6 +373,15 @@ private String resolveVersion(String version) {
return MavenDependency.findNewerVersion(groupId, artifactId, version, getResolutionResult(), metadataFailures,
versionComparator, ctx);
}

private boolean isAnnotationProcessorPathTag(String groupId, String artifactId) {
if (!isTag("path") || !ANNOTATION_PROCESSORS_PATH_MATCHER.matches(getCursor())) {
return false;
}
Xml.Tag tag = getCursor().getValue();
return matchesGlob(tag.getChildValue("groupId").orElse(null), groupId) &&
matchesGlob(tag.getChildValue("artifactId").orElse(null), artifactId);
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,94 @@ void upgradePluginDependencies() {
);
}

@Test
void upgradeAnnotationProcessors() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("org.mapstruct", "mapstruct*", "1.6.x", null, null, null)),
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.1.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
""",
spec -> spec.after(actual ->
assertThat(actual)
.doesNotContain("1.4.1.Final")
.containsPattern("<version>1.6.\\d+(.\\d+)?</version>")
.actual())
)
);
}

@Test
void upgradeAnnotationProcessorsVersionProperty() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("org.mapstruct", "mapstruct*", "1.6.x", null, null, null)),
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<properties>
<org.mapstruct.version>1.4.1.Final</org.mapstruct.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
""",
spec -> spec.after(actual ->
assertThat(actual)
.doesNotContain("1.4.1.Final")
.containsPattern("<org.mapstruct.version>1.6.\\d+(.\\d+)?</org.mapstruct.version>")
.actual())
)
);
}

@Test
void upgradePluginDependenciesOnProperty() {
rewriteRun(
Expand Down