频道
bg

Gradle Platform

coding一月 22, 20221mins
Gradle Java

定义BOMH2

Platform可以定义生成BOM文件

启用插件

java

plugins {
id 'java-platform'
}

定义约束,可以包括外部的依赖和项目的内部依赖

java

dependencies {
constraints {
api project(":hithinkway-usermgmt")
api project(":hithinkway-data-common")
api "org.mapstruct:mapstruct:${mapstruct_version}"
}
}

参考:https://docs.gradle.org/current/userguide/platforms.html

api 以及 implementationH3

implementation配置只会声明当前的依赖,不会传递

如果需要传递依赖那么我们的模块相当与是个library,需要启用插件java-library 后使用api声明依赖

共享构建逻辑H2

公共的构建配置可以通过buildSrc来配置

参考:https://docs.gradle.org/current/samples/sample_convention_plugins.html

定义Precompiled script pluginH3

启用插件groovy-gradle-plugin

java

buildSrc/build.gradle
plugins {
id 'groovy-gradle-plugin'
}

src/main/groovy 下定义precompiled script plugins,例如myproject.java-conventions.gradle

precompiled script plugins中需要用到的plugin在buildSrc/build.gradle 中定义

java

// buildSrc/build.gradle
repositories {
gradlePluginPortal() // so that external plugins can be resolved in dependencies section
}
dependencies {
implementation 'gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.0.5'
testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'
}

最后在模块用启用plugin

java

plugins {
id 'java-library'
id 'maven-publish'
id 'myproject.java-conventions'
}

发布H2

添加插件id 'maven-publish'

添加配置

jsx

publishing {
repositories {
maven {
credentials(PasswordCredentials)
def releasesRepoUrl = "https://nexus.dev.hithinksoft.com/repository/maven-releases/"
def snapshotsRepoUrl = "https://nexus.dev.hithinksoft.com/repository/maven-snapshots/"
url = project.hasProperty('release') ? releasesRepoUrl : snapshotsRepoUrl
}
}
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}

Repository的Credential可以通过环境变量、gradle.properties 等方式传递

jsx

mySecureRepositoryUsername=secret-user
mySecureRepositoryPassword=secret-password

上面的例子中repository的名字叫maven ,所以应该定义

jsx

mavenUsername=secret-user
mavenPassword=secret-password

评论


新的评论

匹配您的Gravatar头像

Joen Yu

@2022 JoenYu, all rights reserved. Made with love.