From d187472bf8b940b79b5a13ed19005e2d4588dcda Mon Sep 17 00:00:00 2001 From: Christoph Pirkl <4711730+kaklakariada@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:41:57 +0200 Subject: [PATCH] #530: Use `java.version` property from parent pom if available (#593) * #530: Use `java.version` property from parent pom if available * Fix sonar warning * Implement review finding --- doc/changes/changes_4.3.4.md | 1 + maven-project-crawler/pk_generated_parent.pom | 1 - maven-project-crawler/pom.xml | 4 --- project-keeper-cli/pk_generated_parent.pom | 1 - project-keeper-cli/pom.xml | 4 --- .../pk_generated_parent.pom | 1 - project-keeper-maven-plugin/pom.xml | 4 --- project-keeper/pk_generated_parent.pom | 1 - project-keeper/pom.xml | 4 --- .../validators/pom/PomFileGenerator.java | 6 ++-- .../validators/pom/PomFileGeneratorTest.java | 35 ++++++++++++------- shared-model-classes/pk_generated_parent.pom | 1 - shared-model-classes/pom.xml | 4 --- shared-test-setup/pk_generated_parent.pom | 1 - shared-test-setup/pom.xml | 2 -- 15 files changed, 27 insertions(+), 43 deletions(-) diff --git a/doc/changes/changes_4.3.4.md b/doc/changes/changes_4.3.4.md index c997e2cf..80edc0c5 100644 --- a/doc/changes/changes_4.3.4.md +++ b/doc/changes/changes_4.3.4.md @@ -19,6 +19,7 @@ The release also pins Maven plugin versions to avoid plugin versions depending o ### Bugfixes * #585: Pinned Maven plugin versions in generated parent pom +* #530: Omit `java.version` property when pom has a parent ### Documentation diff --git a/maven-project-crawler/pk_generated_parent.pom b/maven-project-crawler/pk_generated_parent.pom index d8822f32..9c998903 100644 --- a/maven-project-crawler/pk_generated_parent.pom +++ b/maven-project-crawler/pk_generated_parent.pom @@ -14,7 +14,6 @@ UTF-8 UTF-8 - 11 exasol https://sonarcloud.io diff --git a/maven-project-crawler/pom.xml b/maven-project-crawler/pom.xml index 540cd1b2..da0373dd 100644 --- a/maven-project-crawler/pom.xml +++ b/maven-project-crawler/pom.xml @@ -11,10 +11,6 @@ project-keeper-java-project-crawler-generated-parent ${revision} - - - 17 - ${minimum.maven.version} diff --git a/project-keeper-cli/pk_generated_parent.pom b/project-keeper-cli/pk_generated_parent.pom index c1e1ebd6..e77d99ce 100644 --- a/project-keeper-cli/pk_generated_parent.pom +++ b/project-keeper-cli/pk_generated_parent.pom @@ -14,7 +14,6 @@ UTF-8 UTF-8 - 11 exasol https://sonarcloud.io diff --git a/project-keeper-cli/pom.xml b/project-keeper-cli/pom.xml index 5177a41c..cb149ca1 100644 --- a/project-keeper-cli/pom.xml +++ b/project-keeper-cli/pom.xml @@ -11,10 +11,6 @@ project-keeper-cli-generated-parent ${revision} - - - 17 - com.exasol diff --git a/project-keeper-maven-plugin/pk_generated_parent.pom b/project-keeper-maven-plugin/pk_generated_parent.pom index a20964b8..0825ebf4 100644 --- a/project-keeper-maven-plugin/pk_generated_parent.pom +++ b/project-keeper-maven-plugin/pk_generated_parent.pom @@ -14,7 +14,6 @@ UTF-8 UTF-8 - 11 exasol https://sonarcloud.io diff --git a/project-keeper-maven-plugin/pom.xml b/project-keeper-maven-plugin/pom.xml index e6f31780..99ed40a9 100644 --- a/project-keeper-maven-plugin/pom.xml +++ b/project-keeper-maven-plugin/pom.xml @@ -12,10 +12,6 @@ project-keeper-maven-plugin-generated-parent ${revision} - - - 17 - ${minimum.maven.version} diff --git a/project-keeper/pk_generated_parent.pom b/project-keeper/pk_generated_parent.pom index a768d1db..bb080be1 100644 --- a/project-keeper/pk_generated_parent.pom +++ b/project-keeper/pk_generated_parent.pom @@ -14,7 +14,6 @@ UTF-8 UTF-8 - 11 exasol https://sonarcloud.io diff --git a/project-keeper/pom.xml b/project-keeper/pom.xml index 7faed0a5..b629546e 100644 --- a/project-keeper/pom.xml +++ b/project-keeper/pom.xml @@ -12,10 +12,6 @@ project-keeper-core-generated-parent ${revision} - - - 17 - com.exasol diff --git a/project-keeper/src/main/java/com/exasol/projectkeeper/validators/pom/PomFileGenerator.java b/project-keeper/src/main/java/com/exasol/projectkeeper/validators/pom/PomFileGenerator.java index 68ce507b..ec5bf323 100644 --- a/project-keeper/src/main/java/com/exasol/projectkeeper/validators/pom/PomFileGenerator.java +++ b/project-keeper/src/main/java/com/exasol/projectkeeper/validators/pom/PomFileGenerator.java @@ -101,7 +101,7 @@ private NodeBuilder projectBuilder(final Config config) { .child(VERSION, config.getVersion()) // .child("packaging", "pom") // .nullableChild(parentReference(parentPomRef)) // - .child(properties(modules)) // + .child(properties(modules, parentPomRef == null ? DEFAULT_JAVA_VERSION : null)) // .nullableChild(profiles(modules)) // .nullableChild(distributionManagement(modules)) // .child(licenses(config)) // @@ -207,11 +207,11 @@ private ElementBuilder build(final Collection enabledModule return element("build").child(plugins(enabledModules)); } - private ElementBuilder properties(final Collection enabledModules) { + private ElementBuilder properties(final Collection enabledModules, final String javaVersion) { return element("properties") // .child("project.build.sourceEncoding", "UTF-8") // .child("project.reporting.outputEncoding", "UTF-8") // - .child("java.version", DEFAULT_JAVA_VERSION) // + .nullableChild(javaVersion == null ? null : element("java.version").text(javaVersion)) // .child("sonar.organization", "exasol") // .child("sonar.host.url", "https://sonarcloud.io") // .child("test.excludeTags", "") // diff --git a/project-keeper/src/test/java/com/exasol/projectkeeper/validators/pom/PomFileGeneratorTest.java b/project-keeper/src/test/java/com/exasol/projectkeeper/validators/pom/PomFileGeneratorTest.java index 167a16d9..30f48916 100644 --- a/project-keeper/src/test/java/com/exasol/projectkeeper/validators/pom/PomFileGeneratorTest.java +++ b/project-keeper/src/test/java/com/exasol/projectkeeper/validators/pom/PomFileGeneratorTest.java @@ -1,5 +1,6 @@ package com.exasol.projectkeeper.validators.pom; +import static java.util.Collections.emptyList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.assertAll; @@ -44,7 +45,7 @@ private List getDependencyNames(final Model pom) { // [utest -> dsn~mvn-toolchain~1] @Test - void testGenerateWithDefaultModule() throws XmlPullParserException, IOException { + void testGenerateWithDefaultModule() { final Model pom = runGeneration(List.of(ProjectKeeperModule.DEFAULT), null); final List pluginNames = getPluginNames(pom); final License license = pom.getLicenses().get(0); @@ -88,7 +89,7 @@ static Stream testPluginsAddedByModuleCases() { } @Test - void testProfilesForNativeImages() throws XmlPullParserException, IOException { + void testProfilesForNativeImages() { final Model pom = runGeneration(List.of(ProjectKeeperModule.NATIVE_IMAGE), null); final List profiles = pom.getProfiles(); final Profile profile1 = profiles.get(0); @@ -100,13 +101,26 @@ void testProfilesForNativeImages() throws XmlPullParserException, IOException { ); } - private Model runGeneration(final List modules, final ParentPomRef parentPomRef) - throws IOException, XmlPullParserException { + @Test + void testAddJavaVersionPropertyForRootPom() { + final Model pom = runGeneration(emptyList(), null); + assertThat(pom.getProperties().get("java.version"), equalTo(PomFileGenerator.DEFAULT_JAVA_VERSION)); + } + + @Test + void testOmitJavaVersionPropertyForNonRootPom() { + final Model pom = runGeneration(emptyList(), new ParentPomRef("group", "parent-artifact", "version", "path")); + assertThat(pom.getProperties().get("java.version"), nullValue()); + } + + private Model runGeneration(final List modules, final ParentPomRef parentPomRef) { final String result = new PomFileGenerator().generatePomContent(modules, "com.example", "my-parent-pom", "1.0.0", parentPomRef, new RepoInfo(TEST_REPO_NAME, TEST_REPO_LICENSE)); try (final ByteArrayInputStream inputStream = new ByteArrayInputStream( result.getBytes(StandardCharsets.UTF_8))) { return new MavenXpp3Reader().read(inputStream); + } catch (IOException | XmlPullParserException exception) { + throw new IllegalStateException("Failed to generate POM file: " + exception.getMessage(), exception); } } @@ -119,31 +133,28 @@ static Stream testDependenciesAddedByModuleCases() { @ParameterizedTest @ValueSource(booleans = { true, false }) - void distributionManagement(final boolean hasDm) throws IOException, XmlPullParserException { + void distributionManagement(final boolean hasDm) { final Model pom = runGeneration(hasDm ? List.of(ProjectKeeperModule.MAVEN_CENTRAL) : List.of(), null); assertThat(pom.getDistributionManagement(), hasDm ? not(nullValue()) : nullValue()); } @ParameterizedTest @MethodSource("testPluginsAddedByModuleCases") - void testPluginsAddedByModule(final ProjectKeeperModule module, final List expectedPlugins) - throws XmlPullParserException, IOException { + void testPluginsAddedByModule(final ProjectKeeperModule module, final List expectedPlugins) { final Model pom = runGeneration(List.of(module), null); assertThat(getPluginNames(pom), hasItems(expectedPlugins.toArray(String[]::new))); } @ParameterizedTest @MethodSource("testDependenciesAddedByModuleCases") - void testDependenciesAddedByModule(final ProjectKeeperModule module, final List expectedDependencies) - throws XmlPullParserException, IOException { + void testDependenciesAddedByModule(final ProjectKeeperModule module, final List expectedDependencies) { final Model pom = runGeneration(List.of(module), null); assertThat(getDependencyNames(pom), hasItems(expectedDependencies.toArray(String[]::new))); } @ParameterizedTest @MethodSource("testGenerationWithParentPomCases") - void testGenerationWithParentPom(final String relativePath, final String expectedRelativePath) - throws XmlPullParserException, IOException { + void testGenerationWithParentPom(final String relativePath, final String expectedRelativePath) { final Model pom = runGeneration(List.of(ProjectKeeperModule.DEFAULT), new ParentPomRef("com.example", "my-parent", "1.2.3", relativePath)); final Parent parent = pom.getParent(); @@ -156,7 +167,7 @@ void testGenerationWithParentPom(final String relativePath, final String expecte } @Test - void testReproducibleBuildPluginIsLast() throws XmlPullParserException, IOException { + void testReproducibleBuildPluginIsLast() { final Model pom = runGeneration(List.of(ProjectKeeperModule.DEFAULT), null); final List plugins = pom.getBuild().getPlugins(); final Plugin lastPlugin = plugins.get(plugins.size() - 1); diff --git a/shared-model-classes/pk_generated_parent.pom b/shared-model-classes/pk_generated_parent.pom index 21c6ec27..e865577d 100644 --- a/shared-model-classes/pk_generated_parent.pom +++ b/shared-model-classes/pk_generated_parent.pom @@ -14,7 +14,6 @@ UTF-8 UTF-8 - 11 exasol https://sonarcloud.io diff --git a/shared-model-classes/pom.xml b/shared-model-classes/pom.xml index ccc819bf..dc60c585 100644 --- a/shared-model-classes/pom.xml +++ b/shared-model-classes/pom.xml @@ -10,10 +10,6 @@ 4.0.0 project-keeper-shared-model-classes Project Keeper shared model classes - - - 17 - jakarta.json diff --git a/shared-test-setup/pk_generated_parent.pom b/shared-test-setup/pk_generated_parent.pom index 63a2daa4..391200a8 100644 --- a/shared-test-setup/pk_generated_parent.pom +++ b/shared-test-setup/pk_generated_parent.pom @@ -14,7 +14,6 @@ UTF-8 UTF-8 - 11 exasol https://sonarcloud.io diff --git a/shared-test-setup/pom.xml b/shared-test-setup/pom.xml index 6f485d55..6b23f1e8 100644 --- a/shared-test-setup/pom.xml +++ b/shared-test-setup/pom.xml @@ -12,8 +12,6 @@ ${revision} - - 17 **/*.*