Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 805
kensuke1984 Avatar asked Dec 17 '19 03:12

kensuke1984


People also ask

How can I create an executable jar with dependencies using Gradle?

In its simplest form, creating an executable JAR with Gradle is just a matter of adding the appropriate entries to the manifest. However, it's much more common to have dependencies that need to be included on the classpath, making this approach tricky in practice.

Where are Gradle dependency jars?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file). This declares a dependency on version 12.3 of the "app-magic" library, inside the "com.example.android" namespace group.


1 Answers

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

like image 72
Andrei Avatar answered Oct 06 '22 01:10

Andrei