I am trying read a project property from command line interface. There is a task gradle properties, which prints all of them. Therefore, I can write: gradle properties | grep "rootProject: root project" | awk '{print $NF}' | tr -d "'" and get what I want. The returned result is correct. I would like to use a proper gradle command to achieve the same result. What is a gradle command to get the project name?
this is my build.gradle:
plugins {
id 'java'
}
tasks.register("rootProjectName") {
doLast {
println(rootProject.name)
}
}
build {
println 'hi'
}
Looks like you're just after the rootProject.name property. There is no built-in Gradle task that will give you that property. You can write a simple task that prints that value to the console which will simplify your command.
tasks.register("rootProjectName") {
doLast {
println(rootProject.name)
}
}
Then simply call that task with -q:
$ ./gradlew rootProjectName -q
demo
You can see demo is simply printed out for this example.
So far the closest I got is the following build.gradle:
plugins {
id 'java'
}
task printRootProjectName {
doLast {
println('root project: ' + rootProject.name)
}
}
build {
println 'unwanted output'
}
and then:
gradle printRootProjectName -q | grep "root project:" | awk '{print $NF}' | head -n 1
Related
I have the next project structure.
\rootProj
|
+--\moduleA
| |
| +--\build
| |--build.gradle
| ...
|
+--\moduleB
|
+--\main
| |
| +--\resources
| ...
|
|-build.gradle
...
I'm searching an approach to put everything from moduleA\build to moduleB\main\resources.
Could someone help me with this task?
I've googled about it but found nothing - I'm new in Gradle and it may be a cause I can't ask in the right way.
Thanks in advance!
As an answer to my question I've made the next solution:
buildscript{
ext{
generatedResOutDir = file("$buildDir/generated-resources")
}
}
sourceSets {
main{
output.dir(generatedResOutDir, builtBy: 'copyRes')
}
}
task copyRes(type: Copy){
//for to be certain a directory
//we are going to copy from
dependsOn ':moduleA:build'
from project(':moduleA').buildDir
into '${generatedResOutDir}/static'
}
This solution is inspiried by Gradle DSL SourceSetOutput doc.
In $HOME/.gradle/init.gradle I have:
gradle.buildFinished { buildResult ->
commandLine 'bash', "blah blah blah"
}
Doing gradle build the build succeeds, but after it succeeds I get the error:
Could not find method commandLine() for arguments [bash, blah blah blah] on build 'FooBar' of type org.gradle.invocation.DefaultGradle.
The answer to the question Could not find method commandLine() doesn't help as either putting (type: Exec) after gradle.buildFinished or wrapping the whole thing in exec { } causes Gradle to fail right from the start rather than the build succeeding and then my post-build hook failing.
I'm using Gradle version 6.3
You've misconfigured commandLine. With the following build.gradle:
task lol {
doLast {
println "lol"
}
}
and ~/.gradle/init.gradle:
gradle.buildFinished { buildResult ->
exec {
commandLine 'bash', '-c', 'echo lol2'
}
}
it works as expected.
I want to monitor a kubernetes pod created after redeploy task and once it is completed, I want to check the liquibase logs. If it is successful, I want to delete the job. How can I achieve this in gradle? I don't want to undeploy immediately after redeploy. So doLast is not an option. The following code doesn't keeps printing ob has not completed yet
task undeployAfterCompleted() {
group "kubernetes"
description "Undeploy the liquibase update job after completion"
def output = new ByteArrayOutputStream()
commandLine "bash", "-c", "kubectl get po -n pbr | grep 'liquibase' | awk '{ print \$3 }'"
while(!output.toString().equals('Completed')) {
sleep(5 * 1000)
println "Job has not completed yet."
commandLine "bash", "-c", "kubectl get po -n pbr | grep 'liquibase' | awk '{ print \$3 }'"
}
tasks.undeploy.execute()
}
I added a doLast block to achieve this:
doLast {
String status = getStatus()
println status.length()
while (status != "Completed") {
println "not"
sleep(5 * 1000)
if (getStatus() == "Completed") {
println "did"
break
}
}
tasks.undeploy.execute()
}
I have a Gradle project that I want to import to Versioneye to check if my dependencies are up to date, but it's a complex config file (with external variables etc.) and Versioneye does not manage to handle the dependencies properly.
I don't want to install the Versioneye gradle plugin.
How can I export the dependencies from my repo to Versioneye?
You can list all the dependencies gradle app:dependencies.
With a bit of string manipulation, you can export a "clean" dependencies file and manually upload it to Versioneye.
#!/bin/bash
OUT_DIR='versioneye'
OUT_FILE="${OUT_DIR}/build.gradle"
mkdir -p "${OUT_DIR}"
touch "${OUT_FILE}"
# copy your maven repositories closure below from build.gradle
tee "${OUT_FILE}" <<EOF >/dev/null
allprojects {
repositories {
maven {
url 'https://maven.google.com/maven-google-remote'
}
maven {
url "https://jitpack.io"
}
}
}
EOF
echo 'dependencies {' >> "${OUT_FILE}"
./gradlew app:dependencies | grep '^+---' | sed 's|+--- |compile "|' | sed 's| (\*)||g' | sed 's|$|"|' | sort -u >> "${OUT_FILE}"
echo '}' >> "${OUT_FILE}"
cat "${OUT_FILE}"
cd "${OUT_DIR}"
start .
cd -
echo 'Now, open versioneye.com and manually upload the genreated build.gradle file.'
This will generate a file that looks like this:
allprojects {
repositories {
maven {
url 'https://maven.google.com/maven-google-remote'
}
maven {
url "https://jitpack.io"
}
...
}
}
dependencies {
compile "com.android.support.test.espresso:espresso-contrib:2.2.2"
compile "com.android.support.test.espresso:espresso-core:2.2.2"
compile "com.android.support.test.espresso:espresso-intents:2.2.2"
compile "com.facebook.android:facebook-android-sdk:4.17.0"
compile "com.facebook.fresco:fresco:1.5.0"
compile "com.facebook.fresco:imagepipeline-okhttp3:1.5.0"
...
}
This file can be imported to Versioneye with a file upload and will be processed correctly.
I know that Gradle has the excellent dependencies task that lists out all dependencies for a project. However, it returns them in a tree listing.
I would like to get a list of all my dependencies as they are resolved in just a flat list. Similar to how the Maven dependency plugin list goal behaves.
Here is a short task that meets that need:
task('dependenciesList') << {
println "Compile dependencies"
def selectedDeps = project.configurations.compile.incoming.resolutionResult.allDependencies.collect { dep ->
"${dep.selected}"
}
selectedDeps.unique().sort().each { println it}
}
The third line is the interesting part. You need to get the configuration you care about (compile) then instead of getting dependencies there, the incoming.resolutionResult will provide the resolved values and versions.
<< was removed in Gradle 5. To make the task work in Gradle 5 and later versions, remove << and use doLast { } instead. Also, use runtimeClasspath or compileClasspath for the configuration instead of compile:
task('dependenciesList') {
doLast {
println "Compile dependencies"
def selectedDeps = project.configurations.compileClasspath.incoming.resolutionResult.allDependencies.collect { dep ->
"${dep.selected}"
}
selectedDeps.unique().sort().each { println it}
}
}
Without modifying the build, flatten the tree using sed, sort, and uniq as follows:
$ gradle dependencies | sed 's/^.* //' | sort | uniq
Alternatively, with slightly tighter sed matching:
./gradlew dependencies \
| sed -n 's/.*--- \([^ ]*\).*/\1/p' \
| grep -v "^project$" \
| sort \
| uniq
Thanks for the answers already supplied.
Finally I complete it by a more standard way:
project.gradle.addListener(new DependencyResolutionListener() {
#Override
void beforeResolve(ResolvableDependencies dependencies) {}
#Override
void afterResolve(ResolvableDependencies dependencies) {
dependencies.resolutionResult.allComponents.each { select ->
println "selected component: ${select} " + select.selectionReason
}
}
})
Project implementation will also be resolved in this way, and the final selected component version will be resolved correctly.