Skip to content
Advertisement

Cannot change dependencies of configuration ‘:compile’ after it has been resolved

I have a simple java project that uses json.jar library. gradle.build file content is:

apply plugin: 'java'
jar {
  manifest {
    attributes(
      'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
      'Main-Class': 'main.java.Main'
    )
  }
}
dependencies {
  compile 'org.json:json:20160212'
}

problem is when I want to add json to my classpath and use it, this error happens

* Where:
Build file '/home/tina-admin/Documents/myJavaProjects/LongMan/build.gradle' line: 11

* What went wrong:
A problem occurred evaluating root project 'LongMan'.
> Cannot change dependencies of configuration ':compile' after it has been resolved.

how can I solve this?

Advertisement

Answer

First, you have to add a repositories block to specify where dependencies are retrieved from (usually before dependencies {...}.

repositories {
  mavenCentral()
}

Then, if you put the dependencies block before the jar block it seems to work, although I’m not sure about why it doesn’t work the other way (maybe jar {...} uses the compile configuration and “locks” it).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement