Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法更改配置';的依赖项:编译';解决之后_Java_Gradle - Fatal编程技术网

Java 无法更改配置';的依赖项:编译';解决之后

Java 无法更改配置';的依赖项:编译';解决之后,java,gradle,Java,Gradle,我有一个使用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 { compi

我有一个使用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'
}
问题是,当我想将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.

如何解决这个问题?

首先,您必须添加一个
存储库
块来指定从何处检索依赖项(通常在
依赖项{…}
之前)

repositories {
  mavenCentral()
}

然后,如果您将
依赖项
块放在
jar
块之前,它似乎可以工作,尽管我不确定它为什么不能以另一种方式工作(可能
jar{…}
使用
编译
配置并“锁定”它).

如果您在
gradle.properties

中使用它,请尝试将
org.gradle.configureondemand
设置为
false
,以下是我解决此问题的方法。将此添加到您的框架gradle中。干杯

apply from: 'https://raw.githubusercontent.com/sky-uk/gradle-maven-plugin/master/gradle-mavenizer.gradle'
参考号-

这一点让我很头疼-我不得不将我的jar/fat jar块移到dependencies/repositories块下-下面是我的gradle kotlin,它起了作用:

import org.gradle.jvm.tasks.Jar

plugins {
    java
    kotlin("jvm")
    kotlin("kapt")


}

group = "com.secbot"
version = "1.0-SNAPSHOT"



tasks {
    "build" {
        dependsOn(fatJar)
    }
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>() {
    sourceCompatibility = "11"
    targetCompatibility = "11"
    kotlinOptions.jvmTarget = "11"
}

repositories {
    mavenCentral()
    mavenLocal()
    jcenter()
    google()
    maven(url= "https://oss.sonatype.org/content/groups/public")
    maven(url ="https://jitpack.io")
}


dependencies {
   
    implementation("com.google.code.gson:gson:2.8.6")
  

    implementation(kotlin("stdlib"))
}
val jar by tasks.getting(Jar::class) {
    manifest {
        attributes["Main-Class"] = "com.foo.Application"
    }
}
val fatJar = task("fatJar", type = Jar::class) {
    baseName = "${project.name}-fat"
    manifest {
        attributes["Implementation-Title"] = "Foo Bar!"
        attributes["Implementation-Version"] = version
        attributes["Main-Class"] = "com.foo.Application"
    }
    from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
    with(tasks.jar.get() as CopySpec)
}
import org.gradle.jvm.tasks.Jar
插件{
JAVA
kotlin(“jvm”)
科特林(“卡普特”)
}
group=“com.secbot”
version=“1.0-SNAPSHOT”
任务{
“构建”{
德彭森(法特贾尔)
}
}
tasks.withType(){
sourceCompatibility=“11”
targetCompatibility=“11”
kotlinOptions.jvmTarget=“11”
}
存储库{
mavenCentral()
mavenLocal()
jcenter()
谷歌()
maven(url=”https://oss.sonatype.org/content/groups/public")
maven(url=”https://jitpack.io")
}
依赖关系{
实现(“com.google.code.gson:gson:2.8.6”)
实施(kotlin(“stdlib”))
}
val jar by tasks.get(jar::class){
显示{
属性[“主类”]=“com.foo.Application”
}
}
val fatJar=task(“fatJar”,type=Jar::class){
baseName=“${project.name}-fat”
显示{
属性[“实现标题”]=“Foo-Bar!”
属性[“实现版本”]=版本
属性[“主类”]=“com.foo.Application”
}
from(configurations.runtimeClasspath.get().map{if(it.isDirectory)it else zipTree(it)})
使用(tasks.jar.get()作为CopySpec)
}

哇-真不敢相信这也发生在我身上

Cannot change dependencies of dependency configuration ':implementation' after it has been included in dependency resolution.
我可以通过以下代码解决此问题:

kotlin {
    sourceSets {
        val main by getting {
            dependencies {
                implementation("ca.blah:blah:1.0.0")
            }
        }
    }
}

位于
依赖项{}之前block
而不是build.gradle.kts文件的末尾。

一个附加组件:该问题似乎是由于使用了
配置造成的。在
jar
block中编译
我遇到了同样的问题,在依赖项块+1中添加存储库后,该问题没有得到解决。第二点:“如果将依赖项块放在jar块之前,它似乎可以工作"。这正是让我的项目编译一个依赖于项目中另一个模块的胖jar所需要的。
configurations.compile.collect
解析配置。之后,配置是只读的。但是下面的
dependentials
块试图修改它。因此,错误。根据作为响应,它似乎在AGP 4.1.0-rc01中固定。