diff --git a/.gitignore b/.gitignore index 03ed5d5..344c2c5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build/ .idea/ .boot-releases out/ +.vscode/ \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 8134ed6..7d3e402 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -26,6 +26,7 @@ dependencies { implementation("org.openrewrite:rewrite-properties:${rewriteVersion}") implementation("org.openrewrite.recipe:rewrite-java-dependencies:${rewriteVersion}") + implementation("org.openrewrite.recipe:rewrite-migrate-java:${rewriteVersion}") runtimeOnly("org.openrewrite:rewrite-java-17:${rewriteVersion}") @@ -38,6 +39,8 @@ dependencies { testImplementation("javax.xml.bind:jaxb-api:2.4.0-b180830.0359") testImplementation("jakarta.xml.bind:jakarta.xml.bind-api:3.0.0") + testImplementation("javax:javaee-api:7.0") + testRuntimeOnly("org.openrewrite:rewrite-java-17:${rewriteVersion}") testRuntimeOnly("io.quarkus:quarkus-grpc:1.13.+") diff --git a/src/main/resources/META-INF/rewrite/javaee7-to-quarkus.yml b/src/main/resources/META-INF/rewrite/javaee7-to-quarkus.yml new file mode 100644 index 0000000..2001bad --- /dev/null +++ b/src/main/resources/META-INF/rewrite/javaee7-to-quarkus.yml @@ -0,0 +1,196 @@ +# +# Copyright 2024 the original author or authors. +#
+# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +#
+# https://www.apache.org/licenses/LICENSE-2.0 +#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.quarkus.migrate.javaee.JavaEEtoQuarkus2Migration
+displayName: Migrate JavaEE to Quarkus 2
+description: These recipes help with the migration of a JavaEE application using EJBs and Hibernate to Quarkus 2. Additional transformations like JSF, JMS, Quarkus Tests may be necessary.
+recipeList:
+ - org.openrewrite.quarkus.migrate.javaee.AddQuarkus2MavenPlugins
+ - org.openrewrite.quarkus.migrate.javaee.AddQuarkus2Dependencies
+ - org.openrewrite.quarkus.migrate.javaee.RemoveJavaEEDependencies
+
+ - org.openrewrite.quarkus.migrate.javaee.JavaEEtoQuarkus2CodeMigration
+ - org.openrewrite.java.migrate.Java8toJava11
+
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.quarkus.migrate.javaee.AddQuarkus2Dependencies
+displayName: Add Quarkus 2 dependencies
+description: Add Quarkus 2 dependencies to the project.
+recipeList:
+ # Add Basic Quarkus Extensions
+ - org.openrewrite.java.dependencies.AddDependency:
+ groupId: io.quarkus
+ artifactId: quarkus-arc
+ - org.openrewrite.java.dependencies.AddDependency:
+ groupId: io.quarkus
+ artifactId: quarkus-resteasy
+ - org.openrewrite.java.dependencies.AddDependency:
+ groupId: io.quarkus
+ artifactId: quarkus-resteasy-jackson
+ - org.openrewrite.java.dependencies.AddDependency:
+ groupId: io.quarkus
+ artifactId: quarkus-undertow
+ - org.openrewrite.java.dependencies.AddDependency:
+ groupId: io.quarkus
+ artifactId: quarkus-hibernate-orm
+ - org.openrewrite.java.dependencies.AddDependency:
+ groupId: io.quarkus
+ artifactId: quarkus-jdbc-h2
+ - org.openrewrite.java.dependencies.AddDependency:
+ groupId: io.quarkus
+ artifactId: quarkus-junit5
+ scope: test
+ - org.openrewrite.java.dependencies.AddDependency:
+ groupId: io.rest-assured
+ artifactId: rest-assured
+ scope: test
+
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.quarkus.migrate.javaee.RemoveJavaEEDependencies
+displayName: Remove JavaEE dependencies
+description: Remove JavaEE dependencies from the project.
+recipeList:
+ # Remove JavaEE dependencies
+ - org.openrewrite.java.dependencies.RemoveDependency:
+ groupId: javax*
+ artifactId: javaee-api
+ - org.openrewrite.java.dependencies.RemoveDependency:
+ groupId: javax*
+ artifactId: cdi-api
+ - org.openrewrite.java.dependencies.RemoveDependency:
+ groupId: javax*
+ artifactId: javax*
+
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.quarkus.migrate.javaee.AddQuarkus2MavenPlugins
+displayName: Migrate JavaEE Maven Dependencies to Quarkus 2
+description: Upgrade Standard JavaEE dependencies to Quarkus 2 dependencies.
+recipeList:
+ # Add Quarkus BOM
+ - org.openrewrite.maven.AddManagedDependency:
+ groupId: io.quarkus.platform
+ artifactId: quarkus-bom
+ version: '2.x'
+ type: pom
+ scope: import
+
+ # Add Maven Plugins
+ - org.openrewrite.maven.AddPlugin:
+ groupId: io.quarkus.platform
+ artifactId: quarkus-maven-plugin
+ version: '2.16.12.Final'
+ executions:
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.quarkus.migrate.javaee; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class JavaEEtoQuarkus2CodeTranformationsTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.parser(JavaParser.fromJavaVersion() + .logCompilationWarningsAndErrors(true) + .classpath("javaee-api")) + .recipeFromResources("org.openrewrite.quarkus.migrate.javaee.JavaEEtoQuarkus2CodeMigration"); + } + + @Test + @DocumentExample + void javaEEtoQuarkus2CodeTransformationsTest() { + rewriteRun( + java( + // language=java + """ + package org.acme; + + import javax.ejb.EJB; + import javax.ejb.Local; + import javax.ejb.SessionContext; + import javax.ejb.Singleton; + import javax.ejb.Stateful; + import javax.ejb.Stateless; + + import javax.annotation.Resource; + import javax.persistence.EntityManager; + import javax.persistence.PersistenceContext; + + @Stateless + public class PingEJBSLS { + + @PersistenceContext + private EntityManager entityManager; + + @Resource + private SessionContext context; + + @EJB + private PingEJBLocal pingEJBLocal; + + @EJB(lookup = "java:global/PingEJBSingleton") + private PingEJBSingleton pingEJBSingleton; + + public String getMsg() { + return "PingEJBSLS: " + pingEJBLocal.getMsg() + " " + pingEJBSingleton.getMsg(); + } + + } + + @Stateful + @Local + public class PingEJBLocal { + + private static int hitCount; + + public String getMsg() { + return "PingEJBLocal: " + hitCount++; + } + + } + + @Singleton + public class PingEJBSingleton { + + private static int hitCount; + + @PersistenceContext + private EntityManager entityManager; + + public String getMsg() { + return "PingEJBSingleton: " + hitCount++; + } + } + """, + // language=java + """ + package org.acme; + + import javax.ejb.SessionContext; + import javax.enterprise.context.ApplicationScoped; + import javax.enterprise.context.Dependent; + import javax.enterprise.context.SessionScoped; + import javax.inject.Inject; + import javax.annotation.Resource; + import javax.persistence.EntityManager; + + @Dependent + public class PingEJBSLS { + + @Inject + private EntityManager entityManager; + + @Resource + private SessionContext context; + + @Inject + private PingEJBLocal pingEJBLocal; + + @Inject + private PingEJBSingleton pingEJBSingleton; + + public String getMsg() { + return "PingEJBSLS: " + pingEJBLocal.getMsg() + " " + pingEJBSingleton.getMsg(); + } + + } + + @SessionScoped + public class PingEJBLocal { + + private static int hitCount; + + public String getMsg() { + return "PingEJBLocal: " + hitCount++; + } + + } + + @ApplicationScoped + public class PingEJBSingleton { + + private static int hitCount; + + @Inject + private EntityManager entityManager; + + public String getMsg() { + return "PingEJBSingleton: " + hitCount++; + } + } + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/quarkus/migrate/javaee/JavaEEtoQuarkus2MavenDependenciesMigrationTest.java b/src/test/java/org/openrewrite/quarkus/migrate/javaee/JavaEEtoQuarkus2MavenDependenciesMigrationTest.java new file mode 100644 index 0000000..206f5b2 --- /dev/null +++ b/src/test/java/org/openrewrite/quarkus/migrate/javaee/JavaEEtoQuarkus2MavenDependenciesMigrationTest.java @@ -0,0 +1,465 @@ +/* + * Copyright 2024 the original author or authors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.quarkus.migrate.javaee;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.maven.Assertions.pomXml;
+
+class JavaEEtoQuarkus2MavenDependenciesMigrationTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipeFromResources("org.openrewrite.quarkus.migrate.javaee.JavaEEtoQuarkus2Migration");
+ }
+
+ @Test
+ @DocumentExample
+ void convertJavaEEToQuarkusDependencies1() {
+ rewriteRun(
+ spec -> spec.expectedCyclesThatMakeChanges(2),
+ // language=xml
+ pomXml(
+ """
+
+