添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

gradle java

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?

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