我最近切换到gradle 7.0,现在无法构建我的项目jar,出现错误
无法获取org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer类型的配置容器的未知属性"runtime"。`
这是我的build.gradle:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
implementation group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet-core', version: '2.7'
implementation group: 'org.eclipse.jetty.aggregate', name: 'jetty-all', version: '9.3.0.M1'
//
implementation 'javax.xml.bind:jaxb-api:2.3.0'
// testImplementation group: 'junit', name: 'junit', version: '4.11'
implementation group: 'org.json', name: 'json', version: '20200518'
implementation group: 'com.jolbox', name: 'bonecp', version: '0.8.0.RELEASE'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'
implementation group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-json-provider', version: '2.12.0'
implementation "org.slf4j:slf4j-simple:1.7.9"
}
version = '1.0'
jar {
manifest {
attributes(
'Main-Class': 'classes.RestServer',
)
}
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/.SF"
exclude "META-INF/.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
artifacts {
archives fatJar
}
Gradle在Gradle6.x之后删除了运行时配置。
您可以在build.gradle
中更改fatJar
任务以引用runtimeConfiguration
(根据Java插件文档(:
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
// change here: runtimeClasspath instead of runtime
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/.SF"
exclude "META-INF/.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
或者使用一个插件,为你处理胖罐子构建。几年前我试过Shadow。这还应该处理来自不同jar(如META-INF/LICENSE
(的具有相同名称的文件,这些文件可能会在您的方法中遇到。
对runtime
配置的支持已停止gradle-7.0
,因此configurations.runtime
无法工作。
任务copyAllDependencies
在gradle-7.0
中对我有效。
以下是将依赖项复制到${buildDir}/output/libs
:中的示例
在build.gradle
中添加以下内容。
task copyAllDependencies(type: Copy) {
from configurations.compileClasspath
into "${buildDir}/output/libs"
}
build.dependsOn(copyAllDependencies)
我希望它能有所帮助。