Passing parameter and value to Gradle liquibase plugin - gradle

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

Related

Gradle Liquibase Integration

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.

Apply external Plugin to a subset of subprojects

I am using the Gradle SSH Plugin to deploy some, but not all subprojects to two different remote machines. I want to configure these remotes in the main build script to avoid duplication, but cannot apply the plugin as per this:
configure([
project(':Subproject1'), project(':Subproject5'), project(':Subproject7')
])
{
buildscript {
dependencies {
classpath 'org.hidetake:gradle-ssh-plugin:2.4.2'
}
}
apply plugin: "org.hidetake.ssh"
ssh.settings {
user = getProperty('ssh.username')
identity = file(getProperty('ssh.identity'))
knownHosts = allowAnyHosts
}
remotes {
tomcat {
host = getProperty('ssh.hosts.tomcat')
}
jboss {
host = getProperty('ssh.hosts.jboss')
}
}
}
Gradle fails with > Plugin with id 'org.hidetake.ssh' not found.
Everything is fine when the contents of the configuration closure are applied per project. How can I elegantly solve this issue?
Guessing here as I haven't reproduced your issue, but the buildscript block is special as it is pre-parsed before normal groovy parsing of the build files and I suspect that having it inside a project block like you have will not work.
Have you tried moving the buildscript block to the root level in the script above?
edit 1: old gradle forums post discussion this can be found here
edit 2: adding a snippet of code in response to a comment.
To remove duplication and only define say the repository references in one place, you could use the following pattern:
buildscript {
ext.RepositoryConfigurator = {
maven {
credentials.username artifactoryReader
credentials.password artifactoryReaderPwd
url artifactoryReaderUrl
}
jcenter()
mavenCentral()
}
ext.DependencyConfigurator = {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2"
}
ext.ResolutionConfigurator = {
resolutionStrategy.cacheChangingModulesFor 60, 'minutes'
}
repositories RepositoryConfigurator
dependencies DependencyConfigurator
configurations.classpath ResolutionConfigurator
}
apply plugin: "some.plugin.requiring.above.classpath"
gradle.rootProject {
buildscript {
repositories RepositoryConfigurator
dependencies DependencyConfigurator
configurations.classpath ResolutionConfigurator
}
}
gradle.allprojects {
buildscript {
repositories RepositoryConfigurator
dependencies DependencyConfigurator
configurations.classpath ResolutionConfigurator
}
}
In other words: since the buildscript block is special and pre-parsed, you can not use things defined elsewhere in the buildscript block. Going in the other direction is ok though, i.e. you can define things in the buildscript block which are then visible elsewhere. We can use this to define the repository references once inside the buildscript block and then use that reference in other places in the build file.
The above is from a settings.gradle file so might or might not be a perfect fit, but should demonstrate the idea.

Gradle Custom buildScriptRepository methods

I would like to make a custom buildScript repository method so I can easily reference our internal maven repo. Right now I'm required to declare a maven block everywhere we use our plugin.
Here is the current setup
buildscript {
repositories {
jcenter()
maven { url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
What I would like to do is something like this
buildscript {
repositories {
jcenter()
myReleaseRepo()
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
How can I make a method available to create a repository anywhere we may use the plugin in the future?
Another solution is to add custom methods on RepositoryHandler using some Groovy goodness. Just chuck this in ~/.gradle/init.gradle
RepositoryHandler.metaClass.myReleaseRepo = {
delegate.maven {
name 'myReleaseRepo'
url 'http://myNexus:8081/nexus/content/repositories/My-Release'
}
}
After that, you can use it just as you described:
buildscript {
repositories {
myReleaseRepo()
}
}
Metaclasses in Groovy are just great. The delegate in this case is pretty much like the javascript this. This code is essentially using the RepositoryHandler instance (delegate keyword) and just calling repositoryHandlerInstance.maven(name, url).
It is possible to add a repo to an init script which would then apply to all gradle invocations that use the init script - without having to individually declare your maven repo in each build.gradle.
Solution 1:
Partial solution, does not do exactly what you're asking for. In init.gradle:
allprojects{
buildscript{
repositories{
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
}
}
Then your build.gradle can skip buildscript repo declaration entirely:
buildscript {
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
Solution 2:
In fact, you can even move your buildscript classpath declaration to init and have the plugin apply to all projects that use the init script:
beefier init.gradle
allprojects{
buildscript{
repositories{
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
}
gives you a lighter build.gradle
apply plugin: 'my-plugin'
I tried to, but apparently you cannot move the apply line to init.gradle as well. see this defect.
Solution 3:
I retract what I said in the comment above, I figured out how to do exactly what you're asking for. Apparently you can create extensions for the buildscript block using the initscript. However I still prefer solution2, because it gives you a cleaner build.gradle.
To create a buildscript extension, in your init.gradle:
class customRepos {
def buildscript
customRepos(buildscript) {
this.buildscript = buildscript
}
void addMyRepo() {
buildscript.repositories {
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
}
}
allprojects {
extensions.create('myRepo', customRepos, buildscript)
}
which then allows you to do this in your build.gradle
buildscript{
myRepo.addMyRepo()
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
apply plugin: 'my-plugin'

Gradle Liquibase plugin not using specified contexts

We have a Gradle project which uses Liquibase, and our build file has:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.liquibase:liquibase-gradle-plugin:1.1.1'
classpath 'org.liquibase:liquibase-core'
}
}
apply plugin: 'liquibase'
Yet when we try to update our project via:
gradle update -Dliquibase.contexts=foobar
(tried with --contexts too)
it seems to ignore the specified contexts and runs all the changesets instead.
Example:
changeSet(author: 'me', id: 'someId1', context: 'somethingElse') { // This runs, but shouldn't
// ...
changeSet(author: 'me', id: 'someId2', context: 'foobar') { // Should only run this
The project was forked at one point, so maybe we're misinterpreting the documentation (original, new) but it seems like this should work.
Do we need a different plugin/version? Are we calling it wrong?
Derp, looks like you need to grab the contexts manually in your build file:
main {
if (project.hasProperty('contexts')) {
contexts contexts
}
url 'someurl'
username 'username'
password 'pass'
}
Then pass it in via:
gradle update -Pcontexts=schema
You should define your contexts inside task
task('liquibase_dev') << {
liquibase {
activities {
main {
changeLogFile changeLog
url 'jdbc:postgresql://localhost:5432/test'
username 'postgres'
password 'admin'
contexts 'DEV'
}
}
}
}
Liquibase changelog:
<changeSet author="xxx" context="DEV" id="1591257804177-1" ...>
<changeSet author="xxx" context="PROD" id="1591257804177-2" ...>
Then you can run like:
./gradlew :liquibase_dev update
Only the first changeset will be executed
Newer plugin (2.0.3), same problem and slightly different solution, 'cause I'm keeping a default context.
liquibase.gradle (snippet):
if (!project.hasProperty("contexts")) {
project.ext.contexts = "dev"
}
liquibase {
activities {
main {
driver "${datasource_driver}"
url "${datasource_url}"
username "${datasource_username}"
password "${datasource_password}"
changeLogFile "src/main/resources/db/changelog/master.xml"
defaultSchemaName ""
logLevel "info"
contexts contexts
}
}
}
tasks.liquibaseUpdate.dependsOn(tasks.classes)
gradle.properties (snippet):
liquibaseTaskPrefix=liquibase
So, now it's safe to call:
./gradlew liquibaseUpdate # to apply contexts=dev
And:
./gradlew liquibaseUpdate -Pcontexts=test # to apply contexts=test

Gradle: Cannot configure artifactory from an external build script

I am new to gradle and would like to access my artifactory repository from it. If I put all configurations into one build script, the build succeeds. Here are the relevant parts of my build.gradle:
allprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'artifactory'
}
// ...
buildscript {
repositories {
maven {
url 'http://repo.jfrog.org/artifactory/gradle-plugins'
}
maven {
url artifactory_contextUrl + 'plugins-release'
credentials {
username = artifactory_user
password = artifactory_password
}
}
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.16')
}
}
artifactory {
contextUrl = artifactory_contextUrl
publish {
repository {
repoKey = 'libs-release-local'
username = artifactory_user
password = artifactory_password
maven = true
}
}
resolve {
repository {
repoKey = 'libs-release'
username = artifactory_user
password = artifactory_password
maven = true
}
}
}
dependencies {
// My dependencies ...
}
// Rest of the build script ...
Now, I would like to pull out the artifactory part into a separate gradle script for better organization. This is where the build goes wrong. Quite surprisingly, I get the following error even if I copy my build.gradle to foo.gradle, and change build.gradle to just contain the single line
apply from: 'foo.gradle'
The error is
FAILURE: Build failed with an exception.
* Where:
Script '/path/to/my/project/foo.gradle' line: 5
* What went wrong:
A problem occurred evaluating script.
> Plugin with id 'artifactory' not found.
In case this is not a bug, can anyone please explain this behavior of gradle's apply from and propose a solution?
Thank you
The apply from part is parsed once the build script is already configured, so telling Gradle where to find the plugins with specific ID is too late. You'll have to keep the buildscript part in the script, or put it in the init script:
apply from : 'http://link.to/my/gradle.script'
You can also use the fully qualified class name to apply the plugins in your helper script:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "com.adaptc.gradle:nexus-workflow:0.5"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:2.2.4"
}
}
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin
apply plugin: com.adaptc.gradle.nexusworkflow.NexusWorkflowPlugin
Note that Gradle won't find the plugins if you put quotes around the class name, as you would do normally with plugin names.
This is how I found the class name for the Artifactory plugin:
I downloaded the plugin which was thankfully open source.
I searched for the name of the plugin among the files and found
artifactory-puplish.properties.
It contained the following
property: implementation-class=org.jfrog.gradle.plugin.artifactory.ArtifactoryPublicationsPlugin
The source of nexus-workflow has no such properties file so I looked around until I found
plugins-gradle-master/nexus-workflow/src/main/groovy/com/adaptc/gradle/nexusworkflow/NexusWorkflowPlugin.groovy

Resources