How to make a Jar file with dependencies by Gradle 7.0+?

11,420

Solution 1

You need to replace compile by runtimeClasspath in your case.

In previous Gradle versions, compile had too many responsibilities:

  • Declare dependencies
  • Resolved as the compile classpath
  • Exposed to consuming projects

It has been replaced by implementation for dependency declaration, it also needs to be replaced by something for resolution.

However in your case, you most likely want to package the runtime dependencies and not the compile ones. Hence you should use runtimeClasspath as the configuration to resolve. And not compileClasspath which would not contain any runtimeOnly dependencies.

Solution 2

For others wanting to upgrade their Gradle configuration to the 7.0+ format, note that simply replacing compile with implementation or api will likely bug out if you use the java plugin. You need to be using the java-library plugin. Documentation.

Make sure that in your gradle.config you replace:

apply plugin: 'java'

with:

apply plugin: 'java-library'

You use implementation for non-transitive dependencies, and api for transitive ones (if the dependencies are consumed directly by dependents of your project).

Solution 3

The compile configuration has been deprecated in favor of implementation.

See docs.

Share:
11,420
kensuke1984
Author by

kensuke1984

Updated on June 05, 2022

Comments

  • kensuke1984
    kensuke1984 almost 2 years

    My current jar in build.gradle is like below:

    jar {
    manifest {
        attributes "Main-Class": "hoge.Main"
    }
        from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
    

    and working well.

    However, I have a message from Gradle (maybe since 6+?)

    This is the actual message:

    The compile configuration has been deprecated for dependency declaration. This will fail with an error in Gradle 7.0. Please use the implementation configuration instead.
    

    The part configurations.compile is deprecated.

    How can I update it?

    If I changed

    jar {
    manifest {
        attributes "Main-Class": "hoge.Main"
    }
        from configurations.implementation.collect { it.isDirectory() ? it : zipTree(it) }
    }
    

    Gradle says > Resolving configuration 'implementation' directly is not allowed

  • kensuke1984
    kensuke1984 over 4 years
    I know it. I asked how to update my code for the deprecation.
  • smac89
    smac89 over 4 years
    @kensuke1984 How to do it is to replace all use of compile with implementation
  • kensuke1984
    kensuke1984 over 4 years
    yeah Thank you but when I did it, I have "> Resolving configuration 'implementation' directly is not allowed"
  • smac89
    smac89 over 4 years
    @kensuke1984 you are now asking a new question. See stackoverflow.com/questions/47910578/…
  • Vishwa Ratna
    Vishwa Ratna over 4 years
    compile to implementation has been a major change.
  • kensuke1984
    kensuke1984 over 4 years
    Thanks... imperceptiblethoughts.com/shadow I finally use it. I wonder if there are ways to simply do this kind of things without importing it.