java - Gradle:解决后无法更改配置 ':compile' 的依赖关系

标签 java gradle gradle-plugin

我使用 Netbeans 8.0.2 和 Gradle 支持插件 1.3.8。

我添加了一个任务来生成 uber-Jar,同时排除了一些签名文件,但是当我运行该任务时,它在第 38 行(compile group 行)显示错误,如下所示:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'br.com.myproject.Sample'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
version='1.0.0'

// For DEBUG to work
ext.mainClass = mainClassName

task uniqueJar(type: Jar) {
    baseName = project.name

    from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }

    manifest {
        attributes 'Implementation-Title': project.name,  
                'Implementation-Version': version,
                'Main-Class': mainClassName
    }
    with jar
}

repositories {
    mavenCentral()
    // You may define additional repositories, or even remove "mavenCentral()".
    // Read more about repositories here:
    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}

dependencies {
    // https://mvnrepository.com/artifact/org.eclipse.paho/org.eclipse.paho.client.mqttv3
    compile group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: '1.1.1' // line 38


    testCompile group: 'junit', name: 'junit', version: '4.10'
}

错误消息:

Executing: gradle unique Jar Arguments: [uniqueJar, -c, D:\NetBeansProjects\testeMqtt\settings.gradle]

FAILURE: Build failed with an exception.

  • Where: Build file 'D:\NetBeansProjects\testeMqtt\build.gradle' line: 38

  • What went wrong: A problem occurred evaluating root project 'testeMqtt'.

    Cannot change dependencies of configuration ':compile' after it has been resolved.

  • 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。

构建失败

总时间:0.102秒

如何修复 uber-Jar 任务?

最佳答案

使用 Shadow gradle plugin 解决了我的问题用于生成 uber-Jars:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'br.com.myproject.Sample'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
version='1.0.0'

// For DEBUG to work
ext.mainClass = mainClassName

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
    }
}

apply plugin: 'com.github.johnrengelman.shadow'

关于java - Gradle:解决后无法更改配置 ':compile' 的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43258522/

相关文章:

java 捕获网络摄像头图像

android - 无法为 com.android.build.gradle.internal.api.LibraryVariantOutputImpl 类型的对象设置只读属性 'outputFile' 的值

android - Jenkins 无法解析 com.android.tools.build :gradle:3. 0.0-alpha8

java - GAE 应用程序未在 Firefox 中显示

java - 负载平衡 Web 应用程序

java - 无法运行gradle run命令

Java 19 预览版、Gradle 孵化器功能

java - Gradle:如何在 Gradle 插件中导出项目目录路径?

java - 调用数组并返回不同数组的方法会给出无法解析 B 到变量错误

java - 如何向用户显示我的 Java Swing 应用程序的版本号?