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

check if java.version exists using MavenResolutionResult #322

Merged
merged 2 commits into from
Oct 19, 2023
Merged
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 @@ -22,7 +22,7 @@
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.search.FindProperties;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

Expand Down Expand Up @@ -64,9 +64,10 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
return t;
}
Optional<Xml.Tag> maybeCompilerPlugin = t.getChildren().stream()
.filter(plugin -> "plugin".equals(plugin.getName())
&& "org.apache.maven.plugins".equals(plugin.getChildValue("groupId").orElse("org.apache.maven.plugins"))
&& "maven-compiler-plugin".equals(plugin.getChildValue("artifactId").orElse(null)))
.filter(plugin ->
"plugin".equals(plugin.getName()) &&
"org.apache.maven.plugins".equals(plugin.getChildValue("groupId").orElse("org.apache.maven.plugins")) &&
"maven-compiler-plugin".equals(plugin.getChildValue("artifactId").orElse(null)))
.findAny();
Optional<Xml.Tag> maybeCompilerPluginConfig = maybeCompilerPlugin
.flatMap(it -> it.getChild("configuration"));
Expand Down Expand Up @@ -115,6 +116,8 @@ private boolean currentNewerThanProposed(@SuppressWarnings("OptionalUsedAsFieldO
}

private boolean hasJavaVersionProperty(Xml.Document xml) {
return !FindProperties.find(xml, "java.version").isEmpty();
return xml.getMarkers().findFirst(MavenResolutionResult.class)
.map(r -> r.getPom().getProperties().get("java.version") != null)
.orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.maven.Assertions.pomXml;

class UseMavenCompilerPluginReleaseConfigurationTest implements RewriteTest {
Expand Down Expand Up @@ -495,4 +496,86 @@ void noVersionDowngrade() {
""")
);
}

@Test
void reusesJavaVersionVariableIfDefinedInParentPom() {
rewriteRun(
//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>parent</artifactId>
<version>1.0.0</version>

<properties>
<java.version>11</java.version>
</properties>

<packaging>pom</packaging>
</project>
"""),
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>sample</artifactId>
<version>1.0.0</version>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>sample</artifactId>
<version>1.0.0</version>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>${java.version}</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
)
)
);
}
}
Loading