遇到问题
做task的时候,需要automated build,用的工具是gradle,build文件是kotlin版本的。 但是当使用scanner的时候会遇到报错NoSuchElementException 异常, 样例代码如下:
public static void main(String[] args) {
System.out.print("Please enter a number greater than 0: ");
Scanner scanner = new Scanner(System.in);
var inputNumber = scanner.nextLong();
play(inputNumber, PerfectNumberUI::call);
}
解决方法
在stackoverflow上面找到类似的问题:
需要在build.gradle.kts文件中添加(笔者用的是kotlin)
tasks.named<JavaExec>("run") {
standardInput = System.`in`
}
如果是groovy版本,则添加:
tasks {
run {
standardInput = System.`in`
}
}
这样就可解决问题。
gradle的设置文件模板
另外,将该项目的gradle设置文件放出来,方便以后使用: TDD(Juint5),以及code coverage
plugins {
java
jacoco
pmd
application
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation("org.testng:testng:7.5")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.8.2")
testImplementation("org.junit.platform:junit-platform-console:1.8.2")
}
tasks {
val flags = listOf("-Xlint:unchecked", "-Xlint:deprecation", "-Werror")
getByName<JavaCompile>("compileJava") {
options.compilerArgs = flags
}
getByName<JavaCompile>("compileTestJava") {
options.compilerArgs = flags
}
}
sourceSets {
main {
java.srcDirs("FinalProject/src")
}
test {
java.srcDirs("FinalProject/test")
}
}
val test by tasks.getting(Test::class) {
useJUnitPlatform {}
}
pmd {
ruleSets = listOf()
ruleSetFiles = files("./conf/pmd/ruleset.xml")
toolVersion = "6.37.0"
}
tasks.withType<JacocoReport> {
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.map {
fileTree(it).apply {
exclude("*/preview
参考
https://stackoverflow.com/questions/36723447/java-util-scanner-throws-nosuchelementexception-when-application-is-started-with
https://docs.gradle.org/current/userguide/kotlin_dsl.html
|