Generate a single executable jar with dependent libraries in Gradle 4.9

Note

problem

Until now, I used this to generate an all-in-one JAR.

--Use gradle to generate a single executable jar containing dependent libraries https://qiita.com/mychaelstyle/items/9338f558903ac0c14bc3


However, when Gradle is set to 4.9, Jar without dependent files is created. I changed compile to implementation, so

from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }


 The description

#### **`from configurations.implementation.collect { it.isDirectory() ? it : zipTree(it) }`**

When I changed it to, I got angry as follows

C:\~~>gradlew

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\~~\build.gradle' line: 129

* What went wrong:
A problem occurred evaluating root project '~'.
> Resolving configuration 'implementation' directly is not allowed

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s

If the name is wrong

api' not found.


 It seems that it is clearly blocked internally.

 Even if I searched for it in the error statement, only Issue came out, so I looked it up.

# Measure

 It is said that `` `Configuration.isCanBeResolved ()` `` was added from Gradle 3.3 here.

- Gradle 3.4-rc-1: "Resolving configuration 'apiElements' directly is not allowed" #31 https://github.com/jeremylong/dependency-check-gradle/issues/31

 In other words, whether or not it can be collected for each Configuration is different.

---

 So I searched for a list of dependent Jar from the resolvable Configurations.
 According to this, `` `configurations``` is a Collection, so I checked each one with the following code.

- https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ConfigurationContainer.html

```gradle
configurations.stream().forEach {
	println "################### " + it
	if (it.canBeResolved) {
		it.collect {
			println it.isDirectory() ? it : zipTree(it)
		}
	}
}

List of obtained Configuration


configuration ':annotationProcessor'
configuration ':api'
configuration ':apiElements'
configuration ':archives'
configuration ':compile'
configuration ':compileClasspath'
configuration ':compileOnly'
configuration ':default'
configuration ':implementation'
configuration ':junitPlatform'
configuration ':runtime'
configuration ':runtimeClasspath'
configuration ':runtimeElements'
configuration ':runtimeOnly'
configuration ':testAnnotationProcessor'
configuration ':testCompile'
configuration ':testCompileClasspath'
configuration ':testCompileOnly'
configuration ':testImplementation'
configuration ':testRuntime'
configuration ':testRuntimeClasspath'
configuration ':testRuntimeOnly'

Of these, I felt that `` `compileClasspath``` was something like that, so I'll try using it.

Conclusion

The result is a Jar with all the dependencies as before with the following code:

from configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }






Recommended Posts

Generate a single executable jar with dependent libraries in Gradle 4.9
Make multi-project executable JAR in Gradle
Add a project in any folder with Gradle
Getting started with Gradle (until you create a Java project and combine external libraries into one executable JAR)
Gradle + Kotlin-Generate jar with DSL
Copy dependent jars in Gradle 5
Create a jar file that can be executed in Gradle
How to make a jar file with no dependencies in Maven
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Add a time stamp to the JAR file name in Gradle
Build a Java project with Gradle
What to do if zip dies if there is a pom when making an executable jar with gradle
(Note) Get a set of dependent library jars with the help of Gradle
Create a jar file with the command
Generate DB documentation using SchemaSpy in Gradle
Split a string with ". (Dot)" in Java
How to output jar with main class specified by gradle in Intellij IDEA