【问题标题】:Cannot change dependencies of configuration ':compile' after it has been resolved解析后无法更改配置“:编译”的依赖关系
【发布时间】:2021-06-18 02:59:08
【问题描述】:

我有一个使用 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.

我该如何解决这个问题?

【问题讨论】:

    标签: java gradle


    【解决方案1】:

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

    repositories {
      mavenCentral()
    }
    

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

    【讨论】:

    • 附加组件:问题似乎是由configurations.compilejar 块中的使用引起的
    • 我遇到了同样的问题stackoverflow.com/questions/53300594/… 在依赖块中添加存储库后没有解决
    • +1 表示您的第二点:“如果将依赖项块放在 jar 块之前,它似乎可以工作”。这正是我需要让我的项目编译一个依赖项目中另一个模块的胖 jar。
    • configurations.compile.collect 解析配置。之后,配置为只读。但是下面的dependencies 块试图修改它。因此错误。
    【解决方案2】:

    如果你在gradle.properties中使用,请尝试将org.gradle.configureondemand设置为false

    【讨论】:

    • 根据对应的here,好像在AGP 4.1.0-rc01中修复了。
    【解决方案3】:

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

    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 文件的末尾。

    【讨论】:

      【解决方案4】:

      这就是为我解决这个问题的方法。将此添加到您的框架 gradle。干杯!

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

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

      【讨论】:

        【解决方案5】:

        这让我有点吃惊 - 我不得不将我的 jar / fat jar 块移动到依赖项 / 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)
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-14
          • 1970-01-01
          • 2017-04-03
          • 1970-01-01
          • 2017-04-10
          • 2018-10-13
          • 2021-01-28
          • 2019-06-16
          • 2016-02-25
          相关资源
          最近更新 更多