如何从命令行使用Kotlin运行spark java

时间:2017-05-29 14:47:55

标签: gradle kotlin spark-java

我对Java很新(大概7年没做过)。我正在使用Gradle,spark-java来创建一个简单的hello world应用程序,只运行它:

import static spark.Spark.*;

fun main(args: Array<String>) {
  get("/hello", (req, res) -> "Hello World!");
}

在eclipse中,我可以通过获取gradle插件并启动一个新的gradle项目来实现这一点。但也有&#34; Kotlin&#34;项目。所以你不能用kotlin开始一个新的gradle项目。

我想要了解的是,使用JavaScript和Node我通常会通过命令行执行所有操作。我想构建这个项目并使用Gradle从命令行运行服务器,但我不知道该怎么做。

这是我目前的build.gradle:

// Apply the kotlin plugin to add support for Kotlin
apply plugin: 'kotlin'

buildscript {
    ext.kotlin_version = '1.1.2-4'

    repositories {
        jcenter()
        mavenCentral()
    }

    sourceSets {
        main.kotlin.srcDirs += 'src/main/myKotlin'
        main.java.srcDirs += 'src/main/myJava'
    }

    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-4'
    }
}

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'com.sparkjava:spark-core:2.3'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}

我在命令行上运行gradle并且它已成功构建但是如何启动服务器以便我可以转到localhost:4567并查看我的Hello World输出?我应该遵循的一般流程是什么?

要明确:我正在做出使用Kotlin而不是Java的良心决定。

0 个答案:

没有答案