2

I'm using Gradle and I wonder how I can implement a run task so I can run the program from the command "./gradlew run". I have a project named "demo" and it have the task "application run". Then I created the project "HouseObserver" and it have not the task "application run".

enter image description here

My build.gradle file looks like this.

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

apply plugin: 'java'

task runApp(type: JavaExec) {
  classpath = sourceSets.main.runtimeClasspath

  main = 'HouseObserver.Main'
}

// Using and creating an Executable Jar
jar {
  manifest {
    attributes('Main-Class': 'HouseObserver.Main')
  }
}

task runExecutableJar(type: JavaExec) {
  // Executable jars can have only _one_ jar on the classpath.
  classpath = files(tasks.jar)

  // 'main' does not need to be specified

}



dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:28.0-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    // Need at least basic JME
    compile "org.jmonkeyengine:jme3-core:3.3.0-beta1"
    compile "org.jmonkeyengine:jme3-desktop:3.3.0-beta1"
    compile "org.jmonkeyengine:jme3-lwjgl3:3.3.0-beta1"

    compile group: 'com.simsilica', name: 'lemur', version: '1.13.0'
    compile group: 'com.simsilica', name: 'lemur-proto', version: '1.11.0'

    // needed for the style language
    runtime "org.codehaus.groovy:groovy-all:2.4.5"

    // Standard utility stuff
    compile 'com.google.guava:guava:19.0'
    compile 'org.slf4j:slf4j-api:1.7.13'
    runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
    runtime 'org.apache.logging.log4j:log4j-core:2.5'

}

I'm also trying the way to implement a task from a plugin.

plugins {
    id 'application'
}

application {
    mainClassName = 'my.packages.to.the.Main'
}

But nothing happens. Why?

EDIT:

Here is my latest gradle file.

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
    id 'application'
}

application {
    mainClassName = 'HouseObserver.Main'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:28.0-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    // Need at least basic JME
    compile "org.jmonkeyengine:jme3-core:3.3.0-beta1"
    compile "org.jmonkeyengine:jme3-desktop:3.3.0-beta1"
    compile "org.jmonkeyengine:jme3-lwjgl3:3.3.0-beta1"

    compile group: 'com.simsilica', name: 'lemur', version: '1.13.0'
    compile group: 'com.simsilica', name: 'lemur-proto', version: '1.11.0'

    // needed for the style language
    runtime "org.codehaus.groovy:groovy-all:2.4.5"

    // Standard utility stuff
    compile 'com.google.guava:guava:19.0'
    compile 'org.slf4j:slf4j-api:1.7.13'
    runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
    runtime 'org.apache.logging.log4j:log4j-core:2.5'

}
euraad
  • 2,467
  • 5
  • 30
  • 51
  • See if you can find some inspiration from how bootmonkey does it: https://github.com/Nehon/bootmonkey/blob/master/build.gradle – reden Feb 14 '20 at 13:46
  • 1
    Have a look at the gradle application plugin, this plugin will ad the run task automatically – RoiEX Feb 14 '20 at 13:50
  • @RoiEX It dosent. – euraad Feb 14 '20 at 13:51
  • Ah, thanks for updating your files in the post, try moving the mainClassName part out of the application closure and see if that works – RoiEX Feb 14 '20 at 13:58
  • @RoiEX I should delete it? – euraad Feb 14 '20 at 13:59
  • 1
    The application {} part yes, the mainClassName = part no – RoiEX Feb 14 '20 at 14:00
  • Reconsidering my suggestion, according to the docs, putting it inside the closure is actually the way to go (even though setting it outside it should work as well), so you'll have to clarify what exactly isn't working for you. Maybe your main method is actually run, but just happens to immediately return – RoiEX Feb 14 '20 at 14:05
  • 1
    @RoiEX Yes! It's working now. I had to close and then open the project again so the tasks will be reloaded. – euraad Feb 14 '20 at 14:05
  • @RoiEX But I don't understand why gradle learn out this way, when your way is working? – euraad Feb 14 '20 at 14:06
  • Most likely you just needed to re-import Gradle in order for the IDE to become aware of the new configuration(s). Not sure how to do this in Eclipse, however. – Slaw Feb 14 '20 at 14:08
  • Also, while I don't know which Gradle version you're using, note that the `compile` and `runtime` dependency configurations [are both deprecated](https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations) in favor of `implementation` and `runtimeOnly`, respectively. And if this is a new project you may want to consider using [JUnit 5](https://junit.org/junit5/docs/current/user-guide/) instead of JUnit 4. – Slaw Feb 14 '20 at 14:12
  • @Slaw Yes. It's a new project. I'm using Gradle 5.6.1 – euraad Feb 14 '20 at 14:14
  • The aforementioned dependency configurations were deprecated since at least that version ([same documentation page but for version 5.6.1](https://docs.gradle.org/5.6.1/userguide/java_plugin.html#tab:configurations)). – Slaw Feb 14 '20 at 14:17

0 Answers0