Java gradle 5中的sourcesJar任务
我继承了一个代码库,我怀疑它最初是用Gradle4构建的(但我不确定)。我使用的是Gradle 5.5.1,当我运行Java gradle 5中的sourcesJar任务,java,gradle,groovy,Java,Gradle,Groovy,我继承了一个代码库,我怀疑它最初是用Gradle4构建的(但我不确定)。我使用的是Gradle 5.5.1,当我运行Gradle时,我会发现与发布到Maven repo相关的错误: * What went wrong: A problem occurred evaluating root project 'common'. > Could not find method sourcesJar() for arguments [build_d1u03z05r8d12r3e8b5qq1fxm$
Gradle
时,我会发现与发布到Maven repo相关的错误:
* What went wrong:
A problem occurred evaluating root project 'common'.
> Could not find method sourcesJar() for arguments [build_d1u03z05r8d12r3e8b5qq1fxm$_run_closure3$_closure13$_closure15$_closure16@190bc2b8] on object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication.
看起来像是一个类似的问题,但它是一个不同的错误,他们的解决方案无论如何都不起作用
my build.gradle的相关部分包括:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar {
classifier "sources"
}
artifact testJar {
classifier "tests"
}
}
}
repositories {
maven {
url 'http://repo.url'
credentials {
username "$username"
password "$password"
}
}
}
}
task sourcesJar(type: Jar) {
from sourceSets.main.allSource
classifier = 'sources'
}
task testJar(type: Jar) {
from sourceSets.test.output
classifier = 'tests'
}
在该区块中:
artifact sourcesJar {
classifier "sources"
}
artifact testJar {
classifier "tests"
}
拆下封口…只需使其看起来像这样:
artifact sourcesJar
artifact testJar
好的,我想我已经弄明白了:说一个
发布
块是在gradle4中项目的其余部分之后执行的,但不是在gradle5中
所以,改变
artifact sourcesJar {
classifier "sources"
}
artifact testJar {
classifier "tests"
}
到
让我更进一步。通过该更改,我得到了以下错误:
* What went wrong:
A problem occurred configuring root project 'common'.
> Cannot create a Publication named 'sourcesJar' because this container does not support creating elements by name alone. Please specify which subtype of Publication to create. Known subtypes are: MavenPublication
而且似乎暗示了project的前缀。你应该把它修好
因此,将其更改为:
afterEvaluate {
artifact project.sourcesJar {
classifier "sources"
}
artifact project.testJar {
classifier "tests"
}
}
看起来不错,不过我对这个项目有点怀疑。前缀。在我的例子中,我必须添加以下内容,Gradle知道这些工件:
java {
withJavadocJar()
withSourcesJar()
}
然后我可以这样使用它:
publishing.publications {
mavenJava(MavenPublication) {
from components.java
}
}
Javadoc以及源代码已经发布。似乎不需要添加
afterEvaluate
块。我现在收到一条稍有不同的错误消息:无法为org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication类型的对象获取未知属性“sourcesJar”。
publishing.publications {
mavenJava(MavenPublication) {
from components.java
}
}