Deploying to GlassFish using Maven2

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 />   &nbsp
;                    <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.

Deploying to GlassFish using Maven2 gf mvn nb6 selecting a maven goal

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
------------------------------------------------------------------------

 

 

11 Comments

  1. Chanchal Kumar May 8, 2009
  2. site admin April 17, 2008
  3. LYeung April 12, 2008
  4. Chanchal Kumar March 28, 2008
  5. Chanchal Kumar March 28, 2008
  6. Wouter van Reeven March 23, 2008
  7. Chanchal Kumar March 20, 2008
  8. Wouter van Reeven March 4, 2008
  9. Jacob Nikom March 3, 2008
  10. Sudhakar November 14, 2007
  11. hkjack November 3, 2007