简体   繁体   中英

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

I'm using Netbeans 8.0.2 with Gradle Support plugin 1.3.8.

I've added a task to generate a uber-Jar while excluding a few signature files, however when I run the task it displays an error at line 38 (the compile group line) as follows:

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'
}

Error message:

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.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 0.102 secs

How can I fix the uber-Jar task?

Solved my issue using the Shadow gradle plugin for generating 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM