Gradle - FTSRG/cheat-sheets GitHub Wiki
Groovy
On Linux, Gradle does not install a separate Groovy engine. The Ubuntu PPA packages are quite outdated, so the simplest solution is to use sdkman.
Tutorials
Troubleshooting
Building Groovy script using Grapes & Grab with Gradle
Problem: You're trying to build a Groovy script using Gradle, and the script uses Grapes/Grab to resolve its dependencies. However, gradle build
throws an exception (displayed here with gradle build --stacktrace
).
* What went wrong:
Execution failed for task ':compileGroovy'.
> org/apache/ivy/core/report/ResolveReport
[...]
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileGroovy'.
[...]
Caused by: java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport
at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:167)
at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:54)
at org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonServer.execute(CompilerDaemonServer.java:55)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:364)
... 2 more
Caused by: java.lang.ClassNotFoundException: org.apache.ivy.core.report.ResolveReport
... 8 more
Solution: Add the ivy
configuration (see the example).
apply plugin: 'groovy'
repositories {
mavenCentral()
}
configurations {
ivy
}
dependencies {
compile group: 'org.codehaus.groovy', name: 'groovy', version: '2.4.6'
ivy group: 'org.apache.ivy', name: 'ivy', version: '2.4.0'
}
tasks.withType(GroovyCompile) {
groovyClasspath += configurations.ivy
}
"That said, a better approach is to manage dependencies with Gradle." (Source: http://stackoverflow.com/questions/18173908/error-compiling-a-groovy-project-using-grab-annotation)
So you're generally much better off using Gradle to resolve the dependencies as well:
apply plugin: 'groovy'
dependencies {
compile localGroovy()
}
sourceSets {
main {
groovy {
srcDir '.'
}
}
}
task runScript(dependsOn: 'classes', type: JavaExec) {
main = 'benchmark'
classpath = sourceSets.main.runtimeClasspath
}
The localGroovy()
dependency places the local Groovy sources to the classpath. The srcDir
part sets the source directory to the current directory. To run the script, use the runScript
goal:
$ gradle runScript
Sesame cannot find RDF format
Problem:
Exception in thread "main" org.openrdf.rio.UnsupportedRDFormatException: No parser factory available for RDF format Turtle (mimeTypes=text/turtle, application/x-turtle; ext=ttl)
Solution:
As described in the documentation of the Shadow plug-in, simply add:
shadowJar {
mergeServiceFiles()
}
Re-run Gradle with gradle clean shadowJar
.
The related Maven question is discussed on Stack Overflow
DefaultExternalModuleDependency_Decorated
Could not create Problem: the build throws the following error.
* What went wrong:
A problem occurred evaluating project '...'
> Could not create an instance of type org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency_Decorated.
Solution: this obscure error message is usually caused by writing compile group:
instead of compile
So change this:
compile group: "group:artifact:version"
To this:
compile "group:artifact:version"
Include Java/Scala source code in the build
Java
apply plugin: "java"
jar {
from sourceSets.main.allJava
}
Scala
apply plugin: "scala"
jar {
from sourceSets.main.allScala
}
Older versions of the Eclipse Buildship plug-in fail on import
-
Problem: during import, you may encounter the following error:
Synchronize Gradle builds with workspace failed due to an unexpected error. Unsupported method: HierarchicalEclipseProject.getIdentifier().
-
Solution #1: click Previous and set the Gradle wrapper's version manually to 3.0 (in our experience, 3.1 and 3.2 do not work).
-
Solution #2: update Buildship.
Build errors for IDEA & Scala projects
It seems that there are some compatibility issues in multiple-project builds:
apply plugin: 'idea'
apply plugin: 'scala'
This results in:
* What went wrong:
A problem occurred evaluating project ':...'
> Failed to apply plugin [class 'org.gradle.api.plugins.scala.ScalaBasePlugin']
> A dependency must not be empty
Solution: remove the idea
plugin.
Creating JAR files with all dependencies
If you want to create a JAR file with all dependencies (fat JAR), one option is to use the Shadow plugin. However, it is quite complicated to make it work if you want to define multiple JAR tasks for the same project (e.g., because there are multiple main
functions). Another possibility is presented in this tutorial, which does not involve any external dependencies, and it is possible to extend it for multiple JARs in the following way.
First define a function for creating a task.
def createJarTask(def taskName, def mainClass, def jarBaseName) {
return tasks.create(taskName, Jar) {
manifest {
attributes 'Main-Class': mainClass
}
baseName = jarBaseName
classifier = null
version = null
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
}
And then call it for each JAR.
createJarTask("jarTask1", "com.example.main1", "main1")
createJarTask("jarTask2", "com.example.main2", "main2")