Gradle: How do I run my LIquibase changesets as part of my normal build process? - gradle

I'm using Gradle 2.7 with the Gradle Liquibase plugin v 1.1.1. How do I run my changeSets as part of doing my normal build using
gradle build
? I currently have this in my build.gradle file ...
liquibase {
activities {
main {
File propsFile = new File("${project.rootDir}/src/main/resources/liquibase.properties")
Properties properties = new Properties()
properties.load(new FileInputStream(propsFile))
changeLogFile 'src/main/resources/db.changelog-1.0.xmlll'
url '${url}'
username '${username}'
password '${password}'
}
runList = main
}
}
However when I run the above command ("gradle build"), the above is not run. I know its not run because the username/password are incorrect and I have no change log file ending in ".xmlll". WHat else do I need to add to assure that hte plugin always attempts to execute the changeset?

You need to define a task dependency from build to update.
build.dependsOn update
To get task to run before your tests (assuming you didn't define a new one), you can do
test.dependsOn update
Ref:
Gradle task docs

You can define this in your gradle build file
apply plugin: 'liquibase'
dependencies {
classpath 'org.liquibase:liquibase-gradle-plugin:1.2.0'
}
liquibase {
activities {
main {
changeLogFile 'changelog.xml'
url 'jdbc:h2:db/liquibase_workshop;FILE_LOCK=NO'
username 'sa'
password ''
}
}
And then run this command
gradle update

Related

ORA-01882: timezone region not found in Liquibase Gradle plugin

In our project we are using Liquibase gradle plugin. Recently, we updated ojdbc8 plugin to version 18.3.0.0. Unfortunately, it caused our Liquibase task to fail with ORA-01882: timezone region not found. I found some solutions for this error (e.g. there: ORA-01882: timezone region not found), but I have no idea how I could add this -Duser.timezone or -Doracle.jdbc.timezoneAsRegion property to gradle task. I tried different approaches, but with no success.
This is how some crucial parts of our build.gradle look like:
liquibase {
activities {
oracle {
changeLogFile "$liquibasePath/db.changelog-master.xml"
driver liquibaseProps['oracle.driver']
url "jdbc:oracle:thin:#${liquibaseProps['oracle.ip.port']}:${liquibaseProps['oracle.schema']}"
username liquibaseProps['oracle.username']
password liquibaseProps['oracle.password']
outputDefaultSchema false
outputDefaultCatalog false
}
}
}
def generate(taskName, taskDescription, generateCommand) {
project.task(taskName, type: LiquibaseTask) {
group = 'Liquibase'
description = taskDescription
inputs.property('databases', getRunList())
inputs.dir liquibasePath
outputs.dir sqlScriptsPath
doLast {
new LiquibaseSqlCleanupTask(sqlScriptsPath).execute()
}
}
}
You need to set that as a system property when running gradle. The docs for that are at https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_system_properties, but here is a copy/paste of the most relevant info:
Using the -D command-line option, you can pass a system property to
the JVM which runs Gradle. The -D option of the gradle command has the
same effect as the -D option of the java command.
You can also set system properties in gradle.properties files with the
prefix systemProp.
So you could create a gradle.properties file in the root directory of your project with contents like this:
systemProp.oracle.jdbc.timezoneAsRegion=false

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.

gradle task not recognizing the sonar-project.properties file in project workspace

I have created a sample spring boot project along with sonarqube gradle plugin in it .I have add all the needed properties like login token to sonar-project.properties.But while running gradle task I get exception of authentication
Gradle plugin added
plugins { id "org.sonarqube" version "2.6" }
Screenshot of project structure
I also suffered from this pain and my workaround was to parse the settings file and apply it:
sonarqube {
// this is silly but specifying the actual settings file is not supported
properties {
def props = new Properties()
file("sonar-project.properties").withInputStream { props.load(it) }
props.each {
property("${it.key}", "${it.value}")
}
}
}
Gradle analysis doesn't read sonar-project.properties. Instead, add those values to build.gradle as shown in the docs
sonarqube {
properties {
property "sonar.login", "xxxxxxxx"
}
}

Gradle flyway script to update multiple databases

I have a Gradle script which uses the flyway plugin to create some database tables, and now I need to update the script so that it can work with 2 separate sets of flyway scripts, each of which updates a separate database.
In the single-database version of the script I simply do this:
flyway {
url = 'jdbc:edb://localhost:5432/mydb'
schemas = ['my_schema']
user = 'my_user'
password = 'my_pass'
locations = ['filesystem:src/main/resources/db/flyway/my_db']
}
I've been experimenting with declaring a new task to run the scripts for the second db:
task flywayMigrate2 << {
ext {
flyway {
url = 'jdbc:edb://localhost:5432/mydb2'
schemas = ['my_schema2']
user = 'my_user2'
password = 'my_pass2'
locations = ['filesystem:src/main/resources/db/flyway/my_db2']
}
}
}
flywayMigrate2.finalizedBy flywayMigrate
My Gradle skills are poor, but I know this isn't the right way to do it - my understanding is that the ext block overwrites the original flyway configuration, so if I wanted to run flywayMigrate after flywayMigrate2 it would continue to use the second set of config values rather than reverting to the original set.
I can't be the first person to need to do this, but I'm struggling to find a decent approach, can anyone help?
The flyway { ... } extension object is for configuring properties which are common to all flyway tasks. Each task also has properties which can be configured, my guess is that task level properties override properties configured on the flyway { ... } extension object.
I think you'd just configure two FlywayMigrateTask instances in your build.gradle
import org.flywaydb.gradle.task.*
task migrate1(type: FlywayMigrateTask) {
url = 'jdbc:edb://localhost:5432/mydb1'
user = 'user1'
locations = ['filesystem:src/main/flyway/db1/migration']
// etc
}
task migrate2(type: FlywayMigrateTask) {
url = 'jdbc:edb://localhost:5432/mydb2'
user = 'user2'
locations = ['filesystem:src/main/flyway/db2/migration']
// etc
}
See also AbstractFlywayTask.java
I don't think that you have to define another Gradle task in order to introduce a second Flyway configuration.
According to the documentation you simply can overwrite either using Gradle properties
gradle -Pflyway.user=myUsr -Pflyway.schemas=schema1,schema2 -Pflyway.placeholders.keyABC=valXYZ
or system properties
gradle -Dflyway.user=myUser -Dflyway.schemas=schema1,schema2 -Dflyway.placeholders.keyABC=valueXYZ
or providing an external Flyway config
gradle -Dflyway.configFiles=path/to/myAlternativeConfig.conf flywayMigrate
The Flyway Gradle plugin follows the following configuration order:
System properties
Environment variables
Custom config files
Gradle properties
Flyway configuration section in build.gradle
<user-home>/flyway.conf
Flyway Gradle plugin defaults
Alternatively you could introduce a new task that overwrites the Gradle properties of Flyway.
task migrate2(FlywayMigrateTask) {
project.ext['flyway.user']='myUsr'
project.ext['flyway.password']='mySecretPwd'
project.ext['flyway.schemas']='schema1,schema2,schema3'
project.ext['flyway.placeholders.keyABC']='valueXYZ'
project.ext['flyway.placeholders.otherplaceholder']='value123'
}

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