简体   繁体   English

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

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

I have a simple java project that uses json.jar library.我有一个使用 json.jar 库的简单 java 项目。 gradle.build file content is: 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?我该如何解决这个问题?

First, you have to add a repositories block to specify where dependencies are retrieved from (usually before dependencies {...} .首先,您必须添加一个repositories块来指定从何处检索依赖项(通常在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).然后,如果您将dependencies块放在jar块之前,它似乎可以工作,尽管我不确定为什么它不能以其他方式工作(也许jar {...}使用compile配置并“锁定”它)。

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

Here is what resolved that issue for me.这是为我解决这个问题的方法。 Add this to your framework gradle.将此添加到您的框架gradle。 Cheers!干杯!

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

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

this bit me - i had to move my jar / fat jar block to under the dependencies / repositories blocks - here's my gradle kotlin that worked:这有点我 - 我不得不将我的 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)
}

Wow - Can't believe this happened to me as well哇 - 不敢相信这也发生在我身上

Cannot change dependencies of dependency configuration ':implementation' after it has been included in dependency resolution.

I was able to resolve this by having this code:我能够通过使用以下代码解决此问题:

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

Be located before the dependencies { } block instead of at the end of the build.gradle.kts file.位于dependencies { } block而不是 build.gradle.kts 文件的末尾。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gradle:解决后无法更改配置“:compile”的依赖项 - Gradle: Cannot change dependencies of configuration ':compile' after it has been resolved 信息:配置“编译”已过时,已替换为“实施”和“API” - INFO: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api' 如何解决此问题“配置&#39;compile&#39;已过时,并已由&#39;implementation&#39;和&#39;api&#39;代替。” - How to solve this “Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.” 无法锁定 Java 编译缓存,因为它已被此进程锁定 - Cannot lock Java compile cache as it has already been locked by this process 在调用onWindowFocusChange()之后更改文本 - Change Text after onWindowFocusChange() has been called 修复“无法将配置解析为类型” - Fixing “Configuration cannot be resolved to a type” R.java已经生成,仍然“无法解析为变量” - R.java has been generated, still getting 'cannot be resolved to be a variable' 插入数据后提交响应后无法转发 - After inserting data Cannot forward after response has been committed 像配置“编译”这样的android studio中的同步依赖项错误已过时,已由“实现”和“ api”替换 - Error in syncing dependency in android studio like Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api' 如何编译具有依赖关系的软件包? - How to compile packages that has dependencies?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM