解析后无法更改配置':compile'的依赖关系

时间:2017-03-02 10:12:47

标签: 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 {
  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.

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:10)

首先,您必须添加repositories块以指定从中检索依赖项的位置(通常在dependencies {...}之前。

repositories {
  mavenCentral()
}

然后,如果你把dependencies块放在jar块之前它似乎有效,虽然我不确定为什么它不起作用(也许{{1} }使用jar {...}配置并“锁定”它。)

答案 1 :(得分:3)

如果在org.gradle.configureondemand中使用false,请尝试将gradle.properties设置为print({item:getattr(my_module, item) for item in dir(my_module) if not item.startswith("__") and not item.endswith("__")})

答案 2 :(得分:0)

这是为我解决该问题的原因。将此添加到您的框架gradle。干杯!

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

参考- https://github.com/sky-uk/gradle-maven-plugin

答案 3 :(得分:0)

这有点我 - 我不得不将我的 jar / fat jar 块移动到依赖项 / 存储库块下 - 这是我的 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)
}

答案 4 :(得分:0)

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

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")
            }
        }
    }
}

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