Skip to content

Commit

Permalink
chore: 增加本地调试参数 (#968)
Browse files Browse the repository at this point in the history
* chore: 增加本地调试参数

* chore: 增加本地调试参数
  • Loading branch information
henry-hub authored Nov 3, 2024
1 parent 8e78d78 commit 7ec26f2
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 57 deletions.
11 changes: 11 additions & 0 deletions ihub-base/src/main/groovy/pub/ihub/plugin/IHubLibsVersions.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> LIBS_VERSIONS =
readLines(IHubLibsVersions.classLoader.getResource('META-INF/ihub/libs-versions'))
.collectEntries { it.split '=' }
Expand All @@ -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'
}

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

/**
Expand Down Expand Up @@ -54,8 +55,9 @@ class IHubBomPlugin extends IHubProjectPluginAware<IHubBomExtension> {

// 项目不包含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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +37,7 @@ import static pub.ihub.plugin.IHubProjectPluginAware.EvaluateStage.AFTER
@IHubPlugin(value = IHubJavaagentExtension, beforeApplyPlugins = IHubBomPlugin)
class IHubJavaagentPlugin extends IHubProjectPluginAware<IHubJavaagentExtension> {

@CompileStatic
@Override
protected void apply() {
if (hasPlugin(ApplicationPlugin)) {
Expand All @@ -44,23 +46,27 @@ class IHubJavaagentPlugin extends IHubProjectPluginAware<IHubJavaagentExtension>
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<Plugin<Project>>
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 }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ interface IHubPluginsExtension extends IHubExtensionAware {
@IHubProperty(type = [PROJECT, SYSTEM, ENV], defaultValue = 'false', genericType = Boolean)
Property<Boolean> getMavenAliYunEnabled()

/**
* 是否启用SpringMilestone仓库
*/
@IHubProperty(type = [PROJECT, SYSTEM, ENV], defaultValue = 'false', genericType = Boolean)
Property<Boolean> getMavenSpringMilestoneEnabled()

/**
* 是否启用私有仓库(组件发布仓库)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class IHubPluginsPlugin extends IHubProjectPluginAware<IHubPluginsExtension> {
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() } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -51,7 +55,6 @@ class IHubSettingsPlugin implements Plugin<Settings> {

private static final List<String> 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<String, String> PLUGIN_ALIAS_IDS = [
'plugin-publish': 'com.gradle.plugin-publish',
Expand All @@ -74,7 +77,7 @@ class IHubSettingsPlugin implements Plugin<Settings> {
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
}
Expand Down Expand Up @@ -110,47 +113,49 @@ class IHubSettingsPlugin implements Plugin<Settings> {
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
Expand Down Expand Up @@ -234,9 +239,7 @@ class IHubSettingsPlugin implements Plugin<Settings> {

private static void configVersionCatalogs(Settings settings, IHubSettingsExtension ext) {
settings.dependencyResolutionManagement {
repositories {
mavenCentral()
}
configRepositories it, settings

// 配置发布Catalog组件
if ('true' == ext.includeLibs) {
Expand All @@ -249,7 +252,7 @@ class IHubSettingsPlugin implements Plugin<Settings> {
}

// 配置iHubLibs版本
configIHubCatalogs it
configIHubCatalogs it, settings

// 自动配置./gradle/libs目录下的.versions.toml文件
autoConfigCatalogsFile it, settings
Expand Down Expand Up @@ -280,13 +283,15 @@ class IHubSettingsPlugin implements Plugin<Settings> {
}
}

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)
Expand Down Expand Up @@ -352,4 +357,9 @@ class IHubSettingsPlugin implements Plugin<Settings> {
)
}

private static String getIHubPluginVersion(Settings settings) {
def localVersion = findProperty settings, IHUB_PLUGINS_LOCAL_VERSION
localVersion && localVersion != 'disabled' ? localVersion : IHubPluginsVersion
}

}

0 comments on commit 7ec26f2

Please sign in to comment.