Skip to content

Commit

Permalink
Remove Jakarta annotation dependency when moving to Jakarta packages (#…
Browse files Browse the repository at this point in the history
…487)

* draft for RemoveJakartaAnnotationDependency

* Apply suggestions from code review

* add negative test

* find spring boot only
This is a workaround, reason:#481 (comment)

* Minor polish

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored May 24, 2024
1 parent fcddb6d commit 110db88
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 36 deletions.
15 changes: 15 additions & 0 deletions src/main/resources/META-INF/rewrite/jakarta-ee-9.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ recipeList:
- org.openrewrite.java.migrate.jakarta.UpdateApacheWSSecurityPackages
- org.openrewrite.java.migrate.javaee8
- org.openrewrite.java.migrate.jakarta.JavaxEEApiToJakarta
- org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependency

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.JavaxActivationMigrationToJakartaActivation
Expand Down Expand Up @@ -988,3 +990,16 @@ recipeList:
groupId: jakarta.platform
artifactId: "*"
newVersion: 9.0.0
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependency
displayName: Remove `jakarta.annotation-api` dependency when managed by Spring Boot
description: Counteract the `jakarta.annotation-api` added by `org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies` for Spring Boot applications.
preconditions:
- org.openrewrite.java.dependencies.DependencyInsight:
groupIdPattern: org.springframework.boot
artifactIdPattern: spring-boot-starter
recipeList:
- org.openrewrite.java.dependencies.RemoveDependency:
groupId: jakarta.annotation
artifactId: jakarta.annotation-api
Original file line number Diff line number Diff line change
Expand Up @@ -437,47 +437,48 @@ void projectWithSpringBootStarterWeb() {
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet)),
mavenProject(
"Sample",
//language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
</project>
"""
),
srcMainJava(
Expand All @@ -498,4 +499,115 @@ public class TestApplication {
)
);
}

@Test
void projectWithSpringBoot3StarterWebShouldRemoveJakartaDependency() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet)),
mavenProject(
"Sample",
//language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
"""
),
srcMainJava(
//language=java
java(
"""
import jakarta.servlet.A;
public class TestApplication {
}
"""
)
)
)
);
}

@Test
void doNothingIfNotFoundTransitiveDependency() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet)),
mavenProject(
"Sample",
//language=java
srcMainJava(
java(
"""
import jakarta.servlet.A;
public class TestApplication {
}
"""
)
),
//language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</project>
"""
)
)
);
}
}

0 comments on commit 110db88

Please sign in to comment.