使用 Gradle Kotlin DSL 构建源 jar?
Build source jar with Gradle Kotlin DSL?
This question 询问如何使用 Gradle 构建 SourceJar。我怎样才能对 Gradle Kotlin DSL 做同样的事情?
gradle 代码为:
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
以下将起作用:
val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(sourceSets["main"].allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(tasks["javadoc"])
}
artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}
一个完整的 build.gradle.kts
看起来像这样:
plugins {
kotlin("jvm") version "1.2.71"
}
repositories {
mavenCentral()
}
dependencies {
}
tasks {
val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(sourceSets["main"].allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(tasks["javadoc"])
}
artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}
}
在 Gradle 5.1 中你可以做这样的事情
tasks {
val sourcesJar by registering(Jar::class) {
classifier = "sources"
from(sourceSets.main.get().allSource)
dependsOn(classes)
}
val javadocJar by registering(Jar::class) {
classifier = "javadoc"
from(javadoc.get().destinationDir)
dependsOn(javadoc)
}
}
在 Gradle 5.3.1 中,这会更好一些并且避免了弃用 API:
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn.add(javadoc)
archiveClassifier.set("javadoc")
from(javadoc)
}
artifacts {
archives(sourcesJar)
archives(javadocJar)
archives(jar)
}
}
任务 assemble
将创建所有工件。
从 Gradle 6.0 开始,这更容易和更清洁。您需要做的就是:
java {
withSourcesJar()
withJavadocJar()
}
查看 java extension, and its functions, withSourcesJar() and withJavadocJar()
上的文档
所有描述的方法都因 Gradle 6.6:
上的错误而失败
SourceSet with name 'main' not found
我找到了一个有效的解决方案:
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(android.sourceSets.getByName("main").java.srcDirs)
}
artifacts {
archives(sourcesJar)
}
}
This question 询问如何使用 Gradle 构建 SourceJar。我怎样才能对 Gradle Kotlin DSL 做同样的事情?
gradle 代码为:
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
以下将起作用:
val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(sourceSets["main"].allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(tasks["javadoc"])
}
artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}
一个完整的 build.gradle.kts
看起来像这样:
plugins {
kotlin("jvm") version "1.2.71"
}
repositories {
mavenCentral()
}
dependencies {
}
tasks {
val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(sourceSets["main"].allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(tasks["javadoc"])
}
artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}
}
在 Gradle 5.1 中你可以做这样的事情
tasks {
val sourcesJar by registering(Jar::class) {
classifier = "sources"
from(sourceSets.main.get().allSource)
dependsOn(classes)
}
val javadocJar by registering(Jar::class) {
classifier = "javadoc"
from(javadoc.get().destinationDir)
dependsOn(javadoc)
}
}
在 Gradle 5.3.1 中,这会更好一些并且避免了弃用 API:
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn.add(javadoc)
archiveClassifier.set("javadoc")
from(javadoc)
}
artifacts {
archives(sourcesJar)
archives(javadocJar)
archives(jar)
}
}
任务 assemble
将创建所有工件。
从 Gradle 6.0 开始,这更容易和更清洁。您需要做的就是:
java {
withSourcesJar()
withJavadocJar()
}
查看 java extension, and its functions, withSourcesJar() and withJavadocJar()
上的文档所有描述的方法都因 Gradle 6.6:
上的错误而失败
SourceSet with name 'main' not found
我找到了一个有效的解决方案:
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(android.sourceSets.getByName("main").java.srcDirs)
}
artifacts {
archives(sourcesJar)
}
}