是否可以将gradle发布任务定义为public?



我有一个Gradle7.0发布任务定义在我的Java Spring启动项目子模块build.gradle文件中,像这样:

publishing {
publications {
maven(MavenPublication) {
groupId 'com.sportswin.soa'
artifactId 'soa-auth-api'
version('1.0.0-SNAPSHOT')
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
repositories {
maven {
url = version.endsWith('SNAPSHOT') ? "${dabaiSnapshotRepo}" : "${dabaiReleaseRepo}"
url = "$url"
credentials {
username "$mavenLoginName"
password "$mavenPassword"
}
}
}
}

它在我的每个子模块中都工作得很好。唯一的问题是,这个代码片段必须复制到我的每个子模块。现在子模块增加了50多个+。我必须把它复制粘贴到每个位置。

是否可以将发布任务定义为公共任务,我只需要传递一些参数,如groupId,artifactId,version?我应该怎么做才能使它像这样工作,这样我就不必复制和粘贴重复的代码片段了?

我的项目结构如下:

rootProject
-- build.gradle
-- setting.gradle
module1
-- build.gradle
module2
-- build.gradle

BTW,这是完整的子模块build.gradle:

project(":soa-auth") {
dependencies {
}
}
project(":soa-auth:soa-auth-api") {
jar {
enabled = true
}
bootJar {
enabled = false
}
dependencies {
api project(":soa-misc-biz")
}
publishing {
publications {
maven(MavenPublication) {
groupId 'com.sportswin.soa'
artifactId 'soa-auth-api'
version('1.0.0-SNAPSHOT')
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
repositories {
maven {
url = "${dabaiSnapshotRepo}"
url = "$url"
credentials {
username "$mavenLoginName"
password "$mavenPassword"
}
}
}
}
}
project(":soa-auth:soa-auth-service") {
archivesBaseName = "soa-auth-service"
version = "1.0.0-SNAPSHOT"
bootJar {
manifest {
attributes 'Start-Class': 'com.sportswin.soa.auth.AppStarter'
}
}
dependencies {
implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
implementation project(":soa-auth:soa-auth-api")
}
}

我很乐意提供任何额外的信息来解决这个问题。

解决方案一:buildSrc

buildSrc是一个特殊的文件夹,被Gradle隐式地视为包含的构建。您可以在这里放置一些常见的构建逻辑。

结构
├── buildSrc
│   ├── src/main/groovy/com.example.my-publishing.gradle
│   └── build.gradle
├── module1
│   └── build.gradle
├── module2
│   └── build.gradle
├── build.gradle
└── settings.gradle

./buildSrc/build.gradle

plugins {
id 'groovy-gradle-plugin'
}
repositories {
gradlePluginPortal()
}

./buildSrc/src/main/com.example.my-publishing.gradle

plugins {
id 'java'
id 'maven-publish'
}
java {
withSourcesJar()
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
repositories {
...
}
}

./module1/build.gradle

plugins {
...
id 'com.example.my-publishing'
}

方案二:显式buildSrc

这个解决方案几乎与第一个相同。我更喜欢这样,因为我相信显式比隐式好。

结构
├── build-conventions  <-- Use any name other than 'buildSrc'
│   ├── src/main/groovy/com.example.my-publishing.gradle
│   ├── build.gradle
│   └── settings.gradle  <-- Leave it empty
├── module1
│   └── build.gradle
├── module2
│   └── build.gradle
├── build.gradle
└── settings.gradle

./settings.gradle

...
include('module1')
include('module2')
includeBuild('build-conventions')

方案3:独立插件

如果有一天您的发布逻辑变得非常复杂,您可能需要这个。我选择在Kotlin中实现静态类型。你也可以用Groovy写。

结构
├── plugins
│   ├── src/main/kotlin/com/example/MyPublishingPlugin.kt
│   ├── build.gradle.kts
│   └── settings.gradle.kts  <-- Leave it empty
├── module1
│   └── build.gradle
├── module2
│   └── build.gradle
├── build.gradle
└── settings.gradle

./settings.gradle

...
include('module1')
include('module2')
includeBuild('plugins')

./plugins/build.gradle.kts

plugins {
`java-gradle-plugin`
id("org.jetbrains.kotlin.jvm") version "1.5.0"
}
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
gradlePlugin {
val myPublishing by plugins.creating {
id = "com.example.my-publishing"
implementationClass = "com.example.MyPublishingPlugin"
}
}

./plugins/src/main/kotlin/com/example/MyPublishingPlugin.kt

package com.example
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
class MyPublishingPlugin: Plugin<Project> {
override fun apply(project: Project) {
project.plugins.apply(MavenPublishPlugin::class.java)
project.afterEvaluate {
project.configureJavaExtension()
project.configurePublishingExtension()
}
}
private fun Project.configureJavaExtension() {
val extension = this.extensions.getByType(JavaPluginExtension::class.java)
extension.withSourcesJar()
}
private fun Project.configurePublishingExtension() {
val extension = this.extensions.getByType(PublishingExtension::class.java)
extension.publications { container ->
container.create("maven", MavenPublication::class.java) {
it.from(this.components.getByName("java"))
}
}
extension.repositories {
it.maven { repo ->
repo.url = this.uri(this.layout.buildDirectory.dir("repo"))
// ------------ YOUR IMPLEMENTATION ------------
}
}
}
}

./module1/build.gradle

plugins {
...
id 'com.example.my-publishing'
}

你可以通过命令gradle init生成一个完整的Gradle插件项目。如果你需要传递额外的参数给一个插件,你可以给它附加一个自定义扩展。

相关内容

  • 没有找到相关文章

最新更新