Skip to content

Commit e5e1d5f

Browse files
authored
AddMockitoJavaAgentToMavenSurefirePlugin: add surefire config in reactor modules (#1169)
When a module's parent pom is part of the same reactor and manages the surefire and dependency plugins in pluginManagement (without declaring them in build/plugins), the recipe added an empty argLine property and the maven-dependency-plugin, but skipped adding the surefire plugin with the Java agent argLine. AddPlugin with a null filePattern refuses to run on modules whose parent is a project pom, so the surefire configuration was silently dropped while the dependency-plugin (which passes a **/pom.xml filePattern) was added, leaving a pointless partial change. Pass the same **/pom.xml filePattern for the surefire AddPlugin so the configuration is added consistently, producing a complete, working setup. Fixes #1168
1 parent 6584812 commit e5e1d5f

2 files changed

Lines changed: 128 additions & 1 deletion

File tree

src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
136136
if (FindPlugin.find(document, "org.apache.maven.plugins", "maven-surefire-plugin").isEmpty()) {
137137
doAfterVisit(new AddPlugin("org.apache.maven.plugins", "maven-surefire-plugin", null,
138138
String.format(CONFIGURATION_TAG_TEMPLATE, "@{argLine} " + getArgLineJavaAgentArgument()), null,
139-
null, null).getVisitor());
139+
null, "**/pom.xml").getVisitor());
140140
return document;
141141
}
142142
return super.visitDocument(document, ctx);

src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,4 +1614,131 @@ void augmentsSurefirePluginDeclaredInPluginManagement() {
16141614
)
16151615
);
16161616
}
1617+
1618+
@Test
1619+
void addsSurefireAgentToModuleWhenParentReactorPomManagesPluginsWithoutDeclaringThem() {
1620+
rewriteRun(
1621+
mavenProject("test-project",
1622+
pomXml(
1623+
"""
1624+
<project>
1625+
<modelVersion>4.0.0</modelVersion>
1626+
<groupId>org.sample</groupId>
1627+
<artifactId>test</artifactId>
1628+
<version>1.0</version>
1629+
<packaging>pom</packaging>
1630+
1631+
<modules>
1632+
<module>test-module1</module>
1633+
</modules>
1634+
1635+
<dependencyManagement>
1636+
<dependencies>
1637+
<dependency>
1638+
<groupId>org.mockito</groupId>
1639+
<artifactId>mockito-core</artifactId>
1640+
<version>5.14.0</version>
1641+
<scope>test</scope>
1642+
</dependency>
1643+
</dependencies>
1644+
</dependencyManagement>
1645+
1646+
<build>
1647+
<pluginManagement>
1648+
<plugins>
1649+
<plugin>
1650+
<groupId>org.apache.maven.plugins</groupId>
1651+
<artifactId>maven-dependency-plugin</artifactId>
1652+
<version>3.11.0</version>
1653+
</plugin>
1654+
<plugin>
1655+
<groupId>org.apache.maven.plugins</groupId>
1656+
<artifactId>maven-surefire-plugin</artifactId>
1657+
<version>3.5.6</version>
1658+
</plugin>
1659+
</plugins>
1660+
</pluginManagement>
1661+
</build>
1662+
</project>
1663+
""",
1664+
spec -> spec.path("pom.xml")
1665+
),
1666+
pomXml(
1667+
"""
1668+
<project>
1669+
<modelVersion>4.0.0</modelVersion>
1670+
<groupId>org.sample</groupId>
1671+
<artifactId>test-module1</artifactId>
1672+
<version>1.0</version>
1673+
1674+
<parent>
1675+
<groupId>org.sample</groupId>
1676+
<artifactId>test</artifactId>
1677+
<version>1.0</version>
1678+
<relativePath>../pom.xml</relativePath>
1679+
</parent>
1680+
1681+
<dependencies>
1682+
<dependency>
1683+
<groupId>org.mockito</groupId>
1684+
<artifactId>mockito-core</artifactId>
1685+
<scope>test</scope>
1686+
</dependency>
1687+
</dependencies>
1688+
</project>
1689+
""",
1690+
"""
1691+
<project>
1692+
<modelVersion>4.0.0</modelVersion>
1693+
<groupId>org.sample</groupId>
1694+
<artifactId>test-module1</artifactId>
1695+
<version>1.0</version>
1696+
1697+
<parent>
1698+
<groupId>org.sample</groupId>
1699+
<artifactId>test</artifactId>
1700+
<version>1.0</version>
1701+
<relativePath>../pom.xml</relativePath>
1702+
</parent>
1703+
<properties>
1704+
<argLine></argLine>
1705+
</properties>
1706+
1707+
<dependencies>
1708+
<dependency>
1709+
<groupId>org.mockito</groupId>
1710+
<artifactId>mockito-core</artifactId>
1711+
<scope>test</scope>
1712+
</dependency>
1713+
</dependencies>
1714+
<build>
1715+
<plugins>
1716+
<plugin>
1717+
<groupId>org.apache.maven.plugins</groupId>
1718+
<artifactId>maven-dependency-plugin</artifactId>
1719+
<executions>
1720+
<execution>
1721+
<goals>
1722+
<goal>properties</goal>
1723+
</goals>
1724+
</execution>
1725+
</executions>
1726+
</plugin>
1727+
<plugin>
1728+
<groupId>org.apache.maven.plugins</groupId>
1729+
<artifactId>maven-surefire-plugin</artifactId>
1730+
<configuration>
1731+
<!--suppress MavenModelInspection -->
1732+
<argLine>@{argLine} -javaagent:${org.mockito:mockito-core:jar}</argLine>
1733+
</configuration>
1734+
</plugin>
1735+
</plugins>
1736+
</build>
1737+
</project>
1738+
""",
1739+
spec -> spec.path("test-module1/pom.xml")
1740+
)
1741+
)
1742+
);
1743+
}
16171744
}

0 commit comments

Comments
 (0)