From fdf085b03a97a5161a49b4dad47c095132b3bc85 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Thu, 6 Aug 2020 10:00:59 -0300 Subject: [PATCH 1/3] Avoid including platform BOM twice in the Gradle plugin Fixes #11251 Add test --- .../buildfile/AbstractGradleBuildFile.java | 43 ++++++++++++++----- ...AddExtensionToSingleModuleProjectTest.java | 5 ++- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java index ab60cfbde3a05..1701953276ac0 100644 --- a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java +++ b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java @@ -61,11 +61,30 @@ protected boolean addDependency(AppArtifactCoords coords, boolean managed) { } static boolean addDependencyInModel(Model model, AppArtifactCoords coords, boolean managed) { - StringBuilder newDependency = new StringBuilder() - .append(" implementation '") - .append(coords.getGroupId()) - .append(":") - .append(coords.getArtifactId()); + boolean isBOM = "pom".equals(coords.getType()); + StringBuilder newDependency; + if (isBOM) { + // Check if BOM is not included already + String resolvedPlatform = String + .format("%s:%s:%s", getProperty(model, "quarkusPlatformGroupId"), + getProperty(model, "quarkusPlatformArtifactId"), + getProperty(model, "quarkusPlatformVersion")); + String thisBOM = String.format("%s:%s:%s", coords.getGroupId(), coords.getArtifactId(), coords.getVersion()); + if (thisBOM.equals(resolvedPlatform)) { + // BOM matches the platform, no need to do anything + return false; + } + newDependency = new StringBuilder() + .append(" implementation enforcedPlatform(\"") + .append(thisBOM) + .append("\")'"); + } else { + newDependency = new StringBuilder() + .append(" implementation '") + .append(coords.getGroupId()) + .append(":") + .append(coords.getArtifactId()); + } if (!managed && (coords.getVersion() != null && !coords.getVersion().isEmpty())) { newDependency.append(":").append(coords.getVersion()); @@ -109,11 +128,7 @@ protected void removeDependency(AppArtifactKey key) { @Override public String getProperty(String propertyName) { - final String property = getModel().getPropertiesContent().getProperty(propertyName); - if (property != null || getModel().getRootPropertiesContent() == null) { - return property; - } - return getModel().getRootPropertiesContent().getProperty(propertyName); + return getProperty(getModel(), propertyName); } @Override @@ -121,6 +136,14 @@ public BuildTool getBuildTool() { return BuildTool.GRADLE; } + static String getProperty(Model model, String propertyName) { + final String property = model.getPropertiesContent().getProperty(propertyName); + if (property != null || model.getRootPropertiesContent() == null) { + return property; + } + return model.getRootPropertiesContent().getProperty(propertyName); + } + private Model getModel() { return modelReference.updateAndGet(model -> { if (model == null) { diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/AddExtensionToSingleModuleProjectTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/AddExtensionToSingleModuleProjectTest.java index a5a68c6efc36f..8b46297fd2e65 100644 --- a/integration-tests/gradle/src/test/java/io/quarkus/gradle/AddExtensionToSingleModuleProjectTest.java +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/AddExtensionToSingleModuleProjectTest.java @@ -21,7 +21,10 @@ public void testAddAndRemoveExtension() throws IOException, URISyntaxException, final Path build = projectDir.toPath().resolve("build.gradle"); assertThat(build).exists(); - assertThat(new String(Files.readAllBytes(build))).contains("implementation 'io.quarkus:quarkus-hibernate-orm'"); + assertThat(new String(Files.readAllBytes(build))) + .contains("implementation 'io.quarkus:quarkus-hibernate-orm'") + .doesNotContain("implementation enforcedPlatform('io.quarkus:quarkus-bom:") + .doesNotContain("implementation 'io.quarkus:quarkus-bom:"); runGradleWrapper(projectDir, ":removeExtension", "--extensions=hibernate-orm"); assertThat(new String(Files.readAllBytes(build))).doesNotContain("implementation 'io.quarkus:quarkus-hibernate-orm'"); From 9e5b108d0a58d6d4536dc5000409fabb47d47b31 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Thu, 6 Aug 2020 11:16:26 -0300 Subject: [PATCH 2/3] Do not check the version when comparing the BOM --- .../project/buildfile/AbstractGradleBuildFile.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java index 1701953276ac0..097a14936b6a7 100644 --- a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java +++ b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java @@ -66,17 +66,16 @@ static boolean addDependencyInModel(Model model, AppArtifactCoords coords, boole if (isBOM) { // Check if BOM is not included already String resolvedPlatform = String - .format("%s:%s:%s", getProperty(model, "quarkusPlatformGroupId"), - getProperty(model, "quarkusPlatformArtifactId"), - getProperty(model, "quarkusPlatformVersion")); - String thisBOM = String.format("%s:%s:%s", coords.getGroupId(), coords.getArtifactId(), coords.getVersion()); + .format("%s:%s", getProperty(model, "quarkusPlatformGroupId"), + getProperty(model, "quarkusPlatformArtifactId")); + String thisBOM = String.format("%s:%s", coords.getGroupId(), coords.getArtifactId()); if (thisBOM.equals(resolvedPlatform)) { // BOM matches the platform, no need to do anything return false; } newDependency = new StringBuilder() .append(" implementation enforcedPlatform(\"") - .append(thisBOM) + .append(thisBOM).append(":").append(coords.getVersion()) .append("\")'"); } else { newDependency = new StringBuilder() From c869bc7111fdf1fb626ce6e9f684eb6a33d04fcc Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Thu, 6 Aug 2020 11:18:11 -0300 Subject: [PATCH 3/3] Fixed minor IDE warnings --- .../project/buildfile/AbstractGradleBuildFile.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java index 097a14936b6a7..bde532c250b06 100644 --- a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java +++ b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/buildfile/AbstractGradleBuildFile.java @@ -24,7 +24,7 @@ public abstract class AbstractGradleBuildFile extends BuildFile { private final Path rootProjectPath; - private AtomicReference modelReference = new AtomicReference<>(); + private final AtomicReference modelReference = new AtomicReference<>(); public AbstractGradleBuildFile(final Path projectDirPath, final QuarkusPlatformDescriptor platformDescriptor) { this(projectDirPath, platformDescriptor, null); @@ -161,7 +161,7 @@ protected void refreshData() { this.modelReference.set(null); } - private boolean hasRootProjectFile(final String fileName) throws IOException { + private boolean hasRootProjectFile(final String fileName) { if (rootProjectPath == null) { return false; } @@ -207,17 +207,17 @@ private Model readModel() throws IOException { return new Model(settingsContent, buildContent, propertiesContent, rootSettingsContent, rootPropertiesContent); } - protected String getBuildContent() throws IOException { + protected String getBuildContent() { return getModel().getBuildContent(); } static class Model { private String settingsContent; private String buildContent; - private Properties propertiesContent; + private final Properties propertiesContent; - private String rootSettingsContent; - private Properties rootPropertiesContent; + private final String rootSettingsContent; + private final Properties rootPropertiesContent; public Model(String settingsContent, String buildContent, Properties propertiesContent, String rootSettingsContent, Properties rootPropertiesContent) {