diff --git a/ihub-base/src/main/groovy/pub/ihub/plugin/IHubLibsVersions.groovy b/ihub-base/src/main/groovy/pub/ihub/plugin/IHubLibsVersions.groovy index 7a6c31d6..d56f9312 100644 --- a/ihub-base/src/main/groovy/pub/ihub/plugin/IHubLibsVersions.groovy +++ b/ihub-base/src/main/groovy/pub/ihub/plugin/IHubLibsVersions.groovy @@ -27,6 +27,9 @@ import static org.gradle.api.JavaVersion.current @CompileStatic class IHubLibsVersions { + static final String IHUB_PLUGINS_LOCAL_VERSION = 'iHub.iHubPluginsLocalVersion' + static final String IHUB_LIBS_LOCAL_VERSION = 'iHub.iHubLibsLocalVersion' + static final Map LIBS_VERSIONS = readLines(IHubLibsVersions.classLoader.getResource('META-INF/ihub/libs-versions')) .collectEntries { it.split '=' } @@ -39,4 +42,12 @@ class IHubLibsVersions { current().isCompatibleWith(JavaVersion.VERSION_17) ? getLibsVersion(name) : getLibsVersion(name) + '-java11' } + static String getIHubPluginsVersion() { + getLibsVersion 'ihub-plugins' + } + + static String getIHubLibsVersion() { + getCompatibleLibsVersion 'ihub-libs' + } + } diff --git a/ihub-base/src/test/groovy/pub/ihub/plugin/IHubLibsVersionsTest.groovy b/ihub-base/src/test/groovy/pub/ihub/plugin/IHubLibsVersionsTest.groovy index 04b8d31d..cf7791d1 100644 --- a/ihub-base/src/test/groovy/pub/ihub/plugin/IHubLibsVersionsTest.groovy +++ b/ihub-base/src/test/groovy/pub/ihub/plugin/IHubLibsVersionsTest.groovy @@ -25,12 +25,17 @@ import spock.lang.Title @Title('IHubLibsVersions测试套件') class IHubLibsVersionsTest extends Specification { + def '测试插件版本号'() { + expect: + IHubLibsVersions.IHubPluginsVersion + } + def '测试组件版本包含ihub'() { setup: System.setProperty 'java.version', javaVersion expect: - IHubLibsVersions.getCompatibleLibsVersion('ihub-libs') ==~ expected + IHubLibsVersions.IHubLibsVersion ==~ expected cleanup: JavaVersion.resetCurrent() diff --git a/ihub-bom/src/main/groovy/pub/ihub/plugin/bom/IHubBomPlugin.groovy b/ihub-bom/src/main/groovy/pub/ihub/plugin/bom/IHubBomPlugin.groovy index 3a4f745d..d0ddcd04 100644 --- a/ihub-bom/src/main/groovy/pub/ihub/plugin/bom/IHubBomPlugin.groovy +++ b/ihub-bom/src/main/groovy/pub/ihub/plugin/bom/IHubBomPlugin.groovy @@ -23,7 +23,8 @@ import org.gradle.api.plugins.catalog.VersionCatalogPlugin import pub.ihub.plugin.IHubPlugin import pub.ihub.plugin.IHubProjectPluginAware -import static pub.ihub.plugin.IHubLibsVersions.getCompatibleLibsVersion +import static pub.ihub.plugin.IHubLibsVersions.IHUB_LIBS_LOCAL_VERSION +import static pub.ihub.plugin.IHubLibsVersions.IHubLibsVersion import static pub.ihub.plugin.IHubProjectPluginAware.EvaluateStage.AFTER /** @@ -54,8 +55,9 @@ class IHubBomPlugin extends IHubProjectPluginAware { // 项目不包含dependencies组件时,自动配置ihub-bom if (!project.hasProperty('iHubSettings.includeDependencies')) { + def iHubLibsVersion = project.findProperty(IHUB_LIBS_LOCAL_VERSION) ?: IHubLibsVersion extension.importBoms { - group 'pub.ihub.lib' module 'ihub-dependencies' version getCompatibleLibsVersion('ihub-libs') + group 'pub.ihub.lib' module 'ihub-dependencies' version iHubLibsVersion } } diff --git a/ihub-bom/src/test/groovy/pub/ihub/plugin/bom/IHubBomPluginTest.groovy b/ihub-bom/src/test/groovy/pub/ihub/plugin/bom/IHubBomPluginTest.groovy index 6cc244ff..afce6105 100644 --- a/ihub-bom/src/test/groovy/pub/ihub/plugin/bom/IHubBomPluginTest.groovy +++ b/ihub-bom/src/test/groovy/pub/ihub/plugin/bom/IHubBomPluginTest.groovy @@ -329,4 +329,14 @@ class IHubBomPluginTest extends IHubSpecification { result.output.contains 'BUILD SUCCESSFUL' } + def 'iHubLibsLocalVersion配置测试'() { + when: '配置组件依赖为空' + copyProject 'bom.gradle' + propertiesFile << 'iHub.iHubLibsLocalVersion=test-SNAPSHOT' + def result = gradleBuilder.build() + then: '检查结果' + result.output.contains('test-SNAPSHOT') + result.output.contains 'BUILD SUCCESSFUL' + } + } diff --git a/ihub-javaagent/src/main/groovy/pub/ihub/plugin/javaagent/IHubJavaagentPlugin.groovy b/ihub-javaagent/src/main/groovy/pub/ihub/plugin/javaagent/IHubJavaagentPlugin.groovy index 1236e0b6..8b2f48be 100644 --- a/ihub-javaagent/src/main/groovy/pub/ihub/plugin/javaagent/IHubJavaagentPlugin.groovy +++ b/ihub-javaagent/src/main/groovy/pub/ihub/plugin/javaagent/IHubJavaagentPlugin.groovy @@ -17,6 +17,7 @@ package pub.ihub.plugin.javaagent import com.ryandens.javaagent.JavaagentApplicationPlugin import com.ryandens.javaagent.JavaagentBasePlugin +import groovy.transform.CompileStatic import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.Dependency @@ -36,6 +37,7 @@ import static pub.ihub.plugin.IHubProjectPluginAware.EvaluateStage.AFTER @IHubPlugin(value = IHubJavaagentExtension, beforeApplyPlugins = IHubBomPlugin) class IHubJavaagentPlugin extends IHubProjectPluginAware { + @CompileStatic @Override protected void apply() { if (hasPlugin(ApplicationPlugin)) { @@ -44,23 +46,27 @@ class IHubJavaagentPlugin extends IHubProjectPluginAware if (project.plugins.hasPlugin('org.springframework.boot')) { configureJavaExec 'bootRun' } - withExtension(AFTER) { ext -> - if (ext.javaagent.present) { - Dependency dependency = project.dependencies.create ext.javaagent.get() - project.dependencies { - javaagent group: dependency.group, name: dependency.name, version: dependency.version, - classifier: ext.classifier.get() - } + withExtension AFTER, this::configureJavaagent + } + + private void configureJavaagent(IHubJavaagentExtension ext) { + if (ext.javaagent.present) { + Dependency dependency = project.dependencies.create ext.javaagent.get() + project.dependencies { + javaagent group: dependency.group, name: dependency.name, version: dependency.version, + classifier: ext.classifier.get() } } } + @CompileStatic private void configureJavaExec(String taskName) { applyPlugin JavaagentBasePlugin as Class> - project.tasks.named(taskName, JavaExec).configure javaExecClosure + project.tasks.named(taskName, JavaExec).configure this::configJavaExec } - private final Closure javaExecClosure = { JavaExec exec -> + @CompileStatic + private void configJavaExec(JavaExec exec) { configureJavaForkOptions exec, project.configurations.named(CONFIGURATION_NAME).map { it.files } } diff --git a/ihub-javaagent/src/test/groovy/pub/ihub/plugin/javaagent/IHubJavaagentPluginTest.groovy b/ihub-javaagent/src/test/groovy/pub/ihub/plugin/javaagent/IHubJavaagentPluginTest.groovy index 96d8371e..711ec0ce 100644 --- a/ihub-javaagent/src/test/groovy/pub/ihub/plugin/javaagent/IHubJavaagentPluginTest.groovy +++ b/ihub-javaagent/src/test/groovy/pub/ihub/plugin/javaagent/IHubJavaagentPluginTest.groovy @@ -68,9 +68,7 @@ class IHubJavaagentPluginTest extends IHubSpecification { project.pluginManager.apply 'org.springframework.boot' def bootRun = project.tasks.register 'bootRun', JavaExec project.pluginManager.apply IHubJavaagentPlugin - project.plugins.getPlugin(IHubJavaagentPlugin).javaExecClosure bootRun.get() - - then: '检查结果' + project.plugins.getPlugin(IHubJavaagentPlugin).configJavaExec bootRun.get() then: '检查结果' project.plugins.hasPlugin JavaagentBasePlugin diff --git a/ihub-plugins/src/main/groovy/pub/ihub/plugin/IHubPluginsExtension.groovy b/ihub-plugins/src/main/groovy/pub/ihub/plugin/IHubPluginsExtension.groovy index dd69f64e..bbb40e9f 100644 --- a/ihub-plugins/src/main/groovy/pub/ihub/plugin/IHubPluginsExtension.groovy +++ b/ihub-plugins/src/main/groovy/pub/ihub/plugin/IHubPluginsExtension.groovy @@ -44,6 +44,12 @@ interface IHubPluginsExtension extends IHubExtensionAware { @IHubProperty(type = [PROJECT, SYSTEM, ENV], defaultValue = 'false', genericType = Boolean) Property getMavenAliYunEnabled() + /** + * 是否启用SpringMilestone仓库 + */ + @IHubProperty(type = [PROJECT, SYSTEM, ENV], defaultValue = 'false', genericType = Boolean) + Property getMavenSpringMilestoneEnabled() + /** * 是否启用私有仓库(组件发布仓库) */ diff --git a/ihub-plugins/src/main/groovy/pub/ihub/plugin/IHubPluginsPlugin.groovy b/ihub-plugins/src/main/groovy/pub/ihub/plugin/IHubPluginsPlugin.groovy index 8e5b1964..99624ffa 100644 --- a/ihub-plugins/src/main/groovy/pub/ihub/plugin/IHubPluginsPlugin.groovy +++ b/ihub-plugins/src/main/groovy/pub/ihub/plugin/IHubPluginsPlugin.groovy @@ -75,6 +75,9 @@ class IHubPluginsPlugin extends IHubProjectPluginAware { maven mavenRepo('AliYunPublic', 'https://maven.aliyun.com/repository/public', 'https://repo1.maven.org/maven2') } + if (ext.mavenSpringMilestoneEnabled.get()) { + maven mavenRepo('SpringMilestone', 'https://repo.spring.io/milestone') + } // 添加私有仓库 if (ext.mavenPrivateEnabled.get()) { ext.releaseRepoUrl.orNull?.with { url -> maven mavenRepo('ReleaseRepo', url, ext) { releasesOnly() } } diff --git a/ihub-plugins/src/test/groovy/pub/ihub/plugin/IHubPluginsPluginTest.groovy b/ihub-plugins/src/test/groovy/pub/ihub/plugin/IHubPluginsPluginTest.groovy index 2cf63469..6f43566b 100644 --- a/ihub-plugins/src/test/groovy/pub/ihub/plugin/IHubPluginsPluginTest.groovy +++ b/ihub-plugins/src/test/groovy/pub/ihub/plugin/IHubPluginsPluginTest.groovy @@ -78,6 +78,7 @@ plugins { propertiesFile << ''' iHub.mavenLocalEnabled=true iHub.mavenAliYunEnabled=true +iHub.mavenSpringMilestoneEnabled=true iHub.releaseRepoUrl=https://ihub.pub/nexus/content/repositories/releases iHub.snapshotRepoUrl=https://ihub.pub/nexus/content/repositories/snapshots iHub.customizeRepoUrl=https://ihub.pub/nexus/content/repositories @@ -204,4 +205,20 @@ iHub.repoIncludeGroupRegex=pub\\.ihub\\..* false | false } + def 'cleanRootProject'() { + setup: '初始化项目' + copyProject 'basic.gradle' + settingsFile << 'include \'a\', \'b\', \'c\'' + testProjectDir.newFolder 'a' + testProjectDir.newFolder 'b' + testProjectDir.newFolder 'c' + testProjectDir.newFile('a/build.gradle') << 'task(\'clean\') {}' + + when: '构建项目' + def result = gradleBuilder.withArguments('cleanRootProject').build() + + then: '检查结果' + result.output.contains 'BUILD SUCCESSFUL' + } + } diff --git a/ihub-settings/src/main/groovy/pub/ihub/plugin/IHubSettingsPlugin.groovy b/ihub-settings/src/main/groovy/pub/ihub/plugin/IHubSettingsPlugin.groovy index 20f6e1c7..eabbb62b 100644 --- a/ihub-settings/src/main/groovy/pub/ihub/plugin/IHubSettingsPlugin.groovy +++ b/ihub-settings/src/main/groovy/pub/ihub/plugin/IHubSettingsPlugin.groovy @@ -38,6 +38,10 @@ import java.nio.file.Path import static java.lang.Boolean.valueOf import static org.codehaus.groovy.runtime.ResourceGroovyMethods.readLines import static org.gradle.api.JavaVersion.current +import static pub.ihub.plugin.IHubLibsVersions.IHUB_LIBS_LOCAL_VERSION +import static pub.ihub.plugin.IHubLibsVersions.IHUB_PLUGINS_LOCAL_VERSION +import static pub.ihub.plugin.IHubLibsVersions.IHubLibsVersion +import static pub.ihub.plugin.IHubLibsVersions.IHubPluginsVersion import static pub.ihub.plugin.IHubLibsVersions.getLibsVersion import static pub.ihub.plugin.IHubPluginMethods.printLineConfigContent import static pub.ihub.plugin.IHubPluginMethods.printMapConfigContent @@ -51,7 +55,6 @@ class IHubSettingsPlugin implements Plugin { private static final List IHUB_PLUGIN_IDS = readLines IHubSettingsPlugin .classLoader.getResource('META-INF/ihub/plugin-ids') - private static final String IHUB_PLUGIN_VERSION = getLibsVersion 'ihub-plugins' private static final Map PLUGIN_ALIAS_IDS = [ 'plugin-publish': 'com.gradle.plugin-publish', @@ -74,7 +77,7 @@ class IHubSettingsPlugin implements Plugin { settings.pluginManagement { plugins { (IHUB_PLUGIN_IDS.collectEntries { - [(it): IHUB_PLUGIN_VERSION] + [(it): getIHubPluginVersion(settings)] } + PLUGIN_VERSIONS).findAll { it.value }.each { key, value -> id key version value } @@ -110,47 +113,49 @@ class IHubSettingsPlugin implements Plugin { settings.pluginManager.apply PluginVersionsPlugin } - private void configPluginRepositories(Settings settings) { - settings.pluginManagement { - repositories { - String dirs = "$settings.rootProject.projectDir/gradle/plugins" - if ((dirs as File).directory) { - flatDir dirs: dirs - } - if (valueOf findProperty(settings, 'iHub.mavenLocalEnabled')) { - mavenLocal() + private static void configPluginRepositories(Settings settings) { + configRepositories settings.pluginManagement, settings + printLineConfigContent 'Gradle Plugin Repos', settings.pluginManagement.repositories*.displayName + } + + private static void configRepositories(management, Settings settings) { + management.repositories { + String dirs = "$settings.rootProject.projectDir/gradle/plugins" + if ((dirs as File).directory) { + flatDir dirs: dirs + } + if (valueOf findProperty(settings, 'iHub.mavenLocalEnabled')) { + mavenLocal() + } + if (valueOf findProperty(settings, 'iHub.mavenAliYunEnabled')) { + maven { + name 'AliYunGradle' + url 'https://maven.aliyun.com/repository/gradle-plugin' + artifactUrls 'https://plugins.gradle.org/m2' } - if (valueOf findProperty(settings, 'iHub.mavenAliYunEnabled')) { - maven { - name 'AliYunGradle' - url 'https://maven.aliyun.com/repository/gradle-plugin' - artifactUrls 'https://plugins.gradle.org/m2' - } + } + gradlePluginPortal() + mavenCentral() + // 添加私有仓库 + if (valueOf findProperty(settings, 'iHub.mavenPrivateEnabled', 'true')) { + findProperty(settings, 'iHub.releaseRepoUrl')?.with { repoUrl -> + maven mavenRepo(settings, 'ReleaseRepo', repoUrl, true) } - gradlePluginPortal() - mavenCentral() - // 添加私有仓库 - if (valueOf findProperty(settings, 'iHub.mavenPrivateEnabled', 'true')) { - findProperty(settings, 'iHub.releaseRepoUrl')?.with { repoUrl -> - maven mavenRepo(settings, 'ReleaseRepo', repoUrl, true) - } - findProperty(settings, 'iHub.snapshotRepoUrl')?.with { repoUrl -> - maven mavenRepo(settings, 'SnapshotRepo', repoUrl, false) - } + findProperty(settings, 'iHub.snapshotRepoUrl')?.with { repoUrl -> + maven mavenRepo(settings, 'SnapshotRepo', repoUrl, false) } - // 添加自定义仓库 - findProperty(settings, 'iHub.customizeRepoUrl')?.with { repoUrl -> - maven { - name 'CustomizeRepo' - url repoUrl - } + } + // 添加自定义仓库 + findProperty(settings, 'iHub.customizeRepoUrl')?.with { repoUrl -> + maven { + name 'CustomizeRepo' + url repoUrl } } - printLineConfigContent 'Gradle Plugin Repos', settings.pluginManagement.repositories*.displayName } } - private Closure mavenRepo(Settings settings, String repoName, String repoUrl, boolean releases) { + private static Closure mavenRepo(Settings settings, String repoName, String repoUrl, boolean releases) { return { name repoName url repoUrl @@ -234,9 +239,7 @@ class IHubSettingsPlugin implements Plugin { private static void configVersionCatalogs(Settings settings, IHubSettingsExtension ext) { settings.dependencyResolutionManagement { - repositories { - mavenCentral() - } + configRepositories it, settings // 配置发布Catalog组件 if ('true' == ext.includeLibs) { @@ -249,7 +252,7 @@ class IHubSettingsPlugin implements Plugin { } // 配置iHubLibs版本 - configIHubCatalogs it + configIHubCatalogs it, settings // 自动配置./gradle/libs目录下的.versions.toml文件 autoConfigCatalogsFile it, settings @@ -280,13 +283,15 @@ class IHubSettingsPlugin implements Plugin { } } - private static void configIHubCatalogs(DependencyResolutionManagement management) { + private static void configIHubCatalogs(DependencyResolutionManagement management, Settings settings) { + def iHubPluginVersion = getIHubPluginVersion settings + def iHubLibsVersion = findProperty(settings, IHUB_LIBS_LOCAL_VERSION) ?: IHubLibsVersion management.versionCatalogs { ihub { - from "pub.ihub.lib:ihub-libs:${IHubLibsVersions.getCompatibleLibsVersion('ihub-libs')}" + from "pub.ihub.lib:ihub-libs:${iHubLibsVersion}" IHubSettingsPlugin.IHUB_PLUGIN_IDS.each { pluginId -> plugin(pluginId.contains('ihub-') ? pluginId.split('ihub-').last() : 'root', pluginId) - .version IHubSettingsPlugin.IHUB_PLUGIN_VERSION + .version iHubPluginVersion } IHubSettingsPlugin.PLUGIN_ALIAS_IDS.each { aliasId, id -> plugin(aliasId, id).version getLibsVersion(aliasId) @@ -352,4 +357,9 @@ class IHubSettingsPlugin implements Plugin { ) } + private static String getIHubPluginVersion(Settings settings) { + def localVersion = findProperty settings, IHUB_PLUGINS_LOCAL_VERSION + localVersion && localVersion != 'disabled' ? localVersion : IHubPluginsVersion + } + }