Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

gradle

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?

like image 693
reza Avatar asked Mar 02 '17 10:03

reza


1 Answers

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).

like image 122
rrobby86 Avatar answered Sep 28 '22 01:09

rrobby86