project is Gradle based
I have this command
apollo schema:download --endpoint=https://*******************************/graphql schema.json
this downloads json file from that endpoint then I put that file under the schema(name) package, manually,
this is what I tried
I worked this command and put it under the package
is there any way to do this with scripts so this is automatically done?
I tried this
task<Exec>("apollo") {
commandLine("exec", "apollo schema:download --endpoint=https://*************************/graphql schema.json")
}
I want to do ./gradlew build and during build also run this apollo command
You can make your build task depend on another task like this:
task downloadStuff {
logger.error('Downloading dependency.')
logger.error('Moving dependency somewhere or whatever.')
}
build {
dependsOn downloadStuff
}
Make sure to add it to the root project build.gradle file if you have multiple and it should also be top level.
Related
I am using gradle as the build tool for a terraform project. I do have unit tests written in go for the project under the ..test/... folder . The way I am running test locally is just on the commandline go test ..test/..' which will run all tests under the test folder. I want to integrate this in the build , so that every build will run this command 'go test ..test/..', How do I achieve this in gradle. Can a custom task be utilized to run a go command?
I am trying to do something like the following
task testExec(type: Exec) {
workingDir "${buildDir}/test"
commandLine 'go','test'
} doLast {
println "Test Executed!"
}
But I get the error
> A problem occurred starting process 'command 'go''
For what its worth , I tried other commands and get the same erorr for ex
task testExec(type: Exec) {
workingDir "${buildDir}/test"
commandLine 'echo','${}buildDir'
} doLast {
println "Test Executed!"
}
gives similar error
> A problem occurred starting process 'command 'echo''
You can use the gradle plugin. First you can follow the starting guide and add the plugin:
plugins {
id 'com.github.blindpirate.gogradle' version '0.11.4'
}
golang {
packagePath = 'github.com/your/package' // go import path of project to be built, NOT local file system path!
}
Then you can run the following command to execute all go files that follow the file name convention <name>_test.go:
gradlew goTest
Otherwise you can also create a complete custom task or a custom task with the plugin.
EDIT:
I found the cause of your error. The variable buildDir refers to the build folder in your project: <project_folder>/build. The problem now is that the folder test does not exists and the exception is thrown. Instead, you can use the variable projectDir.
I have two projects in a multi project build. One looks like this:
// project-a build.gradle
task generateJar(type: Exec) {
commandLine "command", "to", "generate", "jar"
}
task generateArtifact(type: Zip, dependsOn: generateJar) {
outputs.file("/path/to/generated/jar")
}
artifacts { archives generateArtifact }
And the other simply depends on it like so:
// project-b build.gradle
dependencies {
implementation project(':project-a')
}
My expectation is that when I run a build on project b, it will first try to run generateArtifact + generateJar from project a, then get the outputted jar for use in project b, however I just get a "package does not exist" error in :project-b:compileJava. Based on the output, it looks like it is not trying to run any tasks in project-a, almost like it doesn't recognize the dependency exists
It looks like you're mixing up project and task dependencies. While you correctly made project-b depend on project-a, that does not automatically mean your custom generateJar and generateArtifact tasks get executed. You still need to mark your JAR in project-a as an "outgoing" artifact or add it to the output of the main source set which you can depend on in the consuming project like so:
sourceSets {
main {
output.file(generateArtifact.outputFile, builtBy: generateArtifact)
}
}
With 'compile' split into 'implementation' and 'api' configuration the behaviour might have changed. Can you just try with 'compile'(its deprecated though) once, to validate this. If it works then it means as suggsted by #sschuberth you may have to handle to explicitly call the generateArtifact of :projecta when you build :projectb.
Let's assume my build.gradle file contains task generateSources which as name suggests generates additional java files. It's easy to ensure that generateSources is executed before compileJava: compileJava.dependsOn generateSources. How can I make sure generateSources is called when importing project into Intellij Idea as well?
To elaborate on #vladimir-sitnikov's answer: I added the idea-ext-plugin to my root project:
apply plugin: 'org.jetbrains.gradle.plugin.idea-ext'
// ...
buildscript {
dependencies {
classpath "org.jetbrains.gradle.plugin.idea-ext:org.jetbrains.gradle.plugin.idea-ext.gradle.plugin:0.7"
}
}
Because without that I wasn't able to use it in my sub project, but now it works like this:
idea.project.settings.taskTriggers {
beforeSync tasks.getByName("generateSources")
}
Adding the plugin to the sub-project only didn't do it.
Note: The plugin's documentation is kind of limited, but in "DSL spec v. 0.2" is stated
beforeSync - before each Gradle project sync. Will NOT be executed on initial import
Didn't try that, but it works with existing projects.
This can be done via id("org.jetbrains.gradle.plugin.idea-ext") plugin (https://github.com/JetBrains/gradle-idea-ext-plugin).
See sample code in Gradle sources: https://github.com/gradle/gradle/blob/135fb4751faf2736c231636e8a2a92d47706a3b9/buildSrc/subprojects/ide/src/main/kotlin/org/gradle/gradlebuild/ide/IdePlugin.kt#L147
You can set the task in Gradle tool window: Execute Before Sync:
I want to have this code snippet
test {
testLogging.showStandardStreams = true
}
Shared for all my gradle projects. Is that possible? Preferrably something I add to ~/.gradle/common.gradle or similar.
Probably the best way to inject build logic into existing build scripts without touching them is using init scripts. So you can create a script like testlogging.gradle that looks like this:
allprojects {
tasks.withType(Test) {
testLogging.showStandardStreams = true
}
}
As you can see I use tasks.withType(Test) instead of test here to reference the test task by type. That has some benefits:
this script works also for builds with no task with name test. This could likely happen (e.g. in multiproject builds)
this script would also apply for any other tasks in your build that are of type Test. Some projects use integTest etc.
To auto apply this script on your machine, you can put it in the folder ~/.gradle/init.d. Gradle considers every .gradle file in there as init script and applies them to each build.
To learn more details about init scripts check the according chapter in the gradle userguide.
Is it possible to use gretty integrationTestTask with a project that uses a war folder?
It seems from the documentation appBeforeIntegrationTest does not have access to the war. Is there another way to run test cases so that it uses the war folder?
Ideally, I want jettyStart -> test -> jettyStop to run. Although when I run it straight jettyStart hangs indefinitely, until jettyStop is run. Is there a way to run jettyStart in Gradle in the background or something?
Regardless what file structure your application has, the integrationTestTask is supposed to be configured with the name of an exsiting gradle task to execute when gradle integrationTest is run:
gretty {
// ...
integrationTestTask = 'integrationTest' // name of existing gradle task
// ...
}
What you want to archive is this:
gretty {
integrationTestTask = 'test'
}
Gretty's workflow when calling integrationTest is as follows: