这里是 Java 11 和 Gradle 7.2。我正在尝试构建一个可重用的库,我可以(最终)将其发布到 Maven 存储库并拉入其他项目。但首先我只想将其发布到我本地的 m2 存储库。
项目目录结构如下:
mylib/
lib/
src/
build.gradle
哪里build.gradle
是:
plugins {
id 'java-library'
id 'maven-publish'
id 'io.freefair.lombok' version "6.5.0-rc1"
}
sourceCompatibility = 1.11
targetCompatibility = 1.11
archivesBaseName = 'mylib'
version = '1.0.0-RC1'
group = 'org.example'
repositories {
mavenCentral()
}
dependencies {
// omitted for brevity
)
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
}
publishing {
publications {
maven(MavenPublication) {
artifact("build/libs/${archivesBaseName}-${version}.jar") {
extension 'jar'
}
}
}
}
tasks.named('test') {
useJUnitPlatform()
}
publishToMavenLocal.configure {
mustRunAfter build
}
当我运行gradle publishToMavenLocal
时我得到:
% ./gradlew clean build publishToMavenLocal
> Task :lib:publishMavenPublicationToMavenLocal FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':lib:publishMavenPublicationToMavenLocal'.
> Failed to publish publication 'maven' to repository 'mavenLocal'
> Invalid publication 'maven': artifact file does not exist: '/Users/myuser/workspace/mylib/lib/build/libs/mylib-1.0.0-RC1.jar'
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 833ms
6 actionable tasks: 6 executed
所以看起来即使我指定(在命令行上)运行 clean
然后 build
然后 publishToMavenLocal
,即使我什至指定(在 build.gradle
中) publishToMavenLocal
必须在 build
之后运行,看来发生的事情是:
-
publishToMavenLocal
坚持先运行(在clean
或build
之前) - 由于 JAR 尚未构建,因此指定位置 (
"build/libs/${archivesBaseName}-${version}.jar"
) 不存在 JAR 文件 - 构建失败,因为 Artifact 不存在
所以我认为我只需要得到 build
在publishToMavenLocal
之前运行但我没主意了。
最佳答案
您正在混合 plugins DSL (插件 { }
) 与 legacy plugin application (应用插件
)。这不是什么大问题,但你应该选择 @sean 的 answer并使用插件 DSL 来解决您的问题。
针对您手头的问题
Could not get unknown property 'plugin'
发生这种情况是因为您错过了应用插件
中的:
apply plugin: 'maven-publish'
关于maven - Gradle:无效发布 'maven': Artifact 文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72625362/