Yesterday I wrote an article about Building Enterprise Applications for GlassFish using Netbeans 6.0 (Beta 2) and Maven2. The article explains how to setup your Netbeans projects to be able to generate an EAR file that can be deployed to GlassFish V2. The one thing missing from that article is how to actually deploy your EAR file to GlassFish V2. This article explains how to do that.
Using the asadmin utility for deployment
Digging through the GlassFish Application Deployment Guide revealed that there is one way to deploy applications to GlassFish. That way is by using the asadmin command. Inspection of the ant build scripts, that are created and used by Netbeans when not using Maven, shows that Netbeans also uses this command to deploy applications. The use of the asadmin command for deployment of applications is fairly easy. The general command that needs to be issued is
asadmin deploy --user=<adminuser> <path to jar/war/ear>
In this command, <adminuser> is the username of a user with admin rights. By default this is “admin”. And <path to jar/war/ear> refers to the path to the archive containing the application that you would like to deploy. This path could be something like “C:\devel\MyProject\MyProject-ear\target\MyProject-1.0-ear.ear” or “./MyProject-ear/target/MyProject-1.0-ear.ear”. the latter path, of course, is a path relative to the current working directory.
The above command assumes you are deploying to a locally installed and running GlassFish server. Things get a little more complicated when deploying to a remote server. The simplest way of doing such a deployment is
asadmin deploy --user=<adminuser> --host=<hostname> <path to jar/war/ear>
In this command, the arguments <adminuser> and <path to jar/war/ear. have the same meaning as in the first command. And <hostname> of course refers to the host to which the application should be deployed. Valid hostnames are localhost, appserv1, appserv1.my.domain but also the 192.168.0.123 IP address. The first and second commands assume your admin console is listening on port 4848. In case the admin console is listening on a different port you can add this flag
--port=<port>
By default, the password of the local GlassFish server is stored in a file called .asadminpass in the home directory of the user that installed the GlassFish server. On Linux the home directory usually is /home/<username> or sometimes export/home/<username>. On Windows the default location of this file is C:\Documents and Settings\<username> (as far as I know. I have modified my default home due to cygwin) but there are many ways for system administrators to modify this location.
By default, the asadmin utility will look into the .asadminpass file for the password to use when deploying your application. This password, however, is host dependent. My .asadminpass file only contains the password for the admin user on my local GlassFish installation. Therefore, deploying to a remote host will make asadmin prompt for the admin password:
$ ./java/nb60b2/glassfish-v2-b58g/bin/asadmin deploy --user=admin --host=laptop --port=4848 \ ./MavenEnterpriseApplication-ear/target/MavenEnterpriseApplication-ear-1.0-SNAPSHOT.ear Please enter the admin password>
fortunately, the asadmin utility allows you to provide the admin password through the –passwordfile command line flag. The argument to this flag is the path to the file containing the admin password, e.g.
--passwordfile="C:\Documents and Settings\wouter\remote_admin_password.txt"
The password file should contain a line like this
AS_ADMIN_PASSWORD=adminadmin
Now the asadmin command completes without prompting for the password:
$ ./java/nb60b2/glassfish-v2-b58g/bin/asadmin deploy --user=admin --passwordfile=/home/wouter/.asadminadminpassword --host=laptop --port=4848 ./MavenEnterpriseApplication-ear/target/MavenEnterpriseApplication-ear-1.0-SNAPSHOT.ear Command deploy executed successfully.
Moving to Maven
Since it is my purpose to use Maven for the deployment of my applications, I need a way to execute the asadmin command from Maven. By default Maven doesn’t allow for executing system commands. Fortunately the Mojo people at codehaus.org (which also hosts the MevenIDE plugin) have developed a plugin to execute system commands. The use of this plugin is fairly simple. the plugin needs to be registered in the pom.xml file of the project that needs the plugin and then the plugin needs to be configured in such a way that it will execute the system command. Here’s the pom.xml part that works for me. I have put this in the pom.xml file of the EAR project.
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>/home/wouter/java/nb60b2/glassfish-v2-b58g/bin/asadmin</executable> <arguments> <argument>deploy</argument> <argument>--user=admin</argument> <argument>--passwordfile=/home/wouter/.asadminadminpassword</argument> <argument>--host=laptop</argument> <argument>--port=4848</argument>< br />   ; <argument>target/${artifactId}-${version}.${packaging}</argument> </arguments> </configuration> </plugin> </plugins> </build>
Let’s focus on the configuration part. The executable tag contains the full path to the asadmin utility. I have tried to use the workingdirectory and basedirectory tags that are mentioned on the plugin page but for some reason they didn’t do what I expected them to do. So I just put the complete path there and that works just fine.
There are two ways to add command line arguments to the asadmin utility and they result in the same behaviour. One way is to specify a commandlineArgs tag and put the entire rest of the command line in that tag. The other way is to make use of the arguments tag with nested argument tags as in the example above. Basically all command line arguments that I explained in the first part of this article are there. simply replace all parts behind the “=” symbols with your values and you should be fine.
The last argument makes use of the parameter inheritance of Maven. Maven will expand the parameters to the values that are set by Maven. In this case, the line
${artifactId}-${version}.${packaging}
will expand to
MavenEnterpriseApplication-ear-1.0-SNAPSHOT.ear
Pretty neat huh? Please be aware that that final argument only works when it is invoked from the EAR project. If you’d like to run the exec target from e.g. the Parent POM Project you’ll have to modify the path so Maven can find the ear file that needs to be deployed.
Invoking the deployment goal
In order to do the actual deployment, the exec:exec goal needs to be executed. From within Netbeans this can be done by right clicking the EAR project and selecting custom -> Goals. A window pops up in which you can specify the goal that needs to be performed. The cool thing is that MevenIDE is smart enough to auto complete whatever you type, so entering “ex” should give you the choice between exec:exec and exec:java. In this case choose the exec:exec goal. Tick the Show Debug Output check box to get extended debugging info. This is particularly useful in case the exec:exec goal fails.
If all goes well, Maven should report that the deployment was succesfull
[DEBUG]Configuring mojo 'org.codehaus.mojo:exec-maven-plugin:1.1-beta-1:exec' --> [DEBUG] (f) arguments = [deploy, --user=admin, --passwordfile=/home/wouter/.asadminadminpassword, --host=laptop, --port=4848, target/MavenEnterpriseApplication-ear-1.0-SNAPSHOT.ear] [DEBUG] (f) basedir = /home/wouter/NetBeansProjects/MavenEnterpriseApplication/MavenEnterpriseApplication-ear [DEBUG] (f) executable = /home/wouter/java/nb60b2/glassfish-v2-b58g/bin/asadmin [DEBUG] (f) project = org.apache.maven.project.MavenProject@38a63e21 [DEBUG] (f) skip = false [DEBUG]-- end configuration -- [exec:exec] Command deploy executed successfully. ------------------------------------------------------------------------ BUILD SUCCESSFUL ------------------------------------------------------------------------ Total time: 7 seconds Finished at: Thu Nov 01 14:52:48 CET 2007 Final Memory: 27M/62M ------------------------------------------------------------------------
Hello Folks,
In the meantime, there has been a good plugin release for glassfish/sailfin, please find the same at: https://maven-glassfish-plugin.dev.java.net/
Cheers,
Chanchal Kumar
Hi LYeung,
Thanks for the suggestion. I will have a look at this soon. In the mean time you might be interested in knowing that the MevenIDE plugin now also supports deploying EAR projects to GlassFish. No more need for external plugins from within NetBeans 🙂
Greets, Wouter
Hi Wouter,
You can try clownfish plugin at http://clownfish.sourceforge.net. It’s a glassfish maven2 plugin that utilises jsr-88.
Cheers.
Sorry, the code was inadvertently left out, here it comes:
exec
executable=”cmd” os=”Windows 2000″
failonerror=”true”
dir=”${basedir}”
arg line=”/c asadmin.bat deploy ..options..
exec
Hi Wouter,
Thanks for the info, I appreciate it. When using either exec-maven-plugin or maven-antrun-plugin for a pure Linux (and in general any unix) deployments (or any other asadmin tasks), the plugins work okay and do the job. Unfortunately, problems pop up when we want a transparent os-proof environment. For instance, for windows we do need a few tweaks.
For someone facing the same issue of setting up a cross-platform continuous integration setup for maven environment, a small tip to make sure that the windows part also works is that a construct like this (only tested on windows 2000) could be used:
… ${artifactId}${version}-${packaging}” />
Please see http://ant.apache.org/manual/CoreTasks/exec.html for details
A better solution, ofcourse, is the next step 🙂
Hi Chanchal,
I agree that using the exec plugin isn’t the preferred way of deploying to GlassFish. As far as I know, GlassFish still isn’t fully supported in Cargo. See e.g.
http://jira.codehaus.org/browse/CARGO-491
and the second comment at
http://blogs.sun.com/theaquarium/entry/vote_for_atlassian_formal_support
Having said that, I have filed an RFE for mevenide to add a Run option to EAR projects. Milos Kleint has provided a fix for this which should be included in a future version of mevenide. This fix already is included in version 3.1-SNAPSHOT but at this moment I don’t know when that fix will be released.
Is there anyone from glassfish/sailfin working to add something to the cargo plugin? http://cargo.codehaus.org/Maven2+plugin, thats one ‘neat’ place to have an elegant plugin. The exec seems to be a bit primitive and error-prone
Hi Jacob,
Maven will download the plugin itself since Maven can download it from the main Maven repository. See http://repo1.maven.org/maven2/org/codehaus/mojo/exec-maven-plugin/
Greets, Wouter
Hi,
You have very useful blog because you provided
some details how to install the exec plugin. It is very
difficult to find the documentation how to install
exec-maven-plugin.
What I did not understand is that you only modified
the pom.xml file and added plugin child, but said nothing
about plugin itself.
Where did you put your exec-maven-plugin-1.0.2.jar file?
Did you place it in your .m2/repository/org/codehause/mojo directory?
Or you added it to you local repository?
Do you have to describe where it is located?
Thank you,
Jacob Nikom
This is great! One of the reasons I’ve stayed away from Netbeans is because of it’s tight integration with Ant. When everyone else is moving to Maven, Netbeans seems bent on sticking with Ant.
But this example proves that Maven can also be used with Netbeans. With Netbeans 6 I am encouraged to take another look at it.
Another way to achieve this using ANT tasks :
org.apache.maven.plugins
maven-antrun-plugin
Deploying ${ear.file} …
run