reza
reza

Reputation: 1776

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

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?

Upvotes: 22

Views: 38748

Answers (5)

Mark Lapasa
Mark Lapasa

Reputation: 1654

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.

Upvotes: 1

bsautner
bsautner

Reputation: 4802

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

Upvotes: 2

portfoliobuilder
portfoliobuilder

Reputation: 7856

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'

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

Upvotes: 0

nuhkoca
nuhkoca

Reputation: 1943

Try to set org.gradle.configureondemand to false if you use it in gradle.properties

Upvotes: 10

rrobby86
rrobby86

Reputation: 1454

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

Upvotes: 22

Related Questions