7

I am trying to make a task to run my Jar file in gradle.

I have come up with the following:

task runJar(dependsOn:[jar]){
  ant.java(jar:,fork:true)
}

However, I am unable to find the path to the jar file. Any help much appreciated. Thank you!

Misha

EDIT: OK this is rather odd. This task runs before compile, etc.???

EDIT: Fixed. The key is in a doLast { } notation, or, in shorthand

task runJar(dependsOn:"jar")<<{
  ant.java(jar:"${libsDir}${File.separator}${archivesBaseName}.jar",fork:true)
}

Misha

Миша Кошелев
  • 1,483
  • 1
  • 24
  • 41

4 Answers4

9

Koppor's answer works perfectly.

With a Main.java in src/main/java, my build.gradle file looks like

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "Main"

Running it gives:

gradle run                 

:compileJava
:processResources UP-TO-DATE
:classes
:run
[Main.java's output]
mmm111mmm
  • 3,607
  • 4
  • 27
  • 44
3

Are you looking for the The Application Plugin? It creates a new task "run", which runs the specified java class.

koppor
  • 19,079
  • 15
  • 119
  • 161
2

You should be able to use the jar.archivePath variable ;

task runJar(dependsOn:[jar]){
  ant.java(jar: jar.archivePath ,fork:true)
}
TheKaptain
  • 1,011
  • 9
  • 10
1

My best solution so far:

task runJar(dependsOn:[jar]){
  ant.java(jar:"${libsDir}${File.separator}${archivesBaseName}.jar",fork:true)
}

Thank you!

Misha

Миша Кошелев
  • 1,483
  • 1
  • 24
  • 41
  • 1
    Hi! I've tried your solution, but I have no output from the jar - I only get a Java Result: 1 information, though a I have wrtitten to `System.out` in my `main` method... How do I get the output? – emesx Mar 17 '12 at 09:33
  • I need to provide **args**. – IgorGanapolsky Nov 02 '16 at 21:47