Gradle Weblogic deployment using wldeploy

on Tuesday, 26 November 2013


In this recipe we will see how easy it is to call the Weblogic tool "wldeploy" from Gradle.

Getting ready


The deployment depends on having a copy of the wlfullclient.jar available to the build scripts. There are plenty of tutorials online on how to create this, so I wont cover that here.

How to do it ...

  • Create our build file.

buildscript{
configurations {
weblogic
}
dependencies {
weblogic files('/path/to/wlfullclient.jar')
}
}
/*******************************************************************
LOAD PROJECT CONFIGURATION
Usage: this task slurps the configuration that is defined in ./gradle/config/weblogicBuildConfig.groovy
into a property called config.
********************************************************************/
task loadConfiguration {
def env = project.hasProperty('env') ? project.getProperty('env') : 'local'
logger.quiet "Loading configuration for environment '$env'"
def configFile = file("$rootDir/gradle/config/weblogicBuildConfig.groovy")
def parsedConfig = new ConfigSlurper(env).parse(configFile.toURL())
allprojects {
ext{
config = parsedConfig
}
}
}
/*******************************************************************
DEPLOY TO WEBLOGIC
Usage: Deploy each file in the ./build/libs directory.
Parameters: -Penv
********************************************************************/
task deployToWLS {
group = 'Deployment'
description = 'Deploy artifacts to a weblogic server. Requires -Penv=<env>'
} << {
ant.taskdef( name: 'wldeploy',
classname: 'weblogic.ant.taskdefs.management.WLDeploy',
classpath: configurations.weblogic.asPath
)
new File("$buildDir/libs/").eachFile { file ->
// Get the filename minus the extension
def filename = file.getName().split("\\.")[0]
logger.lifecycle "[INFO] deploying $file to $env weblogic instance"
ant.wldeploy(action: 'deploy',
source: file,
name: filename,
adminurl: config.wls.adminurl,
user: config.wls.user,
password: config.wls.password,
upload: config.wls.upload,
targets: config.wls.targets,
verbose: config.wls.verbose,
debug: config.wls.debug
)
}
  • Create our Weblogic instance configurations.

environments {
local {
wls {
adminurl = 't3://localhost:7001'
user = 'weblogic'
password = 'welcome1'
targets = 'AdminServer'
upload = 'true'
verbose = 'true'
debug = 'false'
}
}
}

How it works ...

I wont get into the configuration elements of this project. Its covered in great detail here. The important thing to take from this is we are tokenizing our build scripts making them reusable.

The deployToWLS task is where the magic happens. Here we define the ant task using the name, classname and classpath. Next we specify a directory with our deployables; I've used the "build/libs" directory as that's where my war files are packaged to. Next, through the powers of the eachFile closure we get the filename and trim the extension, we then deploy each file in that directory.


0 comments:

Post a Comment