artifact 发布
获取发布仓库
提示
仓库定义请参考 仓库声明。
/**
* 获取发布仓库
*
* @param version 版本号
* @return 仓库 {@link MavenArtifactRepository}
*/
@SuppressWarnings("GrMethodMayBeStatic")
def getPublishRepositories(version) {
if (version.toUpperCase().endsWith("-SNAPSHOT")) {
return pushRepositories.snapshot
}
return pushRepositories.release
}
注意
注意版本号需使用发布模块的版本号。
发布通用配置
配置仓库
publishing {
repositories {
def publishRepositories = getPublishRepositories(project.version)
publishRepositories.each { repository -> maven repository }
}
}
不生成 module meta
tasks.withType(GenerateModuleMetadata) {
enabled = false
}
发布 bom
plugins {
id "java-platform"
id "maven-publish"
}
javaPlatform {
allowDependencies()
}
publishing {
repositories {
def publishRepositories = getPublishRepositories(project.version)
publishRepositories.each { repository -> maven repository }
}
publications {
mavenBom(MavenPublication) {
from components.javaPlatform
pom {
packaging = "pom"
name = "TTS Dependencies"
description = "Maven bom of TTS"
url = "http://www.tts.org/"
properties = [
foo: "bar"
]
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "tts"
name = "Michael Tao"
email = "tts@tts.org"
}
}
scm {
connection = "scm:git:git://github.com/tts/tts-dependencies.git"
}
}
}
}
}
dependencies {
/* declare import bom */
api(
platform("org.springframework.boot:spring-boot-dependencies:2.6.2"),
platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.0")
)
/* declare transitive dependencies */
constraints {
api "org.tts.coding:tts-utils:1.0.0-SNAPSHOT"
}
}
提示
java-platform 声明依赖仅支持 api / runtime 两种 configuration。
声明 jar 依赖需使用 constraints。
发布 jar
plugins {
id "java-library"
id "maven-publish"
}
java {
withJavadocJar()
withSourcesJar()
}
configurations {
implementation.extendsFrom(provided)
}
publishing {
publications {
mavenJar(MavenPublication) {
from components.java
pom {
packaging = "jar"
url = "http://www.tts.org/"
properties = [
foo: "bar"
]
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "tts"
name = "Michael Tao"
email = "tts@tts.org"
}
}
scm {
connection = "scm:git:git://github.com/tts/tts-libraries.git"
}
withXml {
//noinspection GroovyImplicitNullArgumentCall
def pomDependencies = asNode().dependencies.dependency
/* handle provided scope dependencies */
def allDependencies = project.configurations.provided.allDependencies
pomDependencies.findAll {
dependency -> allDependencies.find { dep -> dep.name == dependency.artifactId.text() }
}.findAll { dependency -> dependency.scope.text() == "runtime" }.each {
dependency -> dependency.scope.each { scope -> scope.value = "provided" }
}
/* handle compile scope dependencies */
pomDependencies.findAll { it.scope.text() == "compile" }.each { dependency ->
dependency.scope.each { scope -> scope.replaceNode({}) }
}
}
}
}
}
}
注意
打 jar 时,注意生成 pom 的配置调整。 pom 的通用配置可在 parent 中配置,在 children 中增加或覆盖。数组属性不会覆盖,需注意出现重复属性。