Customize palantir docker gradle plugin - spring-boot

I need to migrate from Transmode gradle docker plugin to Palantir docker gradle plugin.
Old task is as below,
task buildDocker(type: Docker, dependsOn: build) {
group "docker"
push = false
applicationName = awsContainerRepositoryName()
dockerfile = pickDockerfile()
tagVersion = "local"
doFirst {
copy {
from jar
into stageDir
rename { String fileName ->
fileName.replace("${jar.baseName}-${project.version}", "app")
}
}
new File(stageDir, "service_version.txt").text = "${project.version}"
copy {
from "${buildDir}/asciidoc/html5/index.html"
into "${stageDir}/api-documentation/"
}
if (project.hasProperty('customDockerRunStep')) {
runCommand project.customDockerRunStep
}
exec {
commandLine 'bash', 'shared-build/aws/retrieve_aws_ecr_login.sh'
}
}
}
I am trying to add doFirst block as below,
docker {
dependsOn assemble
name awsContainerRepositoryName()
dockerfile file(pickDockerfile())
files bootJar.archivePath
doFirst {
// some code
}
}
But it is giving below error,
* What went wrong:
A problem occurred evaluating root project 'service-abc'.
> No signature of method: build_dgdrt7rh9kp8lojc44jud6em6.docker() is applicable for argument types: (build_dgdrt7rh9kp8lojc44jud6em6$_run_closure7) values: [build_dgdrt7rh9kp8lojc44jud6em6$_run_closure7#8431bf9]
Please help

Related

gradle tasks -all throwing error

I have test build.gradle file as follows
task someTask(type: Sync) {
def folder = new File('fold1/fold2/');
if(!folder.exists()) {
throw new GradleException('Folder Absent');
}
else {
}
}
When I do gradle tasks --all it is running the task and throwing exception. I was thinking that only when this task is run that it will check for folder but it is actually running it for any task I run.
Can someone suggest workaround for this?
Thanks in advance.
Your code is executed during the configuration phase and not during the execution phase. You need to put it in a doFirst or doLast block:
task someTask(type: Sync) {
doLast {
def folder = new File('fold1/fold2/');
if (!folder.exists()) {
throw new GradleException('Folder Absent');
}
else {
}
}
}
See also: Why is my Gradle task always running?

Connect war task to cargo deploy task

I've create a gradle script in order to deploy a deployable on a container using cargo plugin:
class RemoteContainer {
String name
String container
String hostname
Integer port
String username
String password
String purpose
}
def remoteContainers = [new RemoteContainer(
name: 'wildfly10',
container: 'wildfly10x',
hostname: 'localhost',
port: 9990,
username: 'user',
password: 'passwd',
purpose: 'development'
)
]
remoteContainers.each { config ->
task "deployDev${config.name.capitalize()}"(type: com.bmuschko.gradle.cargo.tasks.remote.CargoDeployRemote) {
description = "Deploys WAR to remote Web Application Server: '${config.name}'."
containerId = config.container
hostname = config.hostname
port = config.port
username = config.username
password = config.password
dependsOn war
}
task "undeployDev${config.name.capitalize()}"(type: com.bmuschko.gradle.cargo.tasks.remote.CargoUndeployRemote) {
description = "Deploys WAR to remote Web Application Server: '${config.name}'."
containerId = config.container
hostname = config.hostname
port = config.port
username = config.username
password = config.password
dependsOn = war
}
}
Nevertheless, I've created several tasks in order to create custom war files according to its scope:
task createQAWar(type: War, dependsOn: classes) {
archiveName "webapi-demo-${versioning.info.display}.war"
destinationDir = file("$buildDir/dist")
webInf {
...
}
}
task createDevelopmentWar(type: War, dependsOn: classes) {
archiveName "webapi-dev-${versioning.info.display}.war"
destinationDir = file("$buildDir/dist")
webInf {
...
}
}
task createTestingWar(type: War, dependsOn: classes) {
archiveName "webapi-test-${versioning.info.display}.war"
destinationDir = file("$buildDir/dist")
webInf {
...
}
}
task createProductionWar(type: War, dependsOn: classes) {
archiveName "webapi-prod-${versioning.info.display}.war"
destinationDir = file("$buildDir/dist")
webInf {
...
}
}
I'd like to link that deployDev tasks pick the war artifact generated on createDevelopmentWar.
I've tried to set dependsOn property to createDevelopmentWar:
remoteContainers.each { config ->
task "deployDev${config.name.capitalize()}"(type: com.bmuschko.gradle.cargo.tasks.remote.CargoDeployRemote) {
description = "Deploys WAR to remote Web Application Server: '${config.name}'."
containerId = config.container
hostname = config.hostname
port = config.port
username = config.username
password = config.password
dependsOn = createDevelopmentWar <<<<<<<<<<<<<<<<<
}
}
Nevertheless, gradle it's getting me this message:
What went wrong:
A problem occurred evaluating root project 'webapi'.
Cannot cast object 'task ':createDevelopmentWar'' with class 'org.gradle.api.tasks.bundling.War_Decorated' to class 'java.lang.Iterable
'
I've also tried setting dependsOn to war task, nevertheless the message is the same.
EDIT
Once, I've changed the syntax for dependsOn = [createDevelopmentWar], I'm facing up with another trouble:
It's getting me this message:
Execution failed for task ':deployDevWildfly10'.
Deployable D:\projects\living\platform\webapi\build\libs\webapi-dev-89c157a-dirty.war does not exist
It's trying to get the artifact from build\libs\. Nevertheless, the war artifact has been created in destinationDir = file("$buildDir/dist"):
task createDevelopmentWar(type: War, dependsOn: classes) {
archiveName "webapi-dev-${versioning.info.display}.war"
destinationDir = file("$buildDir/dist")
webInf {
...
}
}
How could I set cargo picks the artifact up from previously war type task(createDevelopmentWar) execution information?
dependsOn = createDevelopmentWar results in a call to setDependsOn(createDevelopmentWar) which expects an Iterable that a task is not.
dependsOn createDevelopmentWar would result in a call to dependsOn(createDevelopmentWar) which expects a varargs parameter and thus sould add the task to the dependencies.
If you really want to replace all dependencies with this one dependency, you have to do it like dependsOn = [createDevelopmentWar].

How to apply javaagent to gretty plugin based on gradle command line?

The question is specific, but it's more of a general 'how to do this in gradle' question.
I have a demo java web app that I can run using the gretty plugin. I would like to selectively control whether a javaagent is applied to the jvmArgs of the gretty process based on a command line flag. The agent jar location is known by getting its path from a dummy configuration:
configurations {
agent
}
dependencies {
...
agent group: 'com.foo', name: 'foo-agent', version: '1.0'
}
I know I can access the jar file location using something like:
project.configurations.agent.find { it.name.startsWith("foo-agent") }
How can I selectively apply that to the gretty jvmArgs configuration based on a command line property such as
gradle -PenableAgent
I ended up solving this by creating a task and simply calling it before I run the war:
task agent {
doFirst {
def agentJar = project.configurations.agent.find { it.name.startsWith("foo-agent") }
gretty.jvmArgs << "-javaagent:" + agentJar
}
}
Then I can simply call:
gradle agent appRunWar
In my project I use Spring Instrument as java agent so this was my solution.
You can make appRun task dependent on agent task then no additional gradle run parameter needed.
dependencies {
...
agent 'org.springframework:spring-instrument:4.2.4.RELEASE'
}
configurations {
dev
agent
}
gretty {
...
contextPath = '/'
jvmArgs=[]
springBoot = true
...
}
task agent {
doFirst {
def agentJar = project.configurations.agent.find{it.name.contains("spring-instrument") }
gretty.jvmArgs << "-javaagent:" + agentJar
}
}
project.afterEvaluate {
tasks.appRun.dependsOn agent
}

How to run a Gradle task after apks are produced in Android Studio?

The following task (in build.gradle of an app's module) seems to run always before the apk is produced:
android.applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
def releaseBuildTask = tasks.create(name: "debug") {
println(".................... test ..............................")
}
releaseBuildTask.mustRunAfter variant.assemble
}
}
Could anyone offer a tip on how to run a task after the apks are produced?
Android tasks are typically created in the "afterEvaluate" phase.
Starting from gradle 2.2, those tasks also include "assembleDebug" and
"assembleRelease". To access such tasks, the user will need to use an
afterEvaluate closure:
afterEvaluate {
assembleDebug.dependsOn someTask
}
source: https://code.google.com/p/android/issues/detail?id=219732#c32
try add this in you app/build.gradle
assembleDebug.doLast {
android.applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
def releaseBuildTask = tasks.create(name: "debug") {
println(".................... test ..............................")
}
releaseBuildTask.mustRunAfter variant.assemble
}
}
println "build finished"
}
invoke the build command and specify the task assembleDebug
./gradlew assembleDebug
I found a solution that works, to copy the release APK into the project root automatically on build completion.
android {
...
task copyReleaseApk(type: Copy) {
from 'build/outputs/apk'
into '..' // Into the project root, one level above the app folder
include '**/*release.apk'
}
afterEvaluate {
packageRelease.finalizedBy(copyReleaseApk)
}
}

(No such file or directory) how to make non existant file to be ignored by gradle

when i run the command 'gradle tasks' or anything for that fact, i got the following error:
Caused by: java.io.FileNotFoundException: /Users/maxit/workspace/Backbone/modules/contact-form/public/build/contact-src.js (No such file or directory)
Here is my gradle build file:
configurations {
sshAntTask
}
dependencies {
sshAntTask 'org.apache.ant:ant-jsch:1.7.1', 'jsch:jsch:0.1.29'
}
// Pull the plugin from Maven Central
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.eriwen:gradle-js-plugin:1.5.0'
}
}
// Invoke the plugin
apply plugin: 'js'
apply plugin:'base'
def appName = "some"
def version = "0.0.1"
def jsSrcDir = 'public/js'
javascript.source {
dev {
js {
srcDir jsSrcDir
include "*.js"
exclude "*.min.js"
}
}
prod {
js {
srcDir jsSrcDir
include "*.min.js"
}
}
}
task combineSrc(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
source = ["${projectDir}/public/templates/templates.js","${projectDir}/public/js/models/contact_model.js", "${projectDir}/public/js/views/contact_form_view.js", "${projectDir}/public/js/app.js" ]
dest = file("${projectDir}/public/build/${appName}-src.js")
}
task appendJQuery(dependsOn: 'combineSrc') {
String backboneSrc = file(new File("${projectDir}/public/build/${appName}-src.js")).text
new File("${projectDir}/public/build/${appName}-jqueryWraped.js").withWriter{ out ->
out << "(function(\$){" + file("${projectDir}/public/build/${appName}-src.js").text + "})(jQuery); \n"
}
}
It appears, that gradle doesn't ignore a file that is none existant. The task 'combineSrc' hasn't been run, yet to create the file....and i am unable to run the task 'cobineSrc' to create the file in the first place. Its kind a dead end. what am i doing wrong and how to make this work? Thank you
The reason you're failing is, that all the stuff you currently doing during the configuration of the appendJQuery task should be done in the execution phase.
just refactor your appendJQuery task to do:
task appendJQuery(dependsOn: 'combineSrc') {
doLast{
String backboneSrc = file(new File("${projectDir}/public/build/${appName}-src.js")).text
new File("${projectDir}/public/build/${appName}-jqueryWraped.js").withWriter{ out ->
out << "(function(\$){" + file("${projectDir}/public/build/${appName}-src.js").text + "})(jQuery); \n"
}
}
}
hope that helps!
René

Resources