Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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

* 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 {
  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).

An add-on: the problem seems to be specifically caused by the use of configurations.compile in the jar block – rrobby86 Mar 2, 2017 at 10:32 I am having same problem stackoverflow.com/questions/53300594/… its not resolved after add repositories in the dependencies block – M.A.Murali Nov 14, 2018 at 12:55 +1 for your second point: "if you put the dependencies block before the jar block it seems to work". That is exactly what I needed to get my project to compile a fat jar with a dependency on another module within the project. – Jonathan E. Landrum Sep 5, 2019 at 13:35 configurations.compile.collect resolves the configuration. After that, the configuration is read-only. But the following dependencies block tries to modify it. Thus the error. – Eduard Wirch Jan 2, 2020 at 12:35

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:

import org.gradle.jvm.tasks.Jar
plugins {
    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.

Here is what resolved that issue for me. Add this to your framework gradle. Cheers!

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.