I am trying to load properties and use it in liquibase task. I am able to load the properties and able to print it out. But when I use it in liquibase task, gradle complains that properties are not found.
I am using:
classpath 'org.liquibase:liquibase-gradle-plugin:1.1.0'
Loading properties:
def springApplicationProperties = "src/main/resources/application-${System.env.SPRING_PROFILES_ACTIVE}.properties"
def springProps = new Properties()
InputStream is = new FileInputStream("${springApplicationProperties}")
springProps.load(is)
println "${springProps['spring.datasource.url']}"
Liquibase block:
liquibase{
activities {
main {
changeLogFile 'src/main/database/changelog.groovy'
url ${springProps['spring.datasource.url']}
username ${springProps['spring.datasource.user']}
password ${springProps['spring.datasource.password']}
}
}
runList = 'main'
}
And I am getting
No such property: $springProps for class:
org.liquibase.gradle.Activity
Looks like springProps is not visible in the liquibase activity. I tried to use ext and project scope, both not visible in liquibase block.
It will work this way:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.liquibase:liquibase-gradle-plugin:1.1.0'
}
}
apply plugin: 'java'
apply plugin: 'org.liquibase.gradle'
ext.springProps = new Properties()
InputStream is = new FileInputStream("src/main/resources/lol.properties")
springProps.load(is)
liquibase {
activities {
main {
url springProps['spring.datasource.url']
username springProps['spring.datasource.user']
password springProps['spring.datasource.password']
}
}
runList = 'main'
}
You unnecessarily added ${}. I've changed script a bit while trying out myself - but it has no side effects.
Related
I use liquibase in my project, and here is one of my config files:
databaseChangeLog:
- changeSet:
id: 123
author: m.rybalkin
changes:
- update:
columns:
- column:
name: code
value: '123'
schemaName: prod
tableName: config_bundle
where: code='321'
Here is my build.gradle of the "liquibase" module:
group 'com.project.test'
version '0.1.0'
buildscript {
dependencies {
classpath "org.liquibase:liquibase-gradle-plugin:${liqubasePluginVersion}"
classpath "gradle.plugin.com.avast.gradle:gradle-docker-compose-plugin:${gradleDockerComposePluginVersion}"
}
}
apply plugin: 'java'
apply plugin: 'org.liquibase.gradle'
apply plugin: 'com.avast.gradle.docker-compose'
dependencies {
liquibaseRuntime "org.liquibase:liquibase-core:${liquibaseCoreVersion}"
liquibaseRuntime "org.postgresql:postgresql:${postgresqlVersion}"
}
liquibase {
activities {
main {
def file = new File("${projectDir}/liquibase.properties")
if (file.exists()) {
def props = new Properties()
InputStream is = new FileInputStream(file)
props.load(is)
is.close()
changeLogFile props['changeLogFile']
outputFile 'liquibase/sql-migration-bundle.sql'
url props['url']
username props['username']
password props['password']
} else {
println "Add ${projectDir}/liquibase.properties if you want use liquibase plugin"
}
}
dockerPostgres {
changeLogFile "${projectDir}/changelog/changelog-master.yml"
url 'jdbc:postgresql://localhost:5555/test'
username 'test'
password 'test'
}
runList = 'main'
}
}
task localUpdate(dependsOn: "composeUp") {
doFirst {
liquibase.setProperty("runList", "dockerPostgres")
}
finalizedBy update
}
task localDropAll(dependsOn: "composeUp") {
doFirst {
liquibase.setProperty("runList", "dockerPostgres")
}
finalizedBy dropAll
}
I have two different names of my schema, a "prod" for production and a "test" for tests.
Is it possible to set a variable in my application.yml or build.gradle for changing the name when I'm testing my app and when I'm deploying it?
P.S. I also have two different profiles of my Spring app - "prod" and "test"
You certainly can add properties at runtime of liquibase (which can be passed in from gradle, directly from commandline, etc).
So you can for example call liquibase on the CLI:
liquibase -Durl= update
I have a Gradle 6.0.1 project. The build.gradle (excerpt) looks like:
plugins {
id "application"
id "com.github.edeandrea.xjc-generation"
id "eclipse"
id "idea"
id "java"
id "org.springframework.boot"
}
...
dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:${property("spring-boot.version")}")
// ...more stuff here
}
// ...more stuff here
I'm managing all the plugin versions in settings.gradle:
pluginManagement {
plugins {
id "application"
id "com.github.edeandrea.xjc-generation" version "1.0"
id "eclipse"
id "idea"
id "java"
id "org.springframework.boot" version "${spring-boot.version}"
}
}
rootProject.name = "spring-core"
...and I usually put the artifact versions in gradle.properties:
#
# Dependency Versions
oracle.version = 18.3.0.0
spring-boot.version = 2.2.1.RELEASE
#
# Gradle Settings
org.gradle.configureondemand = false
org.gradle.daemon = false
#
# System Settings
systemProp.file.encoding = UTF-8
systemProp.sun.jnu.encoding = UTF-8
Now the problem is I can't read dot-properties in settings.gradle (from gradle.properties) in the same way I do it inside build.gradle — I already tried using ${property("spring-boot.version")}.
Is there any way to achieve that? I can easily change the key to something like springBootVersion and it works, but I wonder if there is a way to have in the way I currently have it: spring-boot.version.
use getProperty("spring-boot.version")
simple gradle project with additional variants
task test{
doLast {
//success
println project.property('aaa.bbb.ccc')
println project.'aaa.bbb.ccc'
println getProperty('aaa.bbb.ccc')
//failure: Could not get unknown property 'aaa.bbb.ccc' for task ':test' of type org.gradle.api.DefaultTask
println property('aaa.bbb.ccc')
}
}
gradle.properties
aaa.bbb.ccc=12345
property('aaa.bbb.ccc') fails because it tries to get property on current object (task) but aaa.bbb.ccc defined for project
however project.property('aaa.bbb.ccc') succeeds because it should
project.'aaa.bbb.ccc' is the same as project.getProperty('aaa.bbb.ccc') in groovy
project.getProperty('aaa.bbb.ccc') works because of groovy basic object GroovyObject (IHMO)
and getProperty(name) without prefix actualy located in org.gradle.groovy.scripts.BasicScript and not really documented...
For me the solution from #daggett doesn't work. But I was able to access a property defined inside the ~/.gradle/gradle.properties from the settings.gradle this way:
settings.ext.find('MY_PROPERTY')
To provide some context, I'm using this to read the credentials for the Gradle Build Cache Node remote server:
buildCache {
boolean isCiServer = System.getenv().containsKey("CI")
remote(HttpBuildCache) {
enabled = settings.ext.find('GRADLE_BUILD_CACHE_NODE_PWD') != null
url = 'https://myserver:443/cache/'
allowUntrustedServer = true
push = isCiServer
credentials {
username = settings.ext.find('GRADLE_BUILD_CACHE_NODE_USR')
password = settings.ext.find('GRADLE_BUILD_CACHE_NODE_PWD')
}
}
}
EDIT
Another solution that does not require the use of ext:
username = settings.hasProperty('GRADLE_BUILD_CACHE_NODE_USR') ? settings.GRADLE_BUILD_CACHE_NODE_USR : null
I am trying to integrate Liquibase in my project using Gradle.
For that I made below changes to build.gradle but it gives me error as follows:
Below is my build.gradle file:
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.liquibase:liquibase-gradle-plugin:2.0.1"
}
}
allprojects {'
apply plugin: 'liquibase'
}
dependencies {
liquibaseRuntime 'org.liquibase:liquibase-core:3.6.1'
liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.1'
liquibaseRuntime 'mysql:mysql-connector-java:5.1.34'
}
liquibase {
activities {
doFirst {
if (!project.hasProperty('runList')) {
project.ext.runList = "main,test"
}
}
main {
defaultsFile "$projectDir/sql-migration/mysql/app_mysql.properties"
logFile "$projectDir/sql-migration/mysql/logs/liquibase-" + new Date().format("yyyy-MM-dd_HH-mm-ss")+".log"
}
test {
defaultsFile "$projectDir/sql-migration/mysql/app_test_mysql.properties"
logFile "$projectDir/sql-migration/logs/liquibase-test-" + new Date().format("yyyy-MM-dd_HH-mm-ss")+".log"
}
}
// To execute liquibase on single environment, gradlew app:update -PrunList=test
runList = project.ext.runList
}
I am not able to get why it is not able to find out 'liquibaseRuntime' repositories.
Any help in this would be really appreciated.
Assuming that you have a changelog file, make sure that the changelog file is properly referenced from build.gradle.
Also I would suggest defining the liquibase.url, username and password.
Refer to this post for detailed implementation:
https://dzone.com/articles/managing-your-database-with-liquibase-and-gradle
The error message
Cannot resolve external dependency [...] because no repositories are defined
indicates that you're missing a repositories block. You have one in your buildscript, but you probably need to move it to the top-level, as shown here.
Here's the Error:
FAILURE: Build failed with an exception.
Where: Build file '/home/wieland/GitGradlePackaging/build.gradle' line: 22
What went wrong: A problem occurred evaluating root project 'GitGradlePackaging'.
Could not get unknown property 'org' for object of type org.gradle.api.internal.initialization.DefaultScriptHandler.
And Here's my build.gradle File:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/4.6/userguide/tutorial_java_projects.html
*/
//From example: http://mrhaki.blogspot.co.at/2015/04/gradle-goodness-use-git-commit-id-in.html
buildscript {
repositories {
jcenter()
}
dependencies {
//Add dependencies for build script, so we can access Git from our build script
classpath 'org.ajoberstar:grgit:1.1.0'
}
def git = org.ajoberstar.grgit.Grgit.open(file('.'))
//To save Githash
def githash = git.head().abbreviatedId
}
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building an application
id 'application'
// Apply the groovy plugin to also add support for Groovy (needed for Spock)
id 'groovy'
id 'distribution'
}
// Set version
project.version = mainProjectVersion + " - " + githash
project.ext.set("wholeVersion", "$project.version - $githash")
project.ext.set("buildtimestamp", "2000-01-01 00:00")
def versionfilename = "versioninfo.txt"
def GROUP_DEBUG = 'Debug'
// Task to print project infos
task debugInitialSettings {
group = GROUP_DEBUG
doLast {
println 'Version: ' + project.wholeVersion
println 'Timestamp: ' + project.buildtimestamp
println 'Filename: ' + project.name
}
}
// To add the githash to zip
task renameZip {
doLast {
new File ("$buildDir/distributions/$project.name-${project.version}.zip")
.renameTo ("$buildDir/distributions/$project.name-${project.wholeVersion}.zip")
}
}
distZip.finalizedBy renameZip
// To add the githash to tar
task renameTar{
doLast {
new File ("$buildDir/distributions/$project.name-${project.version}.tar")
.renameTo ("$buildDir/distributions/$project.name-${project.wholeVersion}.tar")
}
}
distTar.finalizedBy renameTar
// Define the main class for the application
mainClassName = 'App'
dependencies {
// This dependency is found on compile classpath of this component and consumers.
compile 'com.google.guava:guava:23.0'
// Use the latest Groovy version for Spock testing
testCompile 'org.codehaus.groovy:groovy-all:2.4.13'
// Use the awesome Spock testing and specification framework even with Java
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
testCompile 'junit:junit:4.12'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
//To generate Testreports as HTML
test {
reports {
junitXml.enabled = false
html.enabled = true
}
}
distributions {
main {
contents {
from { 'build/docs' }
into ('reports') {
from 'build/reports'
}
}
}
}
//To make sure that test and javadoc ran before zip and tar
distTar.dependsOn test
distZip.dependsOn test
distTar.dependsOn javadoc
distZip.dependsOn javadoc
Please keep in mind I have not much knowledge about gradle as I'm just starting to learn it!
Thanks in advance :)
You have to move the githash definition outside the buildscript block
buildscript {
repositories {
jcenter()
}
dependencies {
//Add dependencies for build script, so we can access Git from our build script
classpath 'org.ajoberstar:grgit:1.1.0'
}
}
def git = org.ajoberstar.grgit.Grgit.open(file('.'))
//To save Githash
def githash = git.head().abbreviatedId
The reason is that when the buildscript block is evaluated line by line, its dependencies are not yet loaded. When the rest of the script is evaluated, the dependencies of the buildscript block have already been loaded. This is actually the reason for the buildscript block existence: to be run before the rest of the build and prepare the setup.
I'm using gradle liquibase plugin (https://github.com/liquibase/liquibase-gradle-plugin) but I don't understand how to pass parameters and values.
I want build a rollback based on tag and date but I'm only able to rollback on a tag with this command:
gradle rollback -DliquibaseTag=value
I don't understand how I should pass data, tag or count.
My grade.build is very simple:
apply plugin: 'liquibase'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'org.liquibase:liquibase-gradle-plugin:1.1.0'
classpath 'com.h2database:h2:1.3.160'
}
}
liquibase {
activities {
main {
changeLogFile 'changelog.xml'
url 'jdbc:h2:db/liquibase_workshop'
username 'sa'
password ''
}
}
runList = 'main'
}
It looks like the syntax may be a little different than documented. Instead try something like:
gradle rollbackCount -PliquibaseCommandValue=1