Search code examples
jargradlebuild-script

Gradle - How to run a JAR provided by buildscript


I would like to have a gradle script that executes a jar file. The problem that this jar file should be downloaded from maven central. I checked that the class exists in the lib.

apply plugin: 'application'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "com.nativelibs4java:jnaerator:0.11"
    }
}

task x(type:JavaExec) {
    main = "com.ochafik.lang.jnaerator.JNAerator"
}

But when I start I get

╰─➤  gradle clean x
:clean UP-TO-DATE
:x
Fehler: Hauptklasse com.ochafik.lang.jnaerator.JNAerator konnte nicht gefunden oder geladen werden
:x FAILED

Solution

  • JavaExec forks a new process so the script classpath is gone. You need to configure it:

    task x(type:JavaExec) {
        main = "com.ochafik.lang.jnaerator.JNAerator"
        classpath = buildscript.configurations.classpath
    }