Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for "conventional" project names #477

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/main/groovy/io/micronaut/build/MicronautBomPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +78,7 @@
* prefixed with `managed-`.
*/
@SuppressWarnings({"UnstableApiUsage", "HardCodedStringLiteral"})
public abstract class MicronautBomPlugin implements Plugin<Project> {
public abstract class MicronautBomPlugin implements MicronautPlugin<Project> {

public static final List<String> DEPENDENCY_PATH = Arrays.asList("dependencyManagement", "dependencies", "dependency");

Expand All @@ -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);
Expand Down Expand Up @@ -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> versionCatalog = findVersionCatalog(project, bomExtension);
final VersionCatalogConverter modelConverter = new VersionCatalogConverter(
Expand Down Expand Up @@ -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<Node> 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");
}
});
});
Expand Down Expand Up @@ -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(
Expand All @@ -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));
});
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Project>{

@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");
}
}
}))
));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -131,7 +134,7 @@ class MicronautPublishingPlugin implements Plugin<Project> {

}
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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

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;
import org.gradle.api.plugins.PluginManager;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;

public class NativeImageSupportPlugin implements Plugin<Project> {
public class NativeImageSupportPlugin implements MicronautPlugin<Project> {

public static final String NATIVE_IMAGE_PROPERTIES_EXTENSION_NAME = "nativeImageProperties";
public static final String GENERATE_NATIVE_IMAGE_PROPERTIES_TASK_NAME = "generateNativeImageProperties";
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -50,6 +52,17 @@ public abstract class MicronautBuildSettingsExtension {

abstract Property<Boolean> 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<Boolean> getUseStandardizedProjectNames();

@Inject
protected abstract ProviderFactory getProviders();

Expand All @@ -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();
Expand Down Expand Up @@ -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("-"))
Expand Down Expand Up @@ -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("-"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Settings> {
private static final String[] SAFE_TO_LOG_ENV_VARIABLES = new String[] {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/io/micronaut/build/MicronautPlugin.java
Original file line number Diff line number Diff line change
@@ -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<T> extends Plugin<T> {

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;
}
}
Loading