Answers:
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
}
如果您使用的是Android:
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
从这里
如果您希望将源添加到已编译的类JAR文件中(您也说可以接受),则可以轻松完成此操作。只需将以下内容添加到您的构建文件中即可。您可以看到,从理论上讲,这就像将源放入单独的JAR的解决方案一样:
jar {
from sourceSets.main.allSource
}
不同之处在于,您是通过说“ jar”代替“ sourcesJar”将其添加到主JAR文件中的。
这应该工作
assemble.dependsOn 'propertyJar'
task propertyJar(type: Jar) {
archiveName = "myJarName.jar"
from('src/main/resources') {
include '**'
}}
assemble.dependsOn
,这会使此任务自动运行。
Kotlin DSL等效项为:
tasks {
val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(java.sourceSets["main"].allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(java.docsDir)
}
artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}
}
Project."java"
解决JavaPluginExtension
不提供任何内容sourceSets
或docsDir
属性的问题。
from(sourceSets["main"].allJava)
from(javadoc)
和from(sourceSets["main"].allSource)
使用时:
java
和maven-publish
插件task sourceJar(type: Jar) {
from sourceSets.main.allJava
archiveClassifier = "sources"
}
publishing {
publications {
maven(MavenPublication) {
from components.java
artifact sourceJar
}
}
}
https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html