Gradle 由于 'fatJar task' 构建失败,尽管代码在另一个项目中对我有用

Gradle build fails thanks to 'fatJar task' although code worked for me in another project

我尝试用 Gradle 创建一个 fatJar。我在这个 site 上找到了一个很好的例子,它在另一个项目中对我很有效。在我最近的项目中,在 'gradlew build' 任务期间发生错误,即以下错误:

FAILURE: Build failed with an exception.

Where: Build file 'D:\dev\MarkPublished\build.gradle' line: 40

What went wrong: A problem occurred evaluating root project 'markpublished'. Could not find method Attributes() for arguments [{Implementation->Title=Gradle Jar File, Implementation-Version=1.0-Snapshot, Main-Class=path.classname}] on root project 'myproject'.

这是我的(缩短的)'build.gradle'-文件:

plugins {
  id 'java'
  id 'idea'
}

group 'mygroup'
version '1.0-Snapshot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

idea {
    ...
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.4'
}

repositories {
    ...
}

dependencies {
    ...
}

task fatJar(type: Jar) {
    manifest {
        Attributes ('Implementation-Title': 'Gradle Jar File',
                'Implementation-Version': version,
                'Main-Class': 'path.classname')
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with Jar
}

我使用 Win7 和 IntelliJ Idea 14.1.5。

老实说,我完全不知道,如果它在另一个项目中对我不起作用,我也不会在这里问。

尝试将 fatJar 任务设为:

task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File',
                   'Implementation-Version': version,
                   'Main-Class': 'path.classname'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with Jar
}

属性必须小写,如attributes.

创建 spring-boot jar 工件: 1) 在 build.gradle 文件的最开头插入以下代码(第一行):

buildscript {
 repositories {
    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
 }
 dependencies {
   classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.2.RELEASE'
 }
}

2) jar 名称由 settings.gradle 文件确定: rootProject.name = 'commons-ex1111'

要在 inteliJ 中生成 spring-boot 应用程序 jar,请在 GRADLE window 上触发 'bootJar' 任务,或者简单地在 'gradle bootJar' 上写入 'gradle bootJar'命令行。

Jar 将在以下位置创建:\build\libs

整个build.gradle文件:

buildscript {
    repositories {
        maven { url 'https://repo.spring.io/snapshot' }
        maven { url 'https://repo.spring.io/milestone' }
    }
    dependencies {
      classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.2.RELEASE'
    }
}

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
    id "com.github.johnrengelman.shadow" version "1.2.3"
}

apply plugin: 'io.spring.dependency-management'

group = 'ex1.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

}