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..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); @@ -61,11 +61,29 @@ 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", 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(":").append(coords.getVersion()) + .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 +127,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 +135,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) { @@ -139,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; } @@ -185,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) { 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'");