Deploying to GlassFish using Maven2

11

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=&lt;adminuser&gt; --host=&lt;hostname&gt; &lt;path to jar/war/ear&gt;

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=&lt;port&gt;

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&gt;

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.

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;exec&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;executable&gt;/home/wouter/java/nb60b2/glassfish-v2-b58g/bin/asadmin&lt;/executable&gt;
                    &lt;arguments&gt;
                        &lt;argument&gt;deploy&lt;/argument&gt;
                        &lt;argument&gt;--user=admin&lt;/argument&gt;
                        &lt;argument&gt;--passwordfile=/home/wouter/.asadminadminpassword&lt;/argument&gt;
                        &lt;argument&gt;--host=laptop&lt;/argument&gt;
                        &lt;argument&gt;--port=4848&lt;/argument&gt;&lt;
br /&gt;   &amp;nbsp
;                    &lt;argument&gt;target/${artifactId}-${version}.${packaging}&lt;/argument&gt;
                    &lt;/arguments&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

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' --&gt;
[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
------------------------------------------------------------------------

 

 

Share.

About Author

11 Comments

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

  2. Chanchal Kumar on

    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

  3. Chanchal Kumar on

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

  4. Wouter van Reeven on

    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.

  5. Jacob Nikom on

    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

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

  7. Another way to achieve this using ANT tasks :

    org.apache.maven.plugins
    maven-antrun-plugin

    Deploying ${ear.file} …

    run