diff --git a/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java b/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java index 939e422547..4f068ce090 100644 --- a/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java +++ b/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java @@ -24,6 +24,7 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.ListUtils; +import org.openrewrite.internal.StringUtils; import org.openrewrite.maven.AddPlugin; import org.openrewrite.maven.AddPropertyVisitor; import org.openrewrite.maven.ChangePluginExecutions; @@ -43,6 +44,7 @@ import java.util.List; import java.util.Optional; +import java.util.concurrent.atomic.AtomicBoolean; import static java.util.Collections.emptyList; @@ -65,7 +67,10 @@ public class AddMockitoJavaAgentToMavenSurefirePlugin extends Recipe { final String displayName = "Add Mockito Java Agent to Maven Surefire Plugin"; @Getter - final String description = "Adds required configuration to specifically enable the Mockito/Bytebuddy Java agent in the Maven Surefire plugin for Java 21 compatibility."; + final String description = "Mockito attaches its Byte Buddy agent to the running JVM at test time, which the JDK has " + + "warned about since Java 21 and intends to disallow. This recipe instead loads the agent up front through the " + + "Maven Surefire plugin, adding the `maven-dependency-plugin` `properties` goal to resolve the agent jar path, " + + "to silence the warning and stay ahead of the JDK change."; @Override public TreeVisitor getVisitor() { @@ -83,7 +88,21 @@ private String getArgLineJavaAgentArgument() { } private Xml.Tag buildConfigurationTag(String argLineJavaAgentParam, boolean hasExistingArgLine) { - return Xml.Tag.build(String.format(CONFIGURATION_TAG_TEMPLATE, hasExistingArgLine ? argLineJavaAgentParam : "@{argLine} " + argLineJavaAgentParam)); + return Xml.Tag.build(String.format(CONFIGURATION_TAG_TEMPLATE, hasExistingArgLine ? argLineJavaAgentParam : newArgLineValue(argLineJavaAgentParam))); + } + + private String newArgLineValue(String argLineJavaAgentParam) { + return usesRuntimeArgLineProperty() ? "@{argLine} " + argLineJavaAgentParam : argLineJavaAgentParam; + } + + /** + * The {@code @{argLine}} late replacement, and the empty {@code argLine} property it needs to not be + * passed to the JVM verbatim, are only worth adding when something actually supplies that property. + */ + private boolean usesRuntimeArgLineProperty() { + return getResolutionResult().getPom().getProperties().containsKey("argLine") || + getResolutionResult().getPom().getPlugins().stream().anyMatch(AddMockitoJavaAgentToMavenSurefirePlugin::isJacocoPlugin) || + declaresJacocoPlugin(getCursor().firstEnclosingOrThrow(Xml.Document.class)); } private void maybeAddMavenDependencyPluginWithPropertiesGoal() { @@ -132,11 +151,13 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { } maybeAddMavenDependencyPluginWithPropertiesGoal(); - doAfterVisit(new AddPropertyVisitor("argLine", "", true)); + if (usesRuntimeArgLineProperty()) { + doAfterVisit(new AddPropertyVisitor("argLine", "", true)); + } if (FindPlugin.find(document, "org.apache.maven.plugins", "maven-surefire-plugin").isEmpty()) { doAfterVisit(new AddPlugin("org.apache.maven.plugins", "maven-surefire-plugin", null, - String.format(CONFIGURATION_TAG_TEMPLATE, "@{argLine} " + getArgLineJavaAgentArgument()), null, + String.format(CONFIGURATION_TAG_TEMPLATE, newArgLineValue(getArgLineJavaAgentArgument())), null, null, "**/pom.xml").getVisitor()); return document; } @@ -172,11 +193,15 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } if (argLineTagChildren.size() == 1) { Xml.Tag argLineTag = argLineTagChildren.get(0); - String existingArgLineValue = argLineTag.getValue().orElse("@{argLine}"); + String existingArgLineValue = argLineTag.getValue().orElse(""); if (!existingArgLineValue.contains(argLineJavaAgentParam)) { + // An empty argLine carries nothing to preserve, so it is filled in as if it were absent + String mergedArgLine = StringUtils.isBlank(existingArgLineValue) ? + newArgLineValue(argLineJavaAgentParam) : + existingArgLineValue + " " + argLineJavaAgentParam; List nonArgLineTags = ListUtils.filter(configContents, content -> content != argLineTag); - Xml.Tag mergedConfiguration = buildConfigurationTag(existingArgLineValue + " " + argLineJavaAgentParam, true); + Xml.Tag mergedConfiguration = buildConfigurationTag(mergedArgLine, true); Xml.Tag updatedConfig = config.withContent(ListUtils.concatAll(nonArgLineTags, mergedConfiguration.getContent())); return autoFormat(t.withContent(ListUtils.map(pluginContents, c -> c == config ? updatedConfig : c)), ctx); } @@ -198,6 +223,29 @@ private static boolean isMavenPlugin(Plugin plugin, String artifactId) { (plugin.getGroupId() == null || MAVEN_PLUGINS_GROUP_ID.equals(plugin.getGroupId())); } + private static boolean isJacocoPlugin(Plugin plugin) { + return "jacoco-maven-plugin".equals(plugin.getArtifactId()) && "org.jacoco".equals(plugin.getGroupId()); + } + + /** + * Scans the raw document, as JaCoCo declared in a profile or in {@code pluginManagement} is absent from + * {@link org.openrewrite.maven.tree.ResolvedPom#getPlugins()}. + */ + private static boolean declaresJacocoPlugin(Xml.Document document) { + return new XmlIsoVisitor() { + @Override + public Xml.Tag visitTag(Xml.Tag tag, AtomicBoolean found) { + if ("plugin".equals(tag.getName()) && + "jacoco-maven-plugin".equals(tag.getChildValue("artifactId").orElse(null)) && + "org.jacoco".equals(tag.getChildValue("groupId").orElse(null))) { + found.set(true); + return tag; + } + return super.visitTag(tag, found); + } + }.reduce(document, new AtomicBoolean()).get(); + } + private static boolean hasPropertiesGoal(Plugin plugin) { return plugin.getExecutions().stream() .anyMatch(execution -> execution.getGoals() != null && execution.getGoals().contains("properties")); diff --git a/src/main/resources/META-INF/rewrite/examples.yml b/src/main/resources/META-INF/rewrite/examples.yml index 07708ed8e7..93654de08d 100644 --- a/src/main/resources/META-INF/rewrite/examples.yml +++ b/src/main/resources/META-INF/rewrite/examples.yml @@ -225,9 +225,6 @@ examples: 3.5.4 - - - @@ -254,7 +251,7 @@ examples: maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} diff --git a/src/main/resources/META-INF/rewrite/recipes.csv b/src/main/resources/META-INF/rewrite/recipes.csv index e3ee9cf6a3..e51cb061b2 100644 --- a/src/main/resources/META-INF/rewrite/recipes.csv +++ b/src/main/resources/META-INF/rewrite/recipes.csv @@ -1,20 +1,20 @@ ecosystem,packageName,name,displayName,description,recipeCount,category1,category2,category3,category4,category1Description,category2Description,category3Description,category4Description,options,dataTables maven,org.openrewrite.recipe:rewrite-migrate-java,com.google.guava.InlineGuavaMethods,Inline `guava` methods annotated with `@InlineMe`,Automatically generated recipes to inline method calls based on `@InlineMe` annotations discovered in the type table.,66,,,Guava,Google,,,,,, -maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.JSpecifyBestPractices,JSpecify best practices,"Apply JSpecify best practices, such as migrating off of alternatives, and adding missing `@Nullable` annotations.",34,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.JSpecifyBestPractices,JSpecify best practices,"Apply JSpecify best practices, such as migrating off of alternatives, and adding missing `@Nullable` annotations.",35,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromJakartaAnnotationApi,Migrate from Jakarta annotation API to JSpecify,Migrate from Jakarta annotation API to JSpecify.,5,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromJavaxAnnotationApi,Migrate from javax annotation API to JSpecify,Migrate from javax annotation API to JSpecify.,7,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromJavaxAnnotationApi,Migrate from javax annotation API to JSpecify,Migrate from javax annotation API to JSpecify.,8,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromJetbrainsAnnotations,Migrate from JetBrains annotations to JSpecify,Migrate from JetBrains annotations to JSpecify.,5,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromMicrometerAnnotations,Migrate from Micrometer annotations to JSpecify,Migrate from Micrometer annotations to JSpecify.,6,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromMicronautAnnotations,Migrate from Micronaut Framework annotations to JSpecify,Migrate from Micronaut Framework annotations to JSpecify.,5,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromSpringFrameworkAnnotations,Migrate from Spring Framework annotations to JSpecify,Migrate from Spring Framework annotations to JSpecify.,6,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateToJSpecify,Migrate to JSpecify,This recipe will migrate to JSpecify annotations from various other nullability annotation standards.,29,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateToJSpecify,Migrate to JSpecify,This recipe will migrate to JSpecify annotations from various other nullability annotation standards.,30,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AccessController,Remove Security AccessController,The Security Manager API is unsupported in Java 24. This recipe will remove the usage of `java.security.AccessController`.,4,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddJDeprScanPlugin,Add `JDeprScan` Maven Plug-in,Add the `JDeprScan` Maven plugin to scan class files for uses of deprecated APIs.,1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,"[{""name"":""release"",""type"":""String"",""displayName"":""release"",""description"":""Specifies the Java SE release that provides the set of deprecated APIs for scanning."",""example"":""11""}]", maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddLombokMapstructBinding,Add `lombok-mapstruct-binding` when both MapStruct and Lombok are used,Add the `lombok-mapstruct-binding` annotation processor as needed when both MapStruct and Lombok are used.,6,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""instanceName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddLombokMapstructBindingMavenDependencyOnly,Add `lombok-mapstruct-binding` dependency for Maven when both MapStruct and Lombok are used,"Add the `lombok-mapstruct-binding` when both MapStruct and Lombok are used, and the dependency does not already exist. Only to be called from `org.openrewrite.java.migrate.AddLombokMapstructBinding` to reduce redundant checks.",2,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""instanceName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddMapstructAnnotationProcessorPath,Add `mapstruct-processor` to the `maven-compiler-plugin` annotation processor paths,"Add the `mapstruct-processor` annotation processor path, matching the version of the `mapstruct` dependency, so that MapStruct mappers are generated when annotation processing is configured explicitly.",1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddMissingMethodImplementation,Adds missing method implementations,Check for missing methods required by interfaces and adds them.,1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,"[{""name"":""fullyQualifiedClassName"",""type"":""String"",""displayName"":""Fully qualified class name"",""description"":""A fully qualified class being implemented with missing method."",""example"":""com.yourorg.FooBar"",""required"":true},{""name"":""methodPattern"",""type"":""String"",""displayName"":""Method pattern"",""description"":""A method pattern for matching required method definition."",""example"":""*..* hello(..)"",""required"":true},{""name"":""methodTemplateString"",""type"":""String"",""displayName"":""Method template"",""description"":""Template of method to add"",""example"":""public String hello() { return \\\""Hello from #{}!\\\""; }"",""required"":true}]", -maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddMockitoJavaAgentToMavenSurefirePlugin,Add Mockito Java Agent to Maven Surefire Plugin,Adds required configuration to specifically enable the Mockito/Bytebuddy Java agent in the Maven Surefire plugin for Java 21 compatibility.,1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddMockitoJavaAgentToMavenSurefirePlugin,Add Mockito Java Agent to Maven Surefire Plugin,"Mockito attaches its Byte Buddy agent to the running JVM at test time, which the JDK has warned about since Java 21 and intends to disallow. This recipe instead loads the agent up front through the Maven Surefire plugin, adding the `maven-dependency-plugin` `properties` goal to resolve the agent jar path, to silence the warning and stay ahead of the JDK change.",1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddStaticVariableOnProducerSessionBean,Adds `static` modifier to `@Produces` fields that are in session beans,"Ensures that the fields annotated with `@Produces` which is inside the session bean (`@Stateless`, `@Stateful`, or `@Singleton`) are declared `static`.",1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddSuppressionForIllegalReflectionWarningsPlugin,Add maven jar plugin to suppress illegal reflection warnings,Adds a maven jar plugin that's configured to suppress Illegal Reflection Warnings.,1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,"[{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""An exact version number, or node-style semver selector used to select the version number."",""example"":""29.X""}]", maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.AddSurefireFailsafeArgLine,Add `argLine` to surefire and failsafe plugins,"Adds the specified arguments to the `argLine` configuration of the Maven Surefire and Failsafe plugins, merging with any existing argLine value without duplicating arguments. The `@{argLine}` [late property reference](https://maven.apache.org/surefire/maven-surefire-plugin/faq.html) is prepended so that an agent injected by another plugin during the build, such as the JaCoCo coverage agent from `jacoco-maven-plugin:prepare-agent`, is preserved rather than overwritten. It is not added when the existing `argLine` already references the `argLine` property.",1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,"[{""name"":""argLine"",""type"":""String"",""displayName"":""Arg line"",""description"":""The arguments to add to the surefire and failsafe plugin `argLine` configuration. Individual arguments are space-separated. Arguments already present in the existing argLine are not duplicated."",""example"":""--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED"",""required"":true}]", @@ -49,7 +49,7 @@ maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.J maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JREThrowableFinalMethods,Rename final method declarations `getSuppressed()` and `addSuppressed(Throwable exception)` in classes that extend `Throwable`,The recipe renames `getSuppressed()` and `addSuppressed(Throwable exception)` methods in classes that extend `java.lang.Throwable` to `myGetSuppressed` and `myAddSuppressed(Throwable)`. These methods were added to Throwable in Java 7 and are marked final which cannot be overridden.,1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JREWrapperInterface,Add missing `isWrapperFor` and `unwrap` methods,Add method implementations stubs to classes that implement `java.sql.Wrapper`.,3,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.Java8toJava11,Migrate to Java 11,"This recipe will apply changes commonly needed when upgrading to Java 11. Specifically, for those applications that are built on Java 8, this recipe will update and add dependencies on J2EE libraries that are no longer directly bundled with the JDK. This recipe will also replace deprecated API with equivalents when there is a clear migration strategy. Build files will also be updated to use Java 11 as the target/source and plugins will be also be upgraded to versions that are compatible with Java 11.",256,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""instanceName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" -maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JavaBestPractices,Java best practices,"Applies opinionated best practices for Java projects targeting Java 25. This recipe includes the full Java 25 upgrade chain plus additional improvements to code style, API usage, and third-party dependency reduction that go beyond what the version migration recipes apply.",1686,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""instanceName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" +maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JavaBestPractices,Java best practices,"Applies opinionated best practices for Java projects targeting Java 25. This recipe includes the full Java 25 upgrade chain plus additional improvements to code style, API usage, and third-party dependency reduction that go beyond what the version migration recipes apply.",1687,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""instanceName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JpaCacheProperties,Disable the persistence unit second-level cache,Sets an explicit value for the shared cache mode.,1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.Jre17AgentMainPreMainPublic,Set visibility of `premain` and `agentmain` methods to `public`,Check for a behavior change in Java agents.,5,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.Krb5LoginModuleClass,Use `com.sun.security.auth.module.Krb5LoginModule` instead of `com.ibm.security.auth.module.Krb5LoginModule`,Do not use the `com.ibm.security.auth.module.Krb5LoginModule` class.,2,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, diff --git a/src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java b/src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java index 3158042b3b..cde5b1f83b 100644 --- a/src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java +++ b/src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java @@ -84,9 +84,6 @@ void addsMockitoAgentArgAndPropertiesGoalToMavenPlugins() { 3.5.4 - - - @@ -113,7 +110,7 @@ void addsMockitoAgentArgAndPropertiesGoalToMavenPlugins() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -301,7 +298,6 @@ void addsMockitoAgentArgToExistingConfigurationWithNoArgLineTag() { - some-property @@ -333,7 +329,7 @@ void addsMockitoAgentArgToExistingConfigurationWithNoArgLineTag() { foobar - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -408,9 +404,6 @@ void addsMockitoAgentArgToExistingArgLineTagWithNoValue() { 3.5.4 - - - @@ -437,7 +430,7 @@ void addsMockitoAgentArgToExistingArgLineTagWithNoValue() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -515,9 +508,6 @@ void addsMockitoAgentArgPreservingExistingArgLineArguments() { 3.5.4 - - - @@ -635,9 +625,6 @@ void onlyAddsConfigurationToPomWithMockitoInMultiModuleProject() { 1.0 ../pom.xml - - - @@ -664,7 +651,7 @@ void onlyAddsConfigurationToPomWithMockitoInMultiModuleProject() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -718,9 +705,6 @@ void addsMavenSurefireAndDependencyPluginsWhenAbsent() { 3.5.4 - - - @@ -747,7 +731,7 @@ void addsMavenSurefireAndDependencyPluginsWhenAbsent() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -803,7 +787,7 @@ void addsGoalsTagWithPropertiesGoalToExistingMavenDependencyPluginWhenMissing() maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -823,9 +807,6 @@ void addsGoalsTagWithPropertiesGoalToExistingMavenDependencyPluginWhenMissing() 3.5.4 - - - @@ -855,7 +836,7 @@ void addsGoalsTagWithPropertiesGoalToExistingMavenDependencyPluginWhenMissing() maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -914,7 +895,7 @@ void addsPropertiesGoalToExistingGoalsSectionInMavenDependencyPlugin() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -934,9 +915,6 @@ void addsPropertiesGoalToExistingGoalsSectionInMavenDependencyPlugin() { 3.5.4 - - - @@ -967,7 +945,7 @@ void addsPropertiesGoalToExistingGoalsSectionInMavenDependencyPlugin() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -997,9 +975,6 @@ void makesNoChangeWhenMockitoAgentFlagAlreadyExists() { 3.5.4 - - - @@ -1026,7 +1001,7 @@ void makesNoChangeWhenMockitoAgentFlagAlreadyExists() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} ${project.build.directory}/jacoco.exec @@ -1088,7 +1063,7 @@ void makesNoChangeWhenMockitoAgentFlagAlreadyExistsUsingSingleLineArgline() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} ${project.build.directory}/jacoco.exec @@ -1177,7 +1152,7 @@ void makesNoChangesWhenParentPomManagesSurefirePluginAndHasAgentConfiguration() maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} ${project.build.directory}/jacoco.exec @@ -1330,9 +1305,6 @@ void updatesIndividualPomsWhenParentPomManagesSurefirePluginWithoutAgentConfigur 1.0 ../pom.xml - - - @@ -1348,7 +1320,7 @@ void updatesIndividualPomsWhenParentPomManagesSurefirePluginWithoutAgentConfigur maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -1570,9 +1542,6 @@ void augmentsSurefirePluginDeclaredInPluginManagement() { 3.5.4 - - - @@ -1602,7 +1571,7 @@ void augmentsSurefirePluginDeclaredInPluginManagement() { maven-surefire-plugin - @{argLine} -javaagent:${org.mockito:mockito-core:jar} + -javaagent:${org.mockito:mockito-core:jar} @@ -1700,6 +1669,149 @@ void addsSurefireAgentToModuleWhenParentReactorPomManagesPluginsWithoutDeclaring 1.0 ../pom.xml + + + + org.mockito + mockito-core + test + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + properties + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + -javaagent:${org.mockito:mockito-core:jar} + + + + + + """, + spec -> spec.path("test-module1/pom.xml") + ) + ) + ); + } + + @Test + void omitsLateArgLineReplacementWhenNothingSetsArgLineAtRuntime() { + rewriteRun( + mavenProject("test-project", + pomXml( + """ + + 4.0.0 + org.sample + test + 1.0 + + + + org.mockito + mockito-core + 5.17.0 + test + + + + """, + """ + + 4.0.0 + org.sample + test + 1.0 + + + + org.mockito + mockito-core + 5.17.0 + test + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + properties + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + -javaagent:${org.mockito:mockito-core:jar} + + + + + + """ + ) + ) + ); + } + + @Test + void usesLateArgLineReplacementWhenJacocoPluginPresent() { + rewriteRun( + mavenProject("test-project", + pomXml( + """ + + 4.0.0 + org.sample + test + 1.0 + + + + org.mockito + mockito-core + 5.17.0 + test + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + + """, + """ + + 4.0.0 + org.sample + test + 1.0 @@ -1708,11 +1820,17 @@ void addsSurefireAgentToModuleWhenParentReactorPomManagesPluginsWithoutDeclaring org.mockito mockito-core + 5.17.0 test + + org.jacoco + jacoco-maven-plugin + 0.8.12 + org.apache.maven.plugins maven-dependency-plugin @@ -1735,8 +1853,178 @@ void addsSurefireAgentToModuleWhenParentReactorPomManagesPluginsWithoutDeclaring + """ + ) + ) + ); + } + + @Test + void usesLateArgLineReplacementWhenJacocoPluginOnlyActiveInProfile() { + rewriteRun( + mavenProject("test-project", + pomXml( + """ + + 4.0.0 + org.sample + test + 1.0 + + + + org.mockito + mockito-core + 5.17.0 + test + + + + + coverage + + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + + + """, - spec -> spec.path("test-module1/pom.xml") + """ + + 4.0.0 + org.sample + test + 1.0 + + + + + + + org.mockito + mockito-core + 5.17.0 + test + + + + + coverage + + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + properties + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + @{argLine} -javaagent:${org.mockito:mockito-core:jar} + + + + + + """ + ) + ) + ); + } + + @Test + void usesLateArgLineReplacementWhenArgLinePropertyAlreadyDeclared() { + rewriteRun( + mavenProject("test-project", + pomXml( + """ + + 4.0.0 + org.sample + test + 1.0 + + -Xmx512m + + + + + org.mockito + mockito-core + 5.17.0 + test + + + + """, + """ + + 4.0.0 + org.sample + test + 1.0 + + -Xmx512m + + + + + org.mockito + mockito-core + 5.17.0 + test + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + properties + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + @{argLine} -javaagent:${org.mockito:mockito-core:jar} + + + + + + """ ) ) ); diff --git a/src/test/java/org/openrewrite/java/migrate/UpgradeToJava25Test.java b/src/test/java/org/openrewrite/java/migrate/UpgradeToJava25Test.java index dcfe66517e..10d9a9b2d6 100644 --- a/src/test/java/org/openrewrite/java/migrate/UpgradeToJava25Test.java +++ b/src/test/java/org/openrewrite/java/migrate/UpgradeToJava25Test.java @@ -124,7 +124,7 @@ void upgradesMavenPluginsForJava25() { .containsPattern("maven-surefire-plugin\\s*3\\.5\\.") .containsPattern("maven-failsafe-plugin\\s*3\\.5\\.") .containsPattern("maven-pmd-plugin\\s*3\\.28\\.") - .contains("@{argLine} -javaagent:${org.mockito:mockito-core:jar}") + .contains("-javaagent:${org.mockito:mockito-core:jar}") .actual()) ) )