How to provide command line argument to build task?
For instance, I want to download 60 build version from an archive to my local server path.
Could you please suggest how can I achieve this?
Example task:
task download(type: Download) {
src 'http://archiva/repository/test/$version/project-$version.jar'
dest new File(buildDir, '../../../test/project.jar')
username 'username'
password 'password'
}
gradle download version=60
You can use Project Properties for this purpose (see Project Properties)
Example: consider the following task
task hello{
doLast{
println "Hello ${project.findProperty('myProp')}"
}
}
You can pass property value as follows:
./gradlew hello -PmyProp=world
Note you should use another variable name than "version", as version is already a Gradle property attached to the project.
Note 2 : I noticed you are using simple quote for src value, this cannot work. You need to se double-quote for String interpolation ( see here ):
Use:
src "http://archiva/repository/test/$version/project-$version.jar"
instead of:
src 'http://archiva/repository/test/$version/project-$version.jar'
Related
this seems like it should be very simple, but I'm stuck! I have a gradle task that zips some files and creates an archive. I want to suffix the name of the archive with the version string from the project I'm in (this is a subproject in a multi-project build)
there's another task in this same build.gradle file that accesses this version field, that I want to access too. it looks like this:
task releasePublishAuto(dependsOn: [tasks.cppApiVersion, rootProject.tasks.releasePublish]) {
doLast {
println("############################################################")
println("# PUBLISHED CPP API WITH VERSION: $version")
println("############################################################")
}
}
you can see it accessing the version variable and printing it using $version. it prints something like # PUBLISHED CPP API WITH VERSION: 1.256.4-SNAPSHOT
now, my task is a zipping task, so it's a bit different:
task zipGeneratedCppClientApi(type: Zip, group: "code generator", dependsOn: [tasks.cppApiVersion, "generateCormorantCPP"]) {
from(fileTree(dir: "$buildDir/generated/cormorant_cpp", include: "**/*"))
archiveFileName = "$buildDir/distributions/generated-cpp-api-$version"
}
I would expect this to create a zipfile named generated-cpp-api-1.256.4-SNAPSHOT but instead i get one named generated-cpp-api-null. As far as I can tell this is because in the context of my zipping task, version refers to the version of the zipping library (or somesuch), not the version of the current project. I've tried instead accessing rootproject.version but that's also null - this isn't the root project but a subproject thereof!
How can I access the version field in this non-root project in gradle?
to clarify: if I remove type:zip from my task, version points to the correct value, but without type:zip it doesnt create a zipfile and so isnt very helpful.
In the context of a Zip task, the version property refers to the AbstractArchiveTask.version property, as you have somehow guessed, and not to the current project version property. As you can see, this version property has been deprecated on AbstractArchiveTask class, maybe to avoid such confusion.
To use project version instead, simply use ${project.version} as in following example.
in your subproject build script:
version="1.0.0"
tasks.register('packageDistribution', Zip) {
println("version from AbstractArchiveTask : $version") // => here, version will be null
println("version from project: ${project.version}") // => here, version will be "1.0.0" as expected
archiveFileName = "my-distribution-${project.version}.zip" // => my-distribution-1.0.0.zip as expected
destinationDirectory = layout.buildDirectory.dir('dist')
from layout.projectDirectory.dir("src/dist")
}
I'm trying to remove usages of deprecated stuff from my Gradle scripts.
I noticed archivePath is supposed to be replaced by archiveFile.
What's the "right" way to convert archiveFile to an absolute path?
Here's my Gradle task:
task buildZip(type: Zip){
...
doLast {
// ZipUtil.normaliseZipDates(archivePath.absolutePath)
ZipUtil.normaliseZipDates(archiveFile.get().asFile.absolutePath)
}
normaliseZipDates takes a String, I can't just pass it archiveFile and archiveFile.absolutePath gives "No such property: absolutePath".
At the moment, as shown above, I'm using archiveFile.get().asFile.absolutePath but that seems crazy verbose - what's the correct way to this?
Context: Using Gradle 6.9 at the moment, soon upgrading to to 7.x.
println archiveFile.get().toString() also seems to give full file path -
> Task :buildZip
/Users/xyz/Projects/gradle-scala/build/distributions/gradle-scala-1.0-SNAPSHOT.zip
but I would recommend to stick with archiveFile.get().asFile.absolutePath
I'm trying to use a YAML file as a standard-in to teamcity to pass build parameters. (user will copy the yaml file as stdin) .
which build runner should i use to achieve this. I can find command line as a build runner.but it simply use arguments to a script ? ( i can do the same) but is there any way that i can copy configuration details to teamcity without an argument to a script ?
Jetbrains seems to prefere not do go with yaml. But you can still give XML or Kotlin DSL a try:
You can store project settings in the XML format or in the Kotlin language and define settings programmatically using the Kotlin-based DSL. Kotlin DSL
import jetbrains.buildServer.configs.kotlin.v2019_2.*
import jetbrains.buildServer.configs.kotlin.v2019_2.buildSteps.script
version = "2021.1"
project {
buildType {
id("HelloWorld")
name = "Hello world"
steps {
script {
scriptContent = "echo 'Hello world!'"
}
}
}
}
In an attempt to follow the doc, I added a task like this to my build.gradle file:
task createStartScripts(type: CreateStartScripts) {
applicationName = 'dc-coverage-calculator'
}
I then executed
./gradlew clean installDist
at which point I expected to find a file at build/install/dc-coverage-calculator/bin/dc-coverage-calculator, but no files with dc-coverage-calculator were created anywhere the build folder. Instead, gradle continued to use the default application name, based on the mainClassName.
I also tried removing the hyphens from the app name.
Unfortunately it doesn't work this way. You've added a new task whereas yo need to modify the existing one, which will be done this way:
startScripts {
applicationName = 'dc-coverage-calculator'
}
Grab a demo here.
I want to run multiple soapui projects in Gradle script. The SOAPUI project files are kept is following location:
d:/soapui/projects/path/a.xml, b.xml etc
Will be there any Gradle script that it will enter into the above mentioned location and execute the each project one by one using testrunner.bat
As #RaGe comments you can use the gradle SOAPUI plugin. However if you're looking for a more custom way you can proceed as follows.
You can generate a task on Gradle to execute testrunner to run your SOAPUI projects. Then you can create dynamically one task for each project you've in a directory path, and using .depends you can make that all these dynamic generated tasks are called when you call the specific task.
Your build.gradle could be something like:
// task to execute testrunner
class SoapUITask extends Exec {
String soapUIExecutable = '/SOAPUI_HOME/bin/testrunner.bat'
String soapUIArgs = ''
public SoapUITask(){
super()
this.setExecutable(soapUIExecutable)
}
public void setSoapUIArgs(String soapUIArgs) {
this.args = "$soapUIArgs".trim().split(" ") as List
}
}
// empty task wich has depends to execute the
// ohter tasks
task executeSOAPUI(){
}
// get the path where are your projects
def projectsDir = new File(project.properties['soapuiProjectsPath'])
// create tasks dynamically for each project file
projectsDir.eachFile{ file ->
if(file.name.contains('.xml')){
// create the tasks
task "executeSOAPUI${file.name}"(type: SoapUITask){
println "execute project ${file.name}"
soapUIArgs = ' "' + file.getAbsolutePath() +'"'
}
// make the depends to avoid invoke each task one by one
executeSOAPUI.dependsOn "executeSOAPUI${file.name}"
}
}
To invoke this you can do it using the follow command:
gradle executeSOAPUI -PsoapuiProjectsPath=d:/soapui/projects/path/
Note that -P is used to pass the parameter for projects dir.
Recently I wrote an answer on how to write gradle task to run SOAPUI which can be also util, if you want check more details here.
Hope this helps,