Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Profile dependency migration #4310

Closed
Closed
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 @@ -35,8 +35,11 @@
public class MavenVisitor<P> extends XmlVisitor<P> {

static final XPathMatcher DEPENDENCY_MATCHER = new XPathMatcher("/project/dependencies/dependency");
static final XPathMatcher PROFILE_DEPENDENCY_MATCHER = new XPathMatcher("/project/profiles/profile/dependencies/dependency");
static final XPathMatcher PLUGIN_DEPENDENCY_MATCHER = new XPathMatcher("//plugins/plugin/dependencies/dependency");
static final XPathMatcher PROFILE_PLUGIN_DEPENDENCY_MATCHER = new XPathMatcher("/project/profiles/profile/build/plugins/plugin/dependencies/dependency");
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 PLUGIN_MATCHER = new XPathMatcher("//plugins/plugin");
static final XPathMatcher PARENT_MATCHER = new XPathMatcher("/project/parent");
Expand Down Expand Up @@ -96,6 +99,11 @@ public boolean isDependencyTag() {
*/
public boolean isDependencyTag(String groupId, String artifactId) {
if (!isDependencyTag()) {
if (isTag("dependency") && PROFILE_DEPENDENCY_MATCHER.matches(getCursor())) {
Xml.Tag tag = getCursor().getValue();
return matchesGlob(tag.getChildValue("groupId").orElse(null), groupId) &&
matchesGlob(tag.getChildValue("artifactId").orElse(null), artifactId);
}
return false;
}
Xml.Tag tag = getCursor().getValue();
Expand Down Expand Up @@ -132,7 +140,7 @@ public boolean isDependencyTag(String groupId, String artifactId) {
}

public boolean isPluginDependencyTag(String groupId, String artifactId) {
if (!PLUGIN_DEPENDENCY_MATCHER.matches(getCursor())) {
if (!(PLUGIN_DEPENDENCY_MATCHER.matches(getCursor()) || PROFILE_PLUGIN_DEPENDENCY_MATCHER.matches(getCursor()))) {
return false;
}
Xml.Tag tag = getCursor().getValue();
Expand All @@ -153,6 +161,11 @@ public boolean isManagedDependencyTag() {
*/
public boolean isManagedDependencyTag(String groupId, String artifactId) {
if (!isManagedDependencyTag()) {
if (isTag("dependency") && PROFILE_MANAGED_DEPENDENCY_MATCHER.matches(getCursor())) {
Xml.Tag tag = getCursor().getValue();
return matchesGlob(tag.getChildValue("groupId").orElse(null), groupId) &&
matchesGlob(tag.getChildValue("artifactId").orElse(null), artifactId);
}
return false;
}
Xml.Tag tag = getCursor().getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.maven.Assertions.pomXml;
Expand Down Expand Up @@ -147,6 +148,163 @@ void changeManagedDependencyGroupIdAndArtifactId() {
);
}

@Test
void changeProfileManagedDependencyGroupIdAndArtifactId() {
rewriteRun(
spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"2.1.0",
null
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
</profiles>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
</profiles>
</project>
"""
)
);
}

@Test
void changeProfileManagedDependencyWithDynamicVersion() {
rewriteRun(
spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"2.1.x",
null
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
</profiles>
</project>
""",
spec -> spec.after(pom -> {
assertThat(pom).containsPattern("<version>2.1.(\\d+)</version>");
return pom;
})
)
);
}

@Test
void latestPatchProfileManagedDependency() {
rewriteRun(
spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"latest.patch",
null
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
</profiles>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
</profiles>
</project>
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-java-dependencies/issues/55")
void requireNewGroupIdOrNewArtifactId() {
Expand Down Expand Up @@ -1135,4 +1293,151 @@ void changeGroupIdOnWildcardArtifacts() {
)
);
}

@Test
void changeProfileDependencyGroupIdAndArtifactId() {
rewriteRun(
spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"2.1.0",
null
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
"""
)
);
}

@Test
void changeProfileDependencyWithDynamicVersion() {
rewriteRun(
spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"2.1.x",
null
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
""",
spec -> spec.after(pom -> {
assertThat(pom).containsPattern("<version>2.1.(\\d+)</version>");
return pom;
})
)
);
}

@Test
void latestPatchProfileDependency() {
rewriteRun(
spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"latest.patch",
null
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<profiles>
<profile>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
"""
)
);
}
}
Loading