Skip to content
Draft
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 @@ -90,7 +90,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {

@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (isPropertyTag() && propertyName.equals(tag.getName()) &&
if ((isPropertyTag() || isProfilePropertyTag()) && propertyName.equals(tag.getName()) &&
!newValue.equals(tag.getValue().orElse(null))) {
doAfterVisit(new ChangeTagValueVisitor<>(tag, newValue));
maybeUpdateModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class MavenVisitor<P> extends XmlVisitor<P> {
static final XPathMatcher MANAGED_DEPENDENCY_MATCHER = new XPathMatcher("/project/dependencyManagement/dependencies/dependency");
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 PROFILE_PROPERTY_MATCHER = new XPathMatcher("/project/profiles/profile/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");
Expand Down Expand Up @@ -101,6 +102,10 @@ public boolean isPropertyTag() {
return PROPERTY_MATCHER.matches(getCursor());
}

public boolean isProfilePropertyTag() {
return PROFILE_PROPERTY_MATCHER.matches(getCursor());
}

public boolean isDependencyTag() {
return isTag("dependency") && DEPENDENCY_MATCHER.matches(getCursor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,139 @@ void changeExistingProperty() {
);
}

@Test
void changePropertyInProfile() {
rewriteRun(
spec -> spec.recipe(new ChangePropertyValue("sonar.host.url", "https://new-sonar.example.com", false, false)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://old-sonar.example.com</sonar.host.url>
<java.version>21</java.version>
</properties>
</profile>
</profiles>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>https://new-sonar.example.com</sonar.host.url>
<java.version>21</java.version>
</properties>
</profile>
</profiles>
</project>
"""
)
);
}

@Test
void changePropertyInBothProjectAndProfile() {
rewriteRun(
spec -> spec.recipe(new ChangePropertyValue("java.version", "21", false, false)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<java.version>17</java.version>
</properties>

<profiles>
<profile>
<id>sonar</id>
<properties>
<java.version>17</java.version>
</properties>
</profile>
</profiles>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<properties>
<java.version>21</java.version>
</properties>

<profiles>
<profile>
<id>sonar</id>
<properties>
<java.version>21</java.version>
</properties>
</profile>
</profiles>
</project>
"""
)
);
}

@Test
void doNotChangeUnrelatedPropertyInProfile() {
rewriteRun(
spec -> spec.recipe(new ChangePropertyValue("sonar.host.url", "https://new-sonar.example.com", false, false)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<profiles>
<profile>
<id>sonar</id>
<properties>
<java.version>21</java.version>
</properties>
</profile>
</profiles>
</project>
"""
)
);
}

@Test
void addPropertyInOrder() {
rewriteRun(
Expand Down