At the October Middleware Forum we ran a session on ‘Build Automation with Hudson’ which completed the Continuous Integration journey we had been working on all year. The slides are available here if you want to see what was presented. In this blog post we are going to run through how to install Hudson (and Artifactory) and get a build automation job up and running. Ultimately we want to be able to make a change to our code, check it in to our local GIT repository and then have Hudson run our maven build job on the code. The maven code will install weblogic, create a domain, deploy our code, optionally run some tests and then delete weblogic. Read on to see how we did it
First we need to set up maven. WebLogic 12c includes a copy of Maven so we’ll just use that one. To access it, modify your ~/.bash_profile and add the following (adjust based on your settings):
export ORACLE_HOME=/oracle/product/middleware export MVN_HOME=$ORACLE_HOME/oracle_common/modules/org.apache.maven_3.0.5 export PATH=$PATH:$MVN_HOME/bin:$HOME/bin
You should now be able to run ‘mvn -help’ on the command line. We’ll come back to configuring Maven, but first we need to set up Artifactory
Artifactory is an open-source artifact repository that we will use to manage our compiled code and any dependencies. Having an artifact repository is an important part of Continuous Integration as we should only compile code once and then use the same output on all our servers (dev, test, prod etc). The rationale behind this is that by using the same compiled code we can ensure that it will work in all environments, if we had to re-compile for each environment we couldn’t be confident that it was the same code and would work everywhere. Obviously there will be differences required for each environment, but they can generally be done with build plans (ie: change end-point URLs for test and prod).
Anyway to install and configure Artifactory download the rpm from http://www.jfrog.com/home/v_artifactory_opensource_download and then run:
rpm -ivh artifactory-3.1.0.rpm service artifactory start
Pretty easy! Now just go to localhost:8081/artifactory and you should see your artifactory system (the login is admin/password)
With that installed we need to tell our local maven to use Artifactory as it’s remote repository. This is one of the main benefits of using Artifactory as we can use it as a central repository for our entire team. If we need to use external resources, Artifactory can cache them from everyone, which will minimise network traffic. Plus our local code will be put in here so that everyone has a single source of truth for it. Artifactory can actually generate our maven settings automatically, so just log into Artifactory and click the ‘Maven Settings’ link on the home page. On that screen click ‘Mirror any’ (this will cache any artifacts from remote repositories) and click ‘Generate Settings’. Copy the output to ~/.m2/settings.xml (it may not exist yet). This is the settings file for Maven and controls what repositories it uses and where they are. We also need to add one more server to point to the ext-release-local repository. This repository holds artifacts that aren’t built by us, but aren’t available on a remote repository. We will use this to store some WebLogic artifacts (I really wish Oracle provided them on a remote repository, but oh well). So add the following to the <servers> section
<server> <id>internal</id> <username>admin</username> <password>password</password> </server>
And add the following to the <repositories> section:
<repository> <snapshots/> <id>internal</id> <name>ext-release-local</name> <url>http://localhost:8081/artifactory/ext-release-local</url> </repository>
Before we can use Maven with WebLogic we need to publish some WebLogic artifacts to our repository, plus we’ll also add our WebLogic 12.1.3 zip file to the repository so that we can use it to install WebLogic later. You may remember this trick from a previous blog post.
cd $ORACLE_HOME/oracle_common/plugins/maven/com/oracle/maven/oracle-maven-sync/12.1.3 mvn deploy:deploy-file -Dfile=oracle-maven-sync.12.1.2.jar -DpomFile=oracle-maven-sync.12.1.2.pom -DrepositoryId=internal -Durl=http://localhost:8081/artifactory/ext-release-local -- Check that it worked with: mvn help:describe -DgroupId=com.oracle.maven -DartifactId=oracle-maven-sync -Ddetail=true -- Now synchronise the weblogic repository with mvn com.oracle.maven:oracle-maven-sync:push -Doracle-maven-sync.serverId=internal mvn archetype:crawl
Now we’ll upload the WebLogic zip file into our maven repository. This means we can call it from our maven pom file to install WebLogic on the fly. Again see the previous blog post about this for more information.
cd $MW_HOME/wlserver/lib mvn deploy:deploy-file -Dfile=wls-maven-plugin.jar -DpomFile=pom.xml -DrepositoryId=internal -Durl=http://localhost:8081/artifactory/ext-release-local cd <wherever you downloaded the zip file to> mvn deploy:deploy-file -Dfile=wls1212_dev.zip -DgroupId=com.oracle.weblogic -DartifactId=wls-dev -Dpackaging=zip -Dversion=18.104.22.168 -DrepositoryId=internal -Durl=http://localhost:8081/artifactory/ext-release-local
Now let’s install Hudson. It’s actually very easy, just do the following (as root):
wget -O /etc/yum.repos.d/hudson.repo http://hudson-ci.org/redhat/hudson.repo yum check-update yum install hudson service hudson start
Go to localhost:8080 and you should see a configuration page for Hudson. On this page install the Git, Maven 3 and Maven 3 Snapshot Monitor plugins and click the ‘Finish’ button to start Hudson. You should see a screen like this (minus the job list):
Before we continue we need to do some configuration of hudson to support our Maven, Git and Artifactory settings. First install the Artifactory plugin:
- Go to ‘Manage Hudson’->’Manage Plugins’
- Go to ‘Available’ and click the ‘Search’ tab
- Search for ‘artifactory’
- Install the plugin and restart Hudson
Now to go to ‘Manage Hudson’->’Configure System’ and:
- Set JAVA_HOME to /usr/java/jdk1.7.0_45 (or whatever your JAVA_HOME is)
- Add a GIT server and set the path to ‘git’ (we’ll install git in a minute)
- Add an artifactory server:
- URL: http://localhost:8081/artifactory
- Credentials: admin/password
Now we need to configure the Maven settings as Hudson will not be able to use the settings we created for our normal user. Go to ‘Manage Hudson’->’Maven’ and copy the following into a new parameter called ‘Global’
<?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <servers> <server> <username>admin</username> <password>password</password> <id>central</id> </server> <server> <username>admin</username> <password>password</password> <id>snapshots</id> </server> <server> <id>internal</id> <username>admin</username> <password>password</password> </server> </servers> <profiles> <profile> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>libs-release</name> <url>http://localhost:8081/artifactory/libs-release</url> </repository> <repository> <snapshots /> <id>snapshots</id> <name>libs-snapshot</name> <url>http://localhost:8081/artifactory/libs-snapshot</url> </repository> <repository> <snapshots /> <id>internal</id> <name>ext-release-local</name> <url>http://localhost:8081/artifactory/ext-release-local</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>plugins-release</name> <url>http://localhost:8081/artifactory/plugins-release</url> </pluginRepository> <pluginRepository> <snapshots /> <id>snapshots</id> <name>plugins-snapshot</name> <url>http://localhost:8081/artifactory/plugins-snapshot</url> </pluginRepository> </pluginRepositories> <id>artifactory</id> </profile> <profile> <id>local-weblogic</id> <properties> <wls.adminurl>t3://127.0.0.1:7001</wls.adminurl> <wls.user>weblogic</wls.user> <wls.password>welcome1</wls.password> <wls.middlewareHome>/oracle/products/middleware/Oracle_Home/</wls.middlewareHome> <wls.targets>DefaultServer</wls.targets> </properties> </profile> </profiles> <activeProfiles> <activeProfile>artifactory</activeProfile> <activeProfile>local-weblogic</activeProfile> </activeProfiles> </settings>
We’ll use Git as our source control system. Install it with:
yum install git git config --global user.name "oracle" git config --global user.email "email@example.com"
Create the App
Having installed and configured everything, let’s create our application. You could use any java app you have, but for demonstration purposes we’ll create a basic web app using the Maven archetypes (see our blog post on Maven for more info).
mkdir /home/oracle/code && cd /home/oracle/code mvn archetype:generate -DarchetypeGroupId=com.oracle.weblogic.archetype -DarchetypeArtifactId=basic-webapp -DarchetypeVersion=12.1.2-0-0 -DgroupId=com.test -DartifactId=test-webapp -Dversion=1.0-SNAPSHOT -DarchetypeRepository=~/.m2/repository
Now modify the maven pom.xml file to have all the steps to install WebLogic, deploy and remove. You can use the maven file (https://github.com/Joelith/Maven-Install-WLS/blob/master/pom.xml) (follow the blog post for more info)
cd test-webapp -- Commit to git git init git add . git commit git log --to check that it worked
Note: You will need to change the permissionon the /home/oracle folder so that the Hudson user will be able to access it later. It’s a bit of a hack so that Hudson can access the git folder, normally your git would be remote so this wouldn’t be a problem. I could have also created my code somewhere Hudson had access to.
sudo chmod a+r /home/oracle sudo chmod a+x /home/oracle
Create our Hudson job
Now go back to Hudson and click to ‘Create a new Job’:
- Set the name to ‘BuildTestApp’. Note: Don’t use spaces in the name, Hudson will allow it, but our maven scripts will fail later on
- Click ‘Discard Old Builds’. Set the max to 5 (otherwise we end up with a lot of logs lying around)
- Set SCM to git and set the repo to ‘file:///home/oracle/code/test-webapp’
- Set ‘Build Triggers’ to ‘Poll SCM’ and set the schedule to ‘*****’ (basically run constantly)
- Click ‘Maven3-Artifactory’ under ‘Build Environment’
- Click ‘Add build step’ under ‘Build’ and choose ‘Invoke Maven 3’
- Set the goals to ‘clean verify’
- Click ‘Advanced’
- Set ‘Settings’ to ‘Global’ (this will use the settings parameter we configured earlier)
- Click ‘Save’
Now either make a change to the source code and check it into Git or click ‘Build Now’ to run the job. This will check out the code, compile it, install WebLogic, configure a domain, start WebLogic, deploy the jar to WebLogic, stop WebLogic, remove the domain and delete all the files. It will then push the compiled artifact to Artifactory so others can use it.
Congratulations, if you’ve made it this far! You’ve created a continuous integration environment that is able to control your code and build it automatically (including the supporting software like WebLogic).