由于'fatJar任务',Gradle构建失败,尽管代码在另一个项目中适用于我

问题描述 投票:4回答:2

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

FAILURE:构建因异常而失败。

其中:构建文件'D:\ dev \ MarkPublished \ build.gradle'行:40

出了什么问题:评估根项目'markpublished'时出现问题。无法在根项目'myproject'上找到参数[{Implementation-> Title = Gradle Jar文件,Implementation-Version = 1.0-Snapshot,Main-Class = path.classname}]的方法Attributes()。

这是我的(缩短的)'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。

老实说,我绝对没有任何线索,我不会在这里询问它是否在我的另一个项目中没有用。

java intellij-idea gradle build.gradle gradlew
2个回答
4
投票

尝试将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


1
投票

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

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'

要在intelliJ中生成spring-boot应用程序jar,请在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'

}
© www.soinside.com 2019 - 2024. All rights reserved.