Skip to content

Commit

Permalink
Added Profile dependency migration (#4311)
Browse files Browse the repository at this point in the history
* Added profile dependency migration into dependency, dependencyManagement and plugin section

* Apply formatter & remove superfluous tests

* Remove unused import

---------

Co-authored-by: marcel-gepardec <marcel.reiter@gepardec.at>
  • Loading branch information
timtebeek and marcel-gepardec authored Jul 9, 2024
1 parent da3f0a4 commit 4569dae
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 4 deletions.
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 All @@ -61,9 +64,9 @@ public boolean isAcceptable(SourceFile sourceFile, P p) {
protected MavenResolutionResult getResolutionResult() {
Iterator<Object> itr = getCursor()
.getPath(Xml.Document.class::isInstance);
if(itr.hasNext()) {
if (itr.hasNext()) {
Xml.Document newDocument = (Xml.Document) itr.next();
if(document != null && document != newDocument) {
if (document != null && document != newDocument) {
throw new IllegalStateException(
"The same MavenVisitor instance has been used on two different XML documents. " +
"This violates the Recipe contract that they will return a unique visitor instance every time getVisitor() is called.");
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,9 @@ public boolean isDependencyTag(String groupId, String artifactId) {
}

public boolean isPluginDependencyTag(String groupId, String artifactId) {
if (!PLUGIN_DEPENDENCY_MATCHER.matches(getCursor())) {
if (!isTag("dependency") ||
!PLUGIN_DEPENDENCY_MATCHER.matches(getCursor()) &&
!PROFILE_PLUGIN_DEPENDENCY_MATCHER.matches(getCursor())) {
return false;
}
Xml.Tag tag = getCursor().getValue();
Expand All @@ -153,6 +163,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 @@ -147,6 +147,64 @@ 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
@Issue("https://github.com/openrewrite/rewrite-java-dependencies/issues/55")
void requireNewGroupIdOrNewArtifactId() {
Expand Down Expand Up @@ -1135,4 +1193,58 @@ 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>
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void changeManagedDependencyWithDynamicVersion() {
}

@Test
void latestPatch() {
void latestPatchMangedDependency() {
rewriteRun(
spec -> spec.recipe(new ChangeManagedDependencyGroupIdAndArtifactId(
"javax.activation",
Expand Down Expand Up @@ -176,4 +176,61 @@ void latestPatch() {
)
);
}

@Test
void changeProfileManagedDependencyGroupIdAndArtifactId() {
rewriteRun(
spec -> spec.recipe(new ChangeManagedDependencyGroupIdAndArtifactId(
"javax.activation",
"javax.activation-api",
"jakarta.activation",
"jakarta.activation-api",
"2.1.0"
)),
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>
"""
)
);
}
}

0 comments on commit 4569dae

Please sign in to comment.