I'm writing a gradle build file that will install a basic development domain for our product. Essentially all of the real code is going to be in custom plugins and custom tasks. Several of the steps involved are fairly repetitive (multiple sudo calls, multiple user adds) and I would like to encapsulate the common stuff into a task.
For example:
task('addDBUser', type:AddUser) {
username = joeUser
}
task('startService', type:SudoExec) {
workingDir = "not/too/relevant"
commandLine = "/etc/init.d/coolService start"
}
I'd like to reuse the various functionality that Exec gets me (stdin, stdout, etc.) as tidily as possible, while supplying the boilerplate ("sudo ...") automatically. I'm pretty sure I can just extend Exec instead of DefaultTask, but I don't know a standard way of triggering actual action. It seems easy to modify the commandLine property with what I need, but there's no generic "run()" or the like to use when I want Exec to actually go.
Do I open up Exec to identify which method is it's work method and then invoke it directly? Or is there a more generic way of accomplishing my goal?
To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … e.g. gradlew clean allTests.
To list all the tasks of the project, run the below command: gradle tasks -all.
Parallel execution Yet Gradle will only run one task at a time by default, regardless of the project structure (this will be improved soon). By using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects.
gradle file, which is located in the subdir/. task hello << { println "using build file '$buildFile.name' in '$buildFile.parentFile.name'." } You can use the following command to execute the above script. using build file 'myproject.
To see which method is executed for a task you can check the sources of Exec
and search for a method marked with @TaskAction
. It turns out that it's the exec()
method but in general you don't want to be calling task actions manually but let Gradle do it for you. The best idea in my opinion is to add methods/setters to your custom tasks. It might look like that:
task addUser(type: AddUser) {
username = 'fromGradle'
}
class SudoExec extends Exec {
void sudoCommand(Object... arguments) {
executable 'sudo'
args = arguments.toList()
}
}
class AddUser extends SudoExec {
void setUsername(String username) {
sudoCommand('useradd', username)
}
}
This code is able to handle multiple parameters, as it doesn't hook into setters of any specific parameters, but uses lazy GString evaluation.
task vagrantfile (type:Vagrantfile) {
account 'vagrant'
password 'vagrant'
}
class Vagrantfile extends Copy {
String account
String password
def Vagrantfile() {
from 'templates/'
into 'build/'
include 'Vagrantfile.ubuntu.tpl'
rename {'Vagrantfile'}
expand (account:"${->account}", password:"${->password}")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With