In my last post, I talked about one exciting new feature in WebLogic 11g, support for Mac OS X. Now, I want to cover another exciting new feature – a Maven plugin which allows you to incorporate deployment of your applications to WebLogic into your Maven builds.
I love this new feature because it makes it so easy to compile all my Java source and web artefacts, build a deployment archive and deploy it WebLogic Server in one easy action! I hope you like it too!
A big thank you to Steve Button for bringing this to my attention, helping me to get it working on Mac OS X and sharing some helpful hints and tips.
Setting up the plugin
First, let’s set up the Maven plugin. You will need WebLogic Server 10.3.4 installed to do this. I used the convenient new ‘ZIP Distribution’ which is built for developers. I used Maven 2.2.1 and JDK 1.6 on Mac OS X 10.6 to write this article.
First, we need to create the plugin using the wljarbuilder utility. Execute the following commands from your WebLogic home directory:
# cd <WL_HOME>/wlserver/server/lib # java -jar wljarbuilder.jar -profile weblogic-maven-plugin
This will create a new file called weblogic-maven-plugin.jar in that same directory. Next, we need to extract the Maven POM from the jar file, and place it in the same directory. You may want to extract it into a temporary directory and then move it.
# jar xvf <WL_HOME>/wlserver/server/lib/weblogic-maven-plugin.jar META-INF/maven/com.oracle.weblogic/weblogic-maven-plugin/pom.xml # mv META-INF/maven/com.oracle.weblogic/weblogic-maven-plugin/pom.xml <WL_HOME>/wlserver/server/lib
Before installing the WebLogic Maven plugin into your local repository, it is important to make sure all of the necessary dependencies have been downloaded.
# cd <WL_HOME>/wlserver/server/lib # mvn install
Note: before you continue, if you want to use the WebLogic plugin from the command line, you might want to set up a short name so you can type shorter goal names. You have to do this now, before you continue. To set up a short name, weblogic, you will need to add the following entries to your ~/.m2/settings.xml Maven configuration file…
<pluginGroups> <pluginGroup>com.oracle.weblogic</pluginGroup> </pluginGroups>
… and add the following to the pom.xml that you just extracted into your <WL_HOME>/wlserver/server/lib directory.
<build> <plugins> <plugin> <artifactId>maven-plugin-plugin</artifactId> <version>2.3</version> <configuration> <goalPrefix>weblogic</goalPrefix> </configuration> </plugin> </plugins> </build>
Now we can install the plugin. You must run mvn install first to make sure it downloads the dependencies and recognises the prefix we have defined.
# mvn install # mvn install:install-file -Dfile=weblogic-maven-plugin.jar -DpomFile=pom.xml
The plugin will now be available for use. We can execute the ‘help’ goal to test it:
# mvn com.oracle.weblogic:weblogic-maven-plugin:help
Or, if you set up the short name:
# mvn weblogic:help
Using the plugin to deploy an application
Probably the most convenient way to use the WebLogic Maven plugin, is to incorporate it into your standard build. Let’s look at how we can set up a project to compile, test, package and deploy to WebLogic by simply typing mvn deploy.
First, create a new Maven project. I used mvn archetype:generate, selected the webapp-javaee6 template, and provided the necessary details. I called my project wldemo.
We need to add a few items to the pom.xml to tell Maven to deploy the application to WebLogic.
<plugin> <groupId>com.oracle.weblogic</groupId> <artifactId>weblogic-maven-plugin</artifactId> <version>10.3.4</version> <configuration> <adminurl>t3://localhost:7001</adminurl> <user>weblogic</user> <password>password</password> <name>wldemo</name> <remote>true</remote> <upload>true</upload> <targets>AdminServer</targets> </configuration> <executions> <execution> <id>deploy</id> <phase>pre-integration-test</phase> <goals> <goal>deploy</goal> </goals> <configuration> <source>target/wldemo.war</source> </configuration> </execution> </executions> </plugin> ... <distributionManagement> <!-- use the following if you're not using a snapshot version. --> <repository> <id>local</id> <name>local repository</name> <url>file:///Users/mark/.m2/repository</url> </repository> <!-- use the following if you ARE using a snapshot version. --> <snapshotRepository> <id>localSnapshot</id> <name>local snapshot repository</name> <url>file:///Users/mark/.m2/repository</url> </snapshotRepository> </distributionManagement>
You will have to update the settings to match your environment, as follows:
Setting Meaning ----------- ---------------------------------------- adminurl WebLogic T3 URL for your Admin Server user WebLogic administrative user password WebLogic administrative user's password name Name of application remote Indicates a remote server upload Indicates the war file must be uploaded targets The server, cluster, etc. to deploy to
In addition to the WebLogic settings in the plugins section, you need to set up the distributionManagement section to point to your local repository so that the deploy phase can run successfully.
Now we are ready to build, test and deploy our application! In real life, you might want to write some code first – but for this example, we will just use the simple JSP page that comes in the template.
# mvn deploy
After a few moments, you application will be built and deployed. You can see it right there in the WebLogic Server console:
You can now hit the application at http://yourserver:7001/wldemo. You should get a clean page with the message ‘Hello World!‘
Now you can make changes and quickly and easily deploy them to your server for testing with a single, simple command! The plugin will also let you start and stop applications and undeploy them.
Where to find more information
You might want to read over the documentation here for more details on the goals and parameters that are available in the plugin.