How do I go about specifying the amount of threads to run when I run this gradle command in terminal?
my build.gradle file
task runTask3(type: JavaExec) {
group 'server'
description 'Creates Server socket waits for messages'
classpath = sourceSets.main.runtimeClasspath
main = 'taskone.ThreadPoolServer'
standardInput = System.in
// run with arguments e.g.: gradle runTask3 -Pport=9099 -q --console=plain
if (project.hasProperty('port')) {
args(project.getProperty('port'));
}
}
Related
i have a problem with a gradle exec task.
code:
task startup {
final GradleVersion gradleVersion = GradleVersion.current()
println "##### start script #####"
println "gradle version: ${gradleVersion}"
println "project: ${project.group}.fmp-${project.name}-${project.version}"
}
task create(type: Exec) {
workingDir "./scripts"
commandLine "./create_service_from_template.sh", sn, gwv
}
So when I execute gradle create -Psn=test -Pgwv=4.0 then it works.
but when I start another task (like gradle startup then an error appears:
Could not get unknown property 'sn' for task ':create' of type org.gradle.api.tasks.Exec.
I thought I can put the lines in the create Task into a doLast block, but then it doesn't work.
Can some one help me please?
Thanks.
This happens because Gradle will run the Configuration phase on all tasks.
One fix is to use local variables which are initialized with a check to see if the property was specified. See the beginning:
def sn = project.hasProperty('sn') ? project.sn : ''
def gwv = project.hasProperty('gwv') ? project.gwv : ''
task startup {
final GradleVersion gradleVersion = GradleVersion.current()
println "##### start script #####"
println "gradle version: ${gradleVersion}"
println "project: ${project.group}.fmp-${project.name}-${project.version}"
}
task create(type: Exec) {
workingDir "./scripts"
commandLine "./create_service_from_template.sh", sn, gwv
}
I have a gradle project containing two modules in subdirectories. The directory structure is as below.
root
module1
build.gradle
module2
build.gradle
build.gradle
settings.gradle
The top level settings.gradle includes the two modules. The top level build.gradle contains the following.
task runScript(type: Exec) {
workingDir 'scripts'
commandLine 'python3', 'myscript.py'
}
project(':module1') {
evaluationDependsOn(':module1')
final test = tasks.findByName('test')
test.dependsOn(runScript)
}
project(':module2') {
evaluationDependsOn(':module2')
final test = tasks.findByName('test')
test.dependsOn(runScript)
}
Task runScript sets the database to a known state and must be run before each module test task.
When I run the test task my script only executes once. How can I ensure it executes multiple times?
$ ./gradlew test
... some output
:runScript
RUNNING MY SCRIPT
:module1:test
RUNNING MODULE1 TESTS
... some output
:module2:test
RUNNING MODULE2 TESTS
Things I Tried
I tried adding outputs.upToDateWhen {false} to the task runScript so Gradle never thinks it is up to date. This didn't make any difference; I assume because the task still only occurs once in the task graph?
I tried replacing the lines containing dependsOn for each module with test.doFirst {runScript.execute()}. This changes when the task gets executed but does not result in multiple executions.
$ ./gradlew test
... some output
:module1:test
RUNNING MY SCRIPT
RUNNING MODULE1 TESTS
... some output
:module2:test
RUNNING MODULE2 TESTS
I tried creating a new task for each module. This works but it's duplicating code.
project(':module1') {
evaluationDependsOn(':module1')
final test = tasks.findByName('test')
task runScript(type: Exec) {
workingDir '../scripts'
commandLine 'python3', 'myscript.py'
}
test.dependsOn(runScript)
}
project(':module2') {
evaluationDependsOn(':module2')
final test = tasks.findByName('test')
task runScript(type: Exec) {
workingDir '../scripts'
commandLine 'python3', 'myscript.py'
}
test.dependsOn(runScript)
}
If your script is necessary for each run of each Test task, simply assure its execution before each Test task. Why even use a task then?
subprojects {
tasks.withType(Test) {
doFirst {
exec {
workingDir 'scripts'
commandLine 'python3', 'myscript.py'
}
}
}
}
I want execute some command from command line in gradle task(e.g. print all files in dir):
task dir(type: Exec) {
def adbCommand = ["dir", "*.*"]
commandLine adbCommand
standardOutput = new ByteArrayOutputStream()
doLast {
println ("result = " + standardOutput)
}
}
It's work. OK. But when I put it on onLast section it's not work:
task dir(type: Exec) {
doLast {
def adbCommand = ["dir", "*.*"]
commandLine adbCommand
standardOutput = new ByteArrayOutputStream()
println ("result = " + standardOutput)
}
}
I get error:
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:dir'.
execCommand == null!
The reason is in the fact, that task of Exec should be configured during configuration phase of the build, otherwise your task will be not configured and fail.
In you first example everything works due to configuration happens at the configuratyion phase. Your second example tries to configure the task within doLast closure - right after the task is executed yet.
If you really need to execute something in doLast, you can use something like this, without creating special task:
task someTaskName {
doLast {
exec {
commandLine adbCommand
}
}
}
Here is exec-specification used to execute some command and it's configured and executed at the same time.
My gradle setup accepts arguments during runtime which are checked in a shell script that Gradle Exec task calls. However, in order to reach that point, Gradle deals with dependencies and spends a good amount of time before the end script is executed, which would then throw an error if no arguments are passed.
Build task in gradle looks like below:
task buildAll(type: Exec) {
environment['PROJECT_ROOT'] = "${projectDir}"
workingDir rootProject.projectDir.path
executable rootProject.projectDir.path + "/script.ksh"
if (project.hasProperty('ARGS')) {
args(ARGS.split(','))
}
}
Gradle is called as follows:
./gradlew build -PARGS="-arg1,value1,-arg2,value2,-arg3,-arg4,value4"
I intend to check for -arg2 and if it is not provided, I would like the gradle build to fail with some sort of usage displayed. Is this possible?
You can add an if block at the very beginning of the script:
if (!project.hasProperty('lol')) {
throw new GradleException("'lol' property is required!")
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.guava:guava:18.0'
}
}
however it will not prevent gradle from resolving the dependencies of the script itself - buildscript block will be evaluated first.
I am trying to run my app using a Gradle javaexec task. However, jvmargs and args are not passed to the command execution. Why?
task runArgoDev(type: JavaExec) {
main = "org.app.ArgoDevRunner"
classpath = configurations.testRuntime
project.ext.jvmargs = ['-Xdock:name=Argo', '-Xmx512m', '-Dfile.encoding=UTF-8', '-Dapple.awt.textantialiasing=on', '-ea']
project.ext.args = ['-initParameter', 'implicit-scrollpane-support=true']
}
Above code doesn't have the desired effect because it sets extra properties on the project object, instead of configuring the task. Correct is jvmArgs = ... and args = .... (It's also possible to omit =, [, and ].)
Here is example, to pass program args and jvmargs to run task in gradle.
run {
args 'server', 'test.yml'
jvmArgs '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'
}