[英]Build source jar with Gradle Kotlin DSL?
This question asks how to build a SourceJar with Gradle. 这个问题询问如何使用 Gradle 构建 SourceJar。 How can I do the same with the Gradle Kotlin DSL?
我如何使用 Gradle Kotlin DSL 做同样的事情?
The gradle code would be: 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
}
With Gradle 5.3.1, this is a little nicer and avoids deprecated API:在 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)
}
}
Task assemble
will create all the artifacts.任务
assemble
将创建所有工件。
The following will work:以下将起作用:
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)
}
A complete build.gradle.kts
would look like this:一个完整的
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)
}
}
As of Gradle 6.0 this is a much easier and cleaner.从Gradle 6.0 开始,这是一个更容易和更干净的方法。 All you need to do is:
您需要做的就是:
java {
withSourcesJar()
withJavadocJar()
}
Check out the documentation on the java extension , and its functions, withSourcesJar() and withJavadocJar()查看有关java 扩展及其函数的文档, withSourcesJar()和withJavadocJar()
All described methods fail with error on Gradle 6.6 :所有描述的方法在Gradle 6.6上都失败并出现错误:
SourceSet with name 'main' not found
I've found a solution that works:我找到了一个有效的解决方案:
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(android.sourceSets.getByName("main").java.srcDirs)
}
artifacts {
archives(sourcesJar)
}
}
With Gradle 5.1 you can do something like this使用 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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.