Execution failed for task ':gradle-demo:publishMavenJavaPublicationToMavenRepository'.
> Failed to publish publication 'mavenJava' to repository 'maven'
> Artifact gradle-demo-1.0.jar wasn't produced by this build.
C:\Users\herion>gradle -v
------------------------------------------------------------
Gradle 6.6.1
------------------------------------------------------------
Build time: 2020-08-25 16:29:12 UTC
Revision: f2d1fb54a951d8b11d25748e4711bec8d128d7e3
Kotlin: 1.3.72
Groovy: 2.5.12
Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM: 1.8.0_211 (Oracle Corporation 25.211-b12)
OS: Windows 10 10.0 amd64
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
repositories {
maven {
// change URLs to point to your repos, e.g. http://my.org/repo
def releasesRepoUrl = "http://ops.xxx.com/nexus/repository/maven-releases"
def snapshotsRepoUrl = "http://ops.xxx.com/nexus/repository/maven-snapshots"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username nexusUser
password nexusPassword
}
}
}
}
Artifact my-application-0.0.1-SNAPSHOT.jar wasn't produced by this build.
这是由于以下事实造成jar的:Spring Boot应用程序禁用了主要任务,并且组件希望该任务存在。由于默认情况下该bootJar任务使用与主jar任务相同的文件,因此Gradle的早期版本将:
详情请参考官方说明
官方给我们提供了一些解决方案
configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(bootJar)
}
}
publishing {
publications {
mavenJava(MavenPublication) {
artifact bootJar
artifact sourceJar {
classifier "sources"
}
}
}
}
project.tasks.publish.dependsOn bootJar
jar {
enabled = true
}
bootJar {
classifier = 'application'
}
注意:不推荐使用这种方式,因为该classifier属性在最新版本中已被弃用,并且bootJar使用自定义配置更改任务也会导致不正确的超级jar构造,并且如果解压缩生成的jar分发版,则可能会找到缺少的BOOT-INF目录并必要的 META-INF/MANIFEST.MF值。