From 1f9b22afd05d67f4fcf26202b88a06e9a2c64961 Mon Sep 17 00:00:00 2001 From: Cedric Champeau Date: Mon, 30 Jan 2023 14:58:02 +0100 Subject: [PATCH 1/2] Add support for "conventional" project names This commit adds the ability to use conventional project names, that is to say project names which start with `micronaut-`. This allows having publications which artifact id match the project name. Not having this is source of trouble for using composite builds, since Gradle wouldn't be able to determine the substitutions automatically. Changing the project names to match the artifact id fixes this issue, at the cost of some drawbacks: - full task paths will now use `:micronaut-xxx` as a prefix, instead of just `:xxx` - project dependencies will be broken when this feature is enabled. For example, a dependency on `project(":foo")` will have to be rewritten to `project(":micronaut-foo")` The change is _not_ applied to the root project (which is not published in any case), which will let us preserve the Gradle Enterprise history for that project. It is not applied either to projects which name starts with `test-suite`. This feature is _disabled by default_ for now, it needs to be enabled in the `settings.gradle` file, under the build extension: ```gradle micronautBuild { useStandardizedProjectNames = true } ``` --- gradle.properties | 2 +- .../micronaut/build/MicronautBomPlugin.java | 18 +++---- .../build/MicronautBuildCommonPlugin.groovy | 2 +- ...ndencyResolutionConfigurationPlugin.groovy | 39 --------------- ...pendencyResolutionConfigurationPlugin.java | 49 +++++++++++++++++++ .../build/MicronautModulePlugin.groovy | 2 +- .../build/MicronautPublishingPlugin.groovy | 5 +- .../graalvm/NativeImageSupportPlugin.java | 6 +-- .../MicronautBuildSettingsExtension.java | 26 +++++++--- .../MicronautGradleEnterprisePlugin.java | 3 +- .../io/micronaut/build/MicronautPlugin.java | 33 +++++++++++++ .../build/MicronautSharedSettingsPlugin.java | 36 ++++++++++++-- .../build/{ => utils}/ProviderUtils.java | 8 +-- .../{util => utils}/VersionHandling.java | 2 +- .../build/util/VersionHandlingTest.groovy | 1 + 15 files changed, 161 insertions(+), 71 deletions(-) delete mode 100644 src/main/groovy/io/micronaut/build/MicronautDependencyResolutionConfigurationPlugin.groovy create mode 100644 src/main/groovy/io/micronaut/build/MicronautDependencyResolutionConfigurationPlugin.java create mode 100644 src/main/java/io/micronaut/build/MicronautPlugin.java rename src/main/java/io/micronaut/build/{ => utils}/ProviderUtils.java (82%) rename src/main/java/io/micronaut/build/{util => utils}/VersionHandling.java (98%) diff --git a/gradle.properties b/gradle.properties index 3007b9e2..c7738625 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -projectVersion=6.2.2-SNAPSHOT +projectVersion=6.3.0-SNAPSHOT title=Micronaut Build Plugins projectDesc=Micronaut internal Gradle plugins. Not intended to be used in user's projects projectUrl=https://micronaut.io diff --git a/src/main/groovy/io/micronaut/build/MicronautBomPlugin.java b/src/main/groovy/io/micronaut/build/MicronautBomPlugin.java index 986e0560..b91a2f79 100644 --- a/src/main/groovy/io/micronaut/build/MicronautBomPlugin.java +++ b/src/main/groovy/io/micronaut/build/MicronautBomPlugin.java @@ -27,7 +27,6 @@ import org.gradle.api.Action; import org.gradle.api.GradleException; import org.gradle.api.InvalidUserDataException; -import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; @@ -79,7 +78,7 @@ * prefixed with `managed-`. */ @SuppressWarnings({"UnstableApiUsage", "HardCodedStringLiteral"}) -public abstract class MicronautBomPlugin implements Plugin { +public abstract class MicronautBomPlugin implements MicronautPlugin { public static final List DEPENDENCY_PATH = Arrays.asList("dependencyManagement", "dependencies", "dependency"); @@ -96,7 +95,7 @@ public void apply(Project project) { bomExtension.getPublishCatalog().convention(true); bomExtension.getIncludeBomInCatalog().convention(true); bomExtension.getImportProjectCatalog().convention(true); - bomExtension.getExcludeProject().convention(p -> p.getName().contains("bom") || p.getName().startsWith("test-suite") || !p.getSubprojects().isEmpty()); + bomExtension.getExcludeProject().convention(p -> p.getName().contains("bom") || p.getName().startsWith(TEST_SUITE_PROJECT_PREFIX) || !p.getSubprojects().isEmpty()); bomExtension.getExtraExcludedProjects().add(project.getName()); bomExtension.getCatalogToPropertyNameOverrides().convention(Collections.emptyMap()); bomExtension.getInlineNestedCatalogs().convention(true); @@ -205,7 +204,7 @@ private void configureBOM(Project project, MicronautBomExtension bomExtension) { private void configureLate(Project project, MicronautBomExtension bomExtension, PublishingExtension publishing, TaskContainer tasks) { String mainProjectId = bomExtension.getPropertyName().getOrElse(project.getRootProject().getName().replace("-parent", "").replace('-', '.')); - String publishedName = "micronaut-" + project.getName(); + String publishedName = MicronautPlugin.moduleNameOf(project.getName()); String group = String.valueOf(project.getGroup()); Optional versionCatalog = findVersionCatalog(project, bomExtension); final VersionCatalogConverter modelConverter = new VersionCatalogConverter( @@ -250,14 +249,15 @@ private void configureLate(Project project, MicronautBomExtension bomExtension, forEachProject(bomExtension, project, includedProjects, skippedProjects, p -> { String propertyName = "micronaut." + mainProjectId + ".version"; String projectGroup = String.valueOf(p.getGroup()); + String moduleName = MicronautPlugin.moduleNameOf(p.getName()); Optional pomDep = forEachNode(node, DEPENDENCY_PATH) - .filter(n -> childOf(n, "artifactId").text().equals("micronaut-" + p.getName()) && + .filter(n -> childOf(n, "artifactId").text().equals(moduleName) && childOf(n, "groupId").text().equals(projectGroup)) .findFirst(); if (pomDep.isPresent()) { childOf(pomDep.get(), "version").setValue("${" + propertyName + "}"); } else { - System.err.println("[WARNING] Didn't find dependency " + projectGroup + ":micronaut-" + p.getName() + " in BOM file"); + System.err.println("[WARNING] Didn't find dependency " + projectGroup + ":" + moduleName + " in BOM file"); } }); }); @@ -324,7 +324,7 @@ public void execute(Task task) { }); forEachProject(bomExtension, project, new HashSet<>(), new HashSet<>(), p -> { String moduleGroup = String.valueOf(p.getGroup()); - String moduleName = "micronaut-" + p.getName(); + String moduleName = MicronautPlugin.moduleNameOf(p.getName()); String moduleVersion = assertVersion(p); api.getDependencyConstraints().add( @@ -333,7 +333,7 @@ public void execute(Task task) { .create(moduleGroup + ":" + moduleName + ":" + moduleVersion) ); - String mainModuleName = "micronaut-" + mainProjectId.replace('.', '-'); + String mainModuleName = MicronautPlugin.moduleNameOf(mainProjectId.replace('.', '-')); modelConverter.getExtraVersions().put(mainModuleName, moduleVersion); modelConverter.getExtraLibraries().put(moduleName, VersionCatalogConverter.library(moduleGroup, moduleName, mainModuleName)); }); @@ -408,7 +408,7 @@ private void configureVersionCatalog(Project project, MicronautBomExtension bomE if (bomExtension.getIncludeBomInCatalog().get()) { CatalogPluginExtension catalog = project.getExtensions().getByType(CatalogPluginExtension.class); catalog.versionCatalog(vc -> { - String mainModuleName = "micronaut-" + mainProjectId; + String mainModuleName = MicronautPlugin.moduleNameOf(mainProjectId); String versionName = mainModuleName.replace('-', '.'); vc.library(publishedName, group, publishedName).versionRef(versionName); vc.version(versionName, String.valueOf(project.getVersion())); diff --git a/src/main/groovy/io/micronaut/build/MicronautBuildCommonPlugin.groovy b/src/main/groovy/io/micronaut/build/MicronautBuildCommonPlugin.groovy index 741d8c3d..130cabee 100644 --- a/src/main/groovy/io/micronaut/build/MicronautBuildCommonPlugin.groovy +++ b/src/main/groovy/io/micronaut/build/MicronautBuildCommonPlugin.groovy @@ -16,7 +16,7 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion import org.groovy.lang.groovydoc.tasks.GroovydocTask import static io.micronaut.build.BomSupport.coreBomArtifactId -import static io.micronaut.build.util.VersionHandling.versionProviderOrDefault +import static io.micronaut.build.utils.VersionHandling.versionProviderOrDefault /** * Micronaut internal Gradle plugin. Not intended to be used in user's projects. */ diff --git a/src/main/groovy/io/micronaut/build/MicronautDependencyResolutionConfigurationPlugin.groovy b/src/main/groovy/io/micronaut/build/MicronautDependencyResolutionConfigurationPlugin.groovy deleted file mode 100644 index 8fc2a492..00000000 --- a/src/main/groovy/io/micronaut/build/MicronautDependencyResolutionConfigurationPlugin.groovy +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2003-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License") - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.micronaut.build - -import groovy.transform.CompileStatic -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.initialization.Settings -import org.gradle.api.internal.GradleInternal - -@CompileStatic -class MicronautDependencyResolutionConfigurationPlugin implements Plugin { - @Override - void apply(Project project) { - addMavenCentral(project) - } - - private void addMavenCentral(Project project) { - // TODO: Avoid use of internal API - Settings settings = ((GradleInternal) project.gradle).settings - def repositoriesMode = settings.dependencyResolutionManagement.repositoriesMode.get() - if (repositoriesMode != repositoriesMode.FAIL_ON_PROJECT_REPOS) { - project.repositories.mavenCentral() - } - } -} diff --git a/src/main/groovy/io/micronaut/build/MicronautDependencyResolutionConfigurationPlugin.java b/src/main/groovy/io/micronaut/build/MicronautDependencyResolutionConfigurationPlugin.java new file mode 100644 index 00000000..0e006536 --- /dev/null +++ b/src/main/groovy/io/micronaut/build/MicronautDependencyResolutionConfigurationPlugin.java @@ -0,0 +1,49 @@ +package io.micronaut.build; + +import org.gradle.api.Project; +import org.gradle.api.artifacts.component.ComponentSelector; +import org.gradle.api.initialization.Settings; +import org.gradle.api.initialization.resolve.RepositoriesMode; +import org.gradle.api.internal.GradleInternal; + +public class MicronautDependencyResolutionConfigurationPlugin implements MicronautPlugin{ + + @Override + public void apply(Project project) { + addMavenCentral(project); + configureDependencySubstitutions(project); + } + + private void addMavenCentral(Project project) { + // TODO: Avoid use of internal API + Settings settings = ((GradleInternal) project.getGradle()).getSettings(); + RepositoriesMode repositoriesMode = settings.getDependencyResolutionManagement().getRepositoriesMode().get(); + if (!repositoriesMode.equals(repositoriesMode.FAIL_ON_PROJECT_REPOS)) { + project.getRepositories().mavenCentral(); + } + + } + + public void configureDependencySubstitutions(Project project) { + project.getGradle().settingsEvaluated(settings -> { + MicronautBuildSettingsExtension buildSettingsExtension = settings.getExtensions().getByType(MicronautBuildSettingsExtension.class); + project.getGradle().projectsEvaluated(unused -> project.getConfigurations().all(conf -> conf.getResolutionStrategy().dependencySubstitution(ds -> + project.getRootProject().getAllprojects().forEach(p -> { + String shortName = p.getName().replaceFirst(MICRONAUT_PROJECT_PREFIX, ""); + String group = String.valueOf(p.getGroup()); + if (group.startsWith(MICRONAUT_GROUP_ID) && !shortName.startsWith(TEST_SUITE_PROJECT_PREFIX)) { + ComponentSelector releasedModule = ds.module(group + ":" + MICRONAUT_PROJECT_PREFIX + shortName); + String projectPath = ":" + buildSettingsExtension.getUseStandardizedProjectNames().map(s -> s ? MICRONAUT_PROJECT_PREFIX : "").get() + shortName; + ComponentSelector localVersion = ds.project(projectPath); + if (shortName.endsWith(BOM_PROJECT_SUFFIX)) { + ds.substitute(ds.platform(releasedModule)).using(ds.platform(localVersion)).because("we want to test with what we're building"); + } else { + ds.substitute(releasedModule).using(localVersion).because("we want to test with what we're building"); + } + } + })) + )); + }); + } + +} diff --git a/src/main/groovy/io/micronaut/build/MicronautModulePlugin.groovy b/src/main/groovy/io/micronaut/build/MicronautModulePlugin.groovy index 5b440015..c891379c 100644 --- a/src/main/groovy/io/micronaut/build/MicronautModulePlugin.groovy +++ b/src/main/groovy/io/micronaut/build/MicronautModulePlugin.groovy @@ -5,7 +5,7 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.Dependency -import static io.micronaut.build.util.VersionHandling.versionProviderOrDefault +import static io.micronaut.build.utils.VersionHandling.versionProviderOrDefault /** * Configures a project as a typical Micronaut module project, * which implies that it's a library aimed at being processed diff --git a/src/main/groovy/io/micronaut/build/MicronautPublishingPlugin.groovy b/src/main/groovy/io/micronaut/build/MicronautPublishingPlugin.groovy index e74586a1..94102f83 100644 --- a/src/main/groovy/io/micronaut/build/MicronautPublishingPlugin.groovy +++ b/src/main/groovy/io/micronaut/build/MicronautPublishingPlugin.groovy @@ -14,6 +14,9 @@ import org.gradle.api.publish.maven.plugins.MavenPublishPlugin import org.gradle.api.tasks.bundling.Jar import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.plugins.signing.Sign + +import static io.micronaut.build.MicronautPlugin.moduleNameOf + /** * Micronaut internal Gradle plugin. Not intended to be used in user's projects. */ @@ -131,7 +134,7 @@ class MicronautPublishingPlugin implements Plugin { } publications { - String aid = "micronaut-" + project.name.substring(project.name.indexOf('/') + 1) + String aid = moduleNameOf(project.name.substring(project.name.indexOf('/') + 1)) if (project.extensions.findByType(PublishingExtension).publications.empty) { maven(MavenPublication) { publication -> artifactId( aid ) diff --git a/src/main/groovy/io/micronaut/build/graalvm/NativeImageSupportPlugin.java b/src/main/groovy/io/micronaut/build/graalvm/NativeImageSupportPlugin.java index f392efca..a1299491 100644 --- a/src/main/groovy/io/micronaut/build/graalvm/NativeImageSupportPlugin.java +++ b/src/main/groovy/io/micronaut/build/graalvm/NativeImageSupportPlugin.java @@ -17,7 +17,7 @@ import io.micronaut.build.MicronautBuildExtension; import io.micronaut.build.MicronautBuildExtensionPlugin; -import org.gradle.api.Plugin; +import io.micronaut.build.MicronautPlugin; import org.gradle.api.Project; import org.gradle.api.plugins.ExtensionAware; import org.gradle.api.plugins.JavaPluginExtension; @@ -25,7 +25,7 @@ import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskProvider; -public class NativeImageSupportPlugin implements Plugin { +public class NativeImageSupportPlugin implements MicronautPlugin { public static final String NATIVE_IMAGE_PROPERTIES_EXTENSION_NAME = "nativeImageProperties"; public static final String GENERATE_NATIVE_IMAGE_PROPERTIES_TASK_NAME = "generateNativeImageProperties"; @@ -45,7 +45,7 @@ public void apply(Project project) { task.getInitializeAtBuildtime().convention(nativeImageExtension.getInitializeAtBuildtime()); task.getFeatures().convention(nativeImageExtension.getFeatures()); task.getGroupId().convention(project.getProviders().provider(() -> String.valueOf(project.getGroup()))); - task.getArtifactId().convention(project.getProviders().provider(() -> "micronaut-" + project.getName())); + task.getArtifactId().convention(project.getProviders().provider(() -> MicronautPlugin.moduleNameOf(project.getName()))); }); pluginManager.withPlugin("java", unused -> { SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets(); diff --git a/src/main/java/io/micronaut/build/MicronautBuildSettingsExtension.java b/src/main/java/io/micronaut/build/MicronautBuildSettingsExtension.java index 3a3b7db4..c3767e1b 100644 --- a/src/main/java/io/micronaut/build/MicronautBuildSettingsExtension.java +++ b/src/main/java/io/micronaut/build/MicronautBuildSettingsExtension.java @@ -42,6 +42,8 @@ import java.util.stream.Collectors; import static io.micronaut.build.BomSupport.coreBomArtifactId; +import static io.micronaut.build.MicronautPlugin.BOM_PROJECT_SUFFIX; +import static io.micronaut.build.MicronautPlugin.MICRONAUT_PROJECT_PREFIX; public abstract class MicronautBuildSettingsExtension { private static final Logger LOGGER = LoggerFactory.getLogger(MicronautBuildSettingsExtension.class); @@ -50,6 +52,17 @@ public abstract class MicronautBuildSettingsExtension { abstract Property getUseRemoteCache(); + /** + * Configures use of "standard" project names. When set to "true", + * project names will be automatically configured to start with "micronaut-", + * except for test suites. + * This makes it cleaner for publications, since the project name will match + * the publication artifact id, making it easier to use composite builds. + * Defaults to false. + * @return the standardized project names property + */ + abstract Property getUseStandardizedProjectNames(); + @Inject protected abstract ProviderFactory getProviders(); @@ -64,6 +77,7 @@ public MicronautBuildSettingsExtension(ProviderFactory providers, Settings setti this.settings = settings; getUseLocalCache().convention(booleanProvider(providers, "localCache", true)); getUseRemoteCache().convention(booleanProvider(providers, "remoteCache", true)); + getUseStandardizedProjectNames().convention(false); this.versionCatalogTomlModel = loadVersionCatalogTomlModel(); this.micronautVersion = determineMicronautVersion(); this.micronautTestVersion = determineMicronautTestVersion(); @@ -171,10 +185,10 @@ public void importMicronautCatalogFromGAV(String gavCoordinates) { } if (parts.size() == 3) { String name = "mn"; - if (artifactId.startsWith("micronaut-")) { - artifactId = artifactId.substring("micronaut-".length()); + if (artifactId.startsWith(MICRONAUT_PROJECT_PREFIX)) { + artifactId = artifactId.substring(MICRONAUT_PROJECT_PREFIX.length()); } - if (artifactId.endsWith("-bom")) { + if (artifactId.endsWith(BOM_PROJECT_SUFFIX)) { artifactId = artifactId.substring(0, artifactId.length() - 4); } name += Arrays.stream(artifactId.split("-")) @@ -228,10 +242,10 @@ public void importMicronautCatalog(String alias) { } if (parts.size() == 3) { String name = "mn"; - if (artifactId.startsWith("micronaut-")) { - artifactId = artifactId.substring("micronaut-".length()); + if (artifactId.startsWith(MICRONAUT_PROJECT_PREFIX)) { + artifactId = artifactId.substring(MICRONAUT_PROJECT_PREFIX.length()); } - if (artifactId.endsWith("-bom")) { + if (artifactId.endsWith(BOM_PROJECT_SUFFIX)) { artifactId = artifactId.substring(0, artifactId.length() - 4); } name += Arrays.stream(artifactId.split("-")) diff --git a/src/main/java/io/micronaut/build/MicronautGradleEnterprisePlugin.java b/src/main/java/io/micronaut/build/MicronautGradleEnterprisePlugin.java index 99e332b6..35eea4e2 100644 --- a/src/main/java/io/micronaut/build/MicronautGradleEnterprisePlugin.java +++ b/src/main/java/io/micronaut/build/MicronautGradleEnterprisePlugin.java @@ -19,6 +19,7 @@ import com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension; import com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin; import com.gradle.scan.plugin.BuildScanExtension; +import io.micronaut.build.utils.ProviderUtils; import org.gradle.api.Plugin; import org.gradle.api.initialization.Settings; import org.gradle.api.invocation.Gradle; @@ -32,7 +33,7 @@ import java.lang.reflect.InvocationTargetException; import static io.micronaut.build.BuildEnvironment.PREDICTIVE_TEST_SELECTION_ENV_VAR; -import static io.micronaut.build.ProviderUtils.envOrSystemProperty; +import static io.micronaut.build.utils.ProviderUtils.envOrSystemProperty; public class MicronautGradleEnterprisePlugin implements Plugin { private static final String[] SAFE_TO_LOG_ENV_VARIABLES = new String[] { diff --git a/src/main/java/io/micronaut/build/MicronautPlugin.java b/src/main/java/io/micronaut/build/MicronautPlugin.java new file mode 100644 index 00000000..8972a0ee --- /dev/null +++ b/src/main/java/io/micronaut/build/MicronautPlugin.java @@ -0,0 +1,33 @@ +/* + * Copyright 2003-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micronaut.build; + +import org.gradle.api.Plugin; + +public interface MicronautPlugin extends Plugin { + + String TEST_SUITE_PROJECT_PREFIX = "test-suite"; + String MICRONAUT_PROJECT_PREFIX = "micronaut-"; + String BOM_PROJECT_SUFFIX = "-bom"; + String MICRONAUT_GROUP_ID = "io.micronaut"; + + static String moduleNameOf(String projectName) { + if (projectName.startsWith(MICRONAUT_PROJECT_PREFIX)) { + return projectName; + } + return MICRONAUT_PROJECT_PREFIX + projectName; + } +} diff --git a/src/main/java/io/micronaut/build/MicronautSharedSettingsPlugin.java b/src/main/java/io/micronaut/build/MicronautSharedSettingsPlugin.java index 9e9f2041..7e7ee6c5 100644 --- a/src/main/java/io/micronaut/build/MicronautSharedSettingsPlugin.java +++ b/src/main/java/io/micronaut/build/MicronautSharedSettingsPlugin.java @@ -17,8 +17,8 @@ import io.github.gradlenexus.publishplugin.NexusPublishExtension; import org.gradle.api.InvalidUserCodeException; -import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.initialization.ProjectDescriptor; import org.gradle.api.initialization.Settings; import org.gradle.api.invocation.Gradle; import org.gradle.api.plugins.ExtraPropertiesExtension; @@ -29,12 +29,14 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; -import static io.micronaut.build.ProviderUtils.envOrSystemProperty; +import static io.micronaut.build.utils.ProviderUtils.envOrSystemProperty; @SuppressWarnings("unused") -public class MicronautSharedSettingsPlugin implements Plugin { +public class MicronautSharedSettingsPlugin implements MicronautPlugin{ public static final String NEXUS_STAGING_PROFILE_ID = "11bd7bc41716aa"; @Override @@ -43,9 +45,10 @@ public void apply(Settings settings) { settings.getGradle().getSharedServices().registerIfAbsent(InternalStateCheckingService.NAME, InternalStateCheckingService.class, spec -> spec.parameters(p -> p.getRegisteredByProjectPlugin().set(false))).get(); pluginManager.apply(MicronautBuildSettingsPlugin.class); pluginManager.apply(MicronautGradleEnterprisePlugin.class); + MicronautBuildSettingsExtension buildSettingsExtension = settings.getExtensions().findByType(MicronautBuildSettingsExtension.class); applyPublishingPlugin(settings); + configureProjectNames(settings, buildSettingsExtension); assertUniqueProjectNames(settings); - MicronautBuildSettingsExtension buildSettingsExtension = settings.getExtensions().findByType(MicronautBuildSettingsExtension.class); settings.getGradle().beforeProject(p -> { ExtraPropertiesExtension extraProperties = p.getExtensions().getExtraProperties(); if (!extraProperties.has("micronautVersion")) { @@ -54,6 +57,31 @@ public void apply(Settings settings) { }); } + private void configureProjectNames(Settings settings, MicronautBuildSettingsExtension buildSettings) { + settings.getGradle().settingsEvaluated(unused -> { + Boolean useStandardProjectNames = buildSettings.getUseStandardizedProjectNames().get(); + if (Boolean.TRUE.equals(useStandardProjectNames)) { + Set visited = new HashSet<>(); + configureProjectName(settings.getRootProject(), visited); + } + }); + } + + private void configureProjectName(ProjectDescriptor project, Set visited) { + if (visited.add(project)) { + if (!(":".equals(project.getPath()))) { + String name = project.getName(); + if (!name.startsWith(MICRONAUT_PROJECT_PREFIX) && !name.startsWith(TEST_SUITE_PROJECT_PREFIX)) { + name = MICRONAUT_PROJECT_PREFIX + name; + project.setName(name); + } + } + for (ProjectDescriptor child : project.getChildren()) { + configureProjectName(child, visited); + } + } + } + private void assertUniqueProjectNames(Settings settings) { settings.getGradle().projectsLoaded(gradle -> { Map duplicates = new HashMap<>(); diff --git a/src/main/java/io/micronaut/build/ProviderUtils.java b/src/main/java/io/micronaut/build/utils/ProviderUtils.java similarity index 82% rename from src/main/java/io/micronaut/build/ProviderUtils.java rename to src/main/java/io/micronaut/build/utils/ProviderUtils.java index 12a9f6f8..1d4983a4 100644 --- a/src/main/java/io/micronaut/build/ProviderUtils.java +++ b/src/main/java/io/micronaut/build/utils/ProviderUtils.java @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.micronaut.build; +package io.micronaut.build.utils; import org.gradle.api.provider.ProviderFactory; -class ProviderUtils { - static boolean guessCI(ProviderFactory providers) { +public class ProviderUtils { + public static boolean guessCI(ProviderFactory providers) { return providers .environmentVariable("CI") .flatMap(s -> // Not all workflows may have the enterprise key set @@ -30,7 +30,7 @@ static boolean guessCI(ProviderFactory providers) { .get(); } - static String envOrSystemProperty(ProviderFactory providers, String envName, String propertyName, String defaultValue) { + public static String envOrSystemProperty(ProviderFactory providers, String envName, String propertyName, String defaultValue) { return providers.environmentVariable(envName) .orElse(providers.gradleProperty(propertyName)) .getOrElse(defaultValue); diff --git a/src/main/java/io/micronaut/build/util/VersionHandling.java b/src/main/java/io/micronaut/build/utils/VersionHandling.java similarity index 98% rename from src/main/java/io/micronaut/build/util/VersionHandling.java rename to src/main/java/io/micronaut/build/utils/VersionHandling.java index 0fed6d64..ddc43026 100644 --- a/src/main/java/io/micronaut/build/util/VersionHandling.java +++ b/src/main/java/io/micronaut/build/utils/VersionHandling.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.micronaut.build.util; +package io.micronaut.build.utils; import org.gradle.api.Project; import org.gradle.api.artifacts.VersionCatalogsExtension; diff --git a/src/test/groovy/io/micronaut/build/util/VersionHandlingTest.groovy b/src/test/groovy/io/micronaut/build/util/VersionHandlingTest.groovy index f1f7012b..ed13ab00 100644 --- a/src/test/groovy/io/micronaut/build/util/VersionHandlingTest.groovy +++ b/src/test/groovy/io/micronaut/build/util/VersionHandlingTest.groovy @@ -1,5 +1,6 @@ package io.micronaut.build.util +import io.micronaut.build.utils.VersionHandling import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification From 1e86778133024d444c47671c374edd8b3266fc71 Mon Sep 17 00:00:00 2001 From: Cedric Champeau Date: Mon, 30 Jan 2023 18:15:25 +0100 Subject: [PATCH 2/2] Add warning when std project names are disabled --- .../build/MicronautSharedSettingsPlugin.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/micronaut/build/MicronautSharedSettingsPlugin.java b/src/main/java/io/micronaut/build/MicronautSharedSettingsPlugin.java index 7e7ee6c5..9e379799 100644 --- a/src/main/java/io/micronaut/build/MicronautSharedSettingsPlugin.java +++ b/src/main/java/io/micronaut/build/MicronautSharedSettingsPlugin.java @@ -21,10 +21,12 @@ import org.gradle.api.initialization.ProjectDescriptor; import org.gradle.api.initialization.Settings; import org.gradle.api.invocation.Gradle; +import org.gradle.api.logging.Logging; import org.gradle.api.plugins.ExtraPropertiesExtension; import org.gradle.api.plugins.PluginManager; import org.gradle.api.provider.Provider; import org.gradle.api.provider.ProviderFactory; +import org.slf4j.Logger; import java.net.URI; import java.net.URISyntaxException; @@ -36,8 +38,21 @@ import static io.micronaut.build.utils.ProviderUtils.envOrSystemProperty; @SuppressWarnings("unused") -public class MicronautSharedSettingsPlugin implements MicronautPlugin{ +public class MicronautSharedSettingsPlugin implements MicronautPlugin { + private static final Logger LOGGER = Logging.getLogger(MicronautSharedSettingsPlugin.class); + public static final String NEXUS_STAGING_PROFILE_ID = "11bd7bc41716aa"; + private static final String STANDARDIZED_PROJECT_NAMES_WARNING = "=========================================================\n" + + "Standardized project names are disabled.\n" + + "Consider enabling them with\n" + + " micronautBuild.useStandardizedProjectNames=true\n" + + "in settings.gradle.\n\n" + + "Then you will need to replace project dependencies from\n" + + " project(':foo')\n" + + "to\n" + + " project(':micronaut-foo')\n" + + "in your build scripts\n" + + "========================================================="; @Override public void apply(Settings settings) { @@ -63,6 +78,8 @@ private void configureProjectNames(Settings settings, MicronautBuildSettingsExte if (Boolean.TRUE.equals(useStandardProjectNames)) { Set visited = new HashSet<>(); configureProjectName(settings.getRootProject(), visited); + } else { + LOGGER.warn(STANDARDIZED_PROJECT_NAMES_WARNING); } }); }