首页 > 解决方案 > 如何在 Gradle 中实现“应用程序运行”任务?

问题描述

我正在使用 Gradle,我想知道如何实现运行任务,以便可以从命令“./gradlew run”运行程序。我有一个名为“演示”的项目,它的任务是“应用程序运行”。然后我创建了项目“HouseObserver”,它没有任务“应用程序运行”。

在此处输入图像描述

我的 build.gradle 文件看起来像这样。

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'

}

我也在尝试从插件中实现任务的方法。

plugins {
    id 'application'
}

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

但什么也没有发生。为什么?

编辑:

这是我最新的 gradle 文件。

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'

}

标签: javagradlegradle-pluginjmonkeyenginegradle-task

解决方案


推荐阅读