Skip to content

Commit

Permalink
#530: Use java.version property from parent pom if available (#593)
Browse files Browse the repository at this point in the history
* #530: Use `java.version` property from parent pom if available

* Fix sonar warning

* Implement review finding
  • Loading branch information
kaklakariada authored Oct 22, 2024
1 parent 3287796 commit d187472
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 43 deletions.
1 change: 1 addition & 0 deletions doc/changes/changes_4.3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion maven-project-crawler/pk_generated_parent.pom

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions maven-project-crawler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
<artifactId>project-keeper-java-project-crawler-generated-parent</artifactId>
<version>${revision}</version>
</parent>
<properties>
<!-- Workaround for https://github.com/exasol/project-keeper/issues/530 -->
<java.version>17</java.version>
</properties>
<prerequisites>
<maven>${minimum.maven.version}</maven>
</prerequisites>
Expand Down
1 change: 0 additions & 1 deletion project-keeper-cli/pk_generated_parent.pom

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions project-keeper-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
<artifactId>project-keeper-cli-generated-parent</artifactId>
<version>${revision}</version>
</parent>
<properties>
<!-- Workaround for https://github.com/exasol/project-keeper/issues/530 -->
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.exasol</groupId>
Expand Down
1 change: 0 additions & 1 deletion project-keeper-maven-plugin/pk_generated_parent.pom

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions project-keeper-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<artifactId>project-keeper-maven-plugin-generated-parent</artifactId>
<version>${revision}</version>
</parent>
<properties>
<!-- Workaround for https://github.com/exasol/project-keeper/issues/530 -->
<java.version>17</java.version>
</properties>
<prerequisites>
<maven>${minimum.maven.version}</maven>
</prerequisites>
Expand Down
1 change: 0 additions & 1 deletion project-keeper/pk_generated_parent.pom

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions project-keeper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<artifactId>project-keeper-core-generated-parent</artifactId>
<version>${revision}</version>
</parent>
<properties>
<!-- Workaround for https://github.com/exasol/project-keeper/issues/530 -->
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.exasol</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) //
Expand Down Expand Up @@ -207,11 +207,11 @@ private ElementBuilder build(final Collection<ProjectKeeperModule> enabledModule
return element("build").child(plugins(enabledModules));
}

private ElementBuilder properties(final Collection<ProjectKeeperModule> enabledModules) {
private ElementBuilder properties(final Collection<ProjectKeeperModule> 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", "") //
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,7 +45,7 @@ private List<String> 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<String> pluginNames = getPluginNames(pom);
final License license = pom.getLicenses().get(0);
Expand Down Expand Up @@ -88,7 +89,7 @@ static Stream<Arguments> testPluginsAddedByModuleCases() {
}

@Test
void testProfilesForNativeImages() throws XmlPullParserException, IOException {
void testProfilesForNativeImages() {
final Model pom = runGeneration(List.of(ProjectKeeperModule.NATIVE_IMAGE), null);
final List<Profile> profiles = pom.getProfiles();
final Profile profile1 = profiles.get(0);
Expand All @@ -100,13 +101,26 @@ void testProfilesForNativeImages() throws XmlPullParserException, IOException {
);
}

private Model runGeneration(final List<ProjectKeeperModule> 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<ProjectKeeperModule> 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);
}
}

Expand All @@ -119,31 +133,28 @@ static Stream<Arguments> 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<String> expectedPlugins)
throws XmlPullParserException, IOException {
void testPluginsAddedByModule(final ProjectKeeperModule module, final List<String> 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<String> expectedDependencies)
throws XmlPullParserException, IOException {
void testDependenciesAddedByModule(final ProjectKeeperModule module, final List<String> 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();
Expand All @@ -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<Plugin> plugins = pom.getBuild().getPlugins();
final Plugin lastPlugin = plugins.get(plugins.size() - 1);
Expand Down
1 change: 0 additions & 1 deletion shared-model-classes/pk_generated_parent.pom

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions shared-model-classes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>project-keeper-shared-model-classes</artifactId>
<name>Project Keeper shared model classes</name>
<properties>
<!-- Workaround for https://github.com/exasol/project-keeper/issues/530 -->
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.json</groupId>
Expand Down
1 change: 0 additions & 1 deletion shared-test-setup/pk_generated_parent.pom

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions shared-test-setup/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
<version>${revision}</version>
</parent>
<properties>
<!-- Workaround for https://github.com/exasol/project-keeper/issues/530 -->
<java.version>17</java.version>
<sonar.coverage.exclusions>**/*.*</sonar.coverage.exclusions>
</properties>
<dependencies>
Expand Down

0 comments on commit d187472

Please sign in to comment.