Nativescript custom gradle task - nativescript

How to run a custom gradle task in a nativescript android project?
Use case:
I want to upload the apk to google app store using the plugin bellow:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.triplet.gradle:play-publisher:1.1.5'
}
}
apply plugin: 'com.github.triplet.play'
play {
serviceAccountEmail = 'bla#user.iam.gserviceaccount.com'
pk12File = file('../../key.p12')
track = 'alpha'
}
The plugin creates publishApkRelease task
How to run that task alone?

Add the snippet in the gradle script located in your platforms/android/build.gradle and then use the gradle wrapper to invoke the task like so ./gradlew publishApkRelease for bash or gradlew publishApkRelease in the Windows CMD.
Note that if you delete the platforms/android dir, or modify it by removing/re-adding platform through the tns CLI the changes in the gradle script will be wiped. Make a local copy of the build script or write a hook that will modify the gradle file if you don't want to lose your changes.

Related

Gradle "application" plugin changing entrypoint script names

I am using the Gradle Application plugin to package an app so it can be run in a Docker container. Locally this all works fine and the only non-default Gradle build statements I use are:
apply plugin: 'application'
// Rest of build file declaring dependencies, etc.
mainClassName = 'com.example.MyApp'
distributions {
main {
baseName = 'my-app'
}
}
This results in a launch script in <app_base>/bin/my-app.sh.
But when I build the app on Jenkins the launch script is bin/CI_my-app_develop i.e. it adds CI_ and the current branch as a suffix.
How can I disable this behaviour?
You can configure a default CreateStartScripts task as follows:
createStartScripts {
applicationName = 'my-app'
}
No need to create a custom task for that.

How to execute gradle task during project import in Intellij Idea

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:

How to use Gretty integrationTestTask with a war file?

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:

Wrong order of task execution in gradle 3.3

I want to define methods inside my script file, and use them to define build tasks for each project individual (custom library).
ext.buildDockerImage = { imageName ->
def distDir = "${getProject().getBuildDir()}/docker"
copy {
from "${project.getProjectDir()}/src/docker/"
into distDir
}
println 'Build docker image'
}
In my project build.gradle I have created a task:
apply plugin: "war"
apply plugin: "jacoco"
dependency {
// all dependencies
}
task buildDocker() {
apply from: "${project.getRootDir()}/docker/scripts.gradle"
buildDockerImage("admin")
}
The problem is that whenever I am running gradle build, this tasks executes also:
$ gradle build -xtest
Build docker image
# rest of build
As you can see , all I want is to create a custom library that will hold methods, used to create tasks for each project. But currently I cannot import those methods without breaking the build. Method buildDockerImage will work only after war file is added to build directory, so this task must be executed on demand only, I don't want to be included in the process all the time.
My questions:
how make my task to only run when I execute task manually
why, when I execute my build, script executes as first?
Your task buildDocker() defines everything in configuration phase. So when you run your gradle build this will always run.
task buildDocker() {
apply from: "${project.getRootDir()}/docker/scripts.gradle"
buildDockerImage("admin")
}
If you want to run this task as a standalone task, define your stuff in execution phase of the task. something like below
task buildDocker() {
apply from: "${project.getRootDir()}/docker/scripts.gradle"
doLast{
buildDockerImage("admin")
}
}
Read This article
This might help

How to add clean task - Task 'clean' not found

I am using https://github.com/eriwen/gradle-js-plugin and i would like to be able run task 'clean'. When i run 'gradle -d clean', it gives the following error
Task 'clean' not found in root project
To my understanding, gradle comes with task - 'clean', however the gradles-js-plugin doesn't seem to support that at this time or something. How do i add the task 'clean'?
Here is my build.gradle:
// 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'
def jsSrcDir = 'public/js'
javascript.source {
dev {
js {
srcDir jsSrcDir
include "*.js"
exclude "*.min.js"
}
}
prod {
js {
srcDir jsSrcDir
include "*.min.js"
}
}
}
combineJs{
source = fileTree(javascript.source.dev.js.files)
dest = file("${buildDir}/all.js")
}
The clean task is introduced by the base plugin. So you need to apply this plugin to get the clean task and the clean task rules for cleaning up specific task outputs:
apply plugin:'base'
Make sure you are running the
gradle -d clean
command from your project home path.
According to this guideline, the syntax is as follows:
plugins {
id "base"
}
Just run it from your /android/ folder, i.e navigate to your android folder and run the command from there in the project dir.
you can also add
plugins {
id "base"
}
to your gradle file
On android I added settings.gradle file with the a single line
include ':library'

Resources