java 解决后无法更改配置“:compile”的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42552511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:43:19  来源:igfitidea点击:

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

javagradle

提问by reza

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

我有一个使用 json.jar 库的简单 java 项目。gradle.build 文件内容为:

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

问题是当我想将 json 添加到我的类路径并使用它时,会发生此错误

* 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?

我该如何解决这个问题?

回答by rrobby86

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

首先,您必须添加一个repositories块来指定从何处检索依赖项(通常在dependencies {...}.

repositories {
  mavenCentral()
}

Then, if you put the dependenciesblock before the jarblock it seems to work, although I'm not sure about why it doesn't work the other way (maybe jar {...}uses the compileconfiguration and "locks" it).

然后,如果您将dependencies块放在块之前,jar它似乎可以工作,尽管我不确定为什么它不能以其他方式工作(也许jar {...}使用compile配置并“锁定”它)。