This question already has answers here:
Console application with Java and gradle
(4 answers)
Closed 4 months ago.
NOTE: I have just marked this question as a duplicate.
Here is the answer: Console application with Java and gradle
I created a Gradle application that simply receives some input as a String?. When I try using the "run" task, stringInput evaluates to null automatically. I have already tested this with the Kotlin compiler in the terminal, and it works as expected.
Here is the main code below:
package com.s12works.readLineTest
fun main() {
print("Enter text: ")
val stringInput: String? = readLine()
println("You entered: $stringInput")
}
The output from running this application with the run task:
**> Task :app:run**
Enter text:
You entered: null
How can I stop this?
Any help would be appreciated.
The run task of the application plugin is of type JavaExec. As you can see in the DSL reference for the JavaExec (https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html#org.gradle.api.tasks.JavaExec:standardInput) the default input stream is set to empty. To change this behavior you can add the following to your build script:
run {
standardInput = System.in
}
Related
This question already has an answer here:
Using environment variables in Karate DSL testing
(1 answer)
Closed 2 years ago.
I am trying to pass some cmd line args from gradle to be used in karate-config.js.
Cmd: ./gradlew test -Denv=qa -Dmodule=payments
I looked at https://github.com/intuit/karate#command-line and followed similar steps and put this in build.gradle:
test {
...
systemProperty "karate.env", System.properties.getProperty("env")
systemProperty "karate.module", System.properties.getProperty("module")
}
Now in karate-config.js, I have code like below:
var environmentvar = karate.env;
var modulevar = karate.module;
environment var (karate.env) variable gets the correct value, but module var (karate.module) always shows as undefined. Any pointers on how to fix this?
Karate 0.9.4
JDK 1.8.0_231
Aren't you missing a karate. for e.g.:
System.properties.getProperty("karate.env")
Just didn't regcognized the important information that accessing karate.env works.
The environment variable karate.env is treated special. Using karate object to access other system properties the same way does not work.
You should read accessing system properties.
Solution: Use karate.properties['prop.name'] to access your module system variable.
In your case:
var environmentvar = karate.env;
var modulevar = karate.properties['module'];
This question already has an answer here:
How to create global variable in karate? [duplicate]
(1 answer)
Closed 1 year ago.
I'm trying to configure different testing targets via karate-config-<env>.js files located in the same directory.
When I try to execute the tests against the different target-systems:
mvn test -Dkarate.env=int02 (tried: -DargLine="-Dkarate.env=int02")
the karate-config-int02.js file is not executed and the test execution gets stuck somewhere.
I've read the documentation, but for now I found no working example.
I am working with karate 0.9.4 on macOS with Java 1.8 in a maven 3.6.0 example-project for a prof of concept.
Extending the pom file, as shown below, was also not working:
<properties>
<karate.env>int02</karate.env>
</properties>
I though that via the -Dkarate.env=int02 I could ensure that the karate-config-int02.js would be used to configure the instance specific properties I need.
I do have a line in both karate-config files like:
karate.log('karate-config|karate-config-int02 is called')
but I always see:
karate-config is called
The simplest way and how 90% of projects do it is with just the one karate-config.js and then some if else JS logic as per the docs. Maybe you can stick to that.
var env = karate.env || 'dev';
var config = { someUrlBase: 'https://localhost:8080/' };
if (env == 'stage') {
// over-ride only those that need to be
config.someUrlBase = 'https://stage-host/v1/auth';
} else if (env == 'e2e') {
config.someUrlBase = 'https://e2e-host/v1/auth';
}
return config;
Else please follow this process so we can figure out what could be wrong: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
When using Kotlin's Script Engine, trying to import packages or use any class throws an "unresolved reference"
javax.script.ScriptException: error: unresolved reference: mrpowergamerbr
fun loritta(context: com.mrpowergamerbr.loritta.commands.CommandContext) {
^
This doesn't happen when running the class within IntelliJ IDEA, however it does happen when running the class on production.
While this YouTrack issue is related to fat JARs, this also can happen if you aren't using fat JARs (loading all the libraries via the startup classpath option or the Class-Path manifest option)
To fix this, or you can all your dependencies on your startup script like this:
-Dkotlin.script.classpath=jar1:jar2:jar3:jar4
Example:
java -Dkotlin.script.classpath=libs/dependency1.jar:libs/dependency2.jar:yourjar.jar -jar yourjar.jar
Or, if you prefer, set the property via code, using your Class-Path manifest option.
val path = this::class.java.protectionDomain.codeSource.location.path
val jar = JarFile(path)
val mf = jar.manifest
val mattr = mf.mainAttributes
// Yes, you SHOULD USE Attributes.Name.CLASS_PATH! Don't try using "Class-Path", it won't work!
val manifestClassPath = mattr[Attributes.Name.CLASS_PATH] as String
// The format within the Class-Path attribute is different than the one expected by the property, so let's fix it!
// By the way, don't forget to append your original JAR at the end of the string!
val propClassPath = manifestClassPath.replace(" ", ":") + ":Loritta-0.0.1-SNAPSHOT.jar"
// Now we set it to our own classpath
System.setProperty("kotlin.script.classpath", propClassPath)
While I didn't test this yet, in another unrelated answer it seems you can also supply your own classpath if you initialize the KotlinJsr223JvmLocalScriptEngine object yourself (as seen here)
I need to determine if my Grails application is currently under testing. I cannot use if (Environment.getCurrent() == Environment.TEST), because my current environment is Environment.CUSTOM with name jenkins. Is there an other way to find out if it's currently under testing?
One approach I've used is to set an environment variable by hooking into the eventTestPhaseStart GANT event. You can do this by creating a script named Events.groovy in /scripts.
<project dir>/scripts/Events.groovy:
eventTestPhaseStart = { args ->
System.properties['grails.testPhase'] = args
}
You can use it like so to determine if the app is under testing:
if (System.properties['grails.testPhase'] != null)
println "I'm testing!"
You can also use it to have specific behavior for a particular test phase:
if (System.properties['grails.testPhase'] == 'integration')
println "Do something only when running integration tests."
I am playing a bit with scala, using maven and scala plugin.
I can't find a way to have
mvn test
report failure details - in particular, whenever some function returns wrong reply, I am getting information about the failure, but I have no way to see WHICH wrong reply was reported.
For example, with test like:
object MyTestSpec extends Specification {
"my_function" should {
"return proper value for 3" {
val cnt = MyCode.my_function(3)
cnt must be_==(3)
}
}
}
in case my function returns something different than 3, I get only
Failed tests:
my_function should return proper value for 3
but there is no information what value was actually returned.
Is it possible to get this info somehow (apart from injecting manual println's)?
The Maven JUnit plugin only shows the failed tests in the console and not error messages. If you want to read the failure message you need to open the corresponding target/site/surefire-reports/MyTestSpecs.txt file.
You can also swap Maven for sbt-0.6.9 (Simple Build Tool) which can run specs specifications.
Eric.
cnt aka "my_function return value" must be_==(3)