Remote Deployments of WAR files with GlassFish v2

GlassFish v2 provides support for remote deployment of application. In fact, you have two options for deploying applications remotely

  1. GlassFish admin web application
  2. GlassFish asadmin console application

This blog post will focus on option 2, GlassFish asadmin console application. I prefer this approach since I can create a script that will automatically deploy the application. There is no requirement for me to click thru a web application interface. Simply create the WAR/EAR file and send it to the remote server. In my case, I’ll create an Ant script to automate this process.

Setting Up the Environment

You first have to set up your environment. We will use an Ant script to define the GlassFish Ant tasks.

<project name="glassfish-admin" default="list-components" basedir=".">

    <property name="asinstall.dir" location="c:/Sun/AppServer" />

    <property name="admin.user" value="admin" />
    <property name="password.file" location="password.txt" />

    <property name="admin.host" value="127.0.0.1" />
    <property name="admin.port" value="4848" />

    <property name="appname" value="KillerApp" />
    <property name="war.filename" value="${appname}.war" />

    <path id="glassfish.class.path">
        <fileset dir="${asinstall.dir}/lib" includes="**/*.jar"/>
    </path>

    <target name="sun-taskdef" description="Defines taskdefs for sun-appserv commands">
        <taskdef name="sun-appserv-deploy"
                classname="org.apache.tools.ant.taskdefs.optional.sun.appserv.DeployTask"
                classpath="${asinstall.dir}/lib/sun-appserv-ant.jar" />

        <taskdef name="sun-appserv-undeploy"
                classname="org.apache.tools.ant.taskdefs.optional.sun.appserv.UndeployTask"
                classpath="${asinstall.dir}/lib/sun-appserv-ant.jar" />

    </target>

     ...
</project>

On line 3, we define the directory of our GlassFish installation. You should update this accordingly for your environment.

Lines 5-6, define the admin user name and password file. I’ll come back to the password file later.

Line 8-9, define the name of the remote server and the port. In this example, the server is localhost (127.0.0.1) but you can change this to any server on your network.

Lines 11-12, define the name of your application and the application file name.

Lines 18-27, define the GlassFish admin Ant tasks. In this example, we are defining the undeploy and deploy tasks. There are others available, see this reference for details.

The Password File

The password file must have the following format:

AS_ADMIN_PASSWORD=mysupersescretpassword

Deploying the Application

Now that we have the environment setup, we can cover the steps for deploying the application.

<project name="glassfish-admin" default="list-components" basedir=".">

    <!--  ... definitions from above section: setup environment ... -->

    <target name="deploy" depends="sun-taskdef" description="Perform remote deploy only to dev.">

        <sun-appserv-deploy
             file="target/${war.filename}"
             force="true"
             upload="true"
             user="admin"
             passwordfile="${password.file}"
             host="${admin.host}"
             port="${admin.port}"
         />

    </target>
</project>

Lines 7-15 perform the deployment of our application. This is accomplished with the sun-appserv-deploy task. The attributes for this task are self-explanatory. Once the task is complete, then the application is deployed to the remote server

Undeploying the Application

You can use the sun-appserv-undeploy task to undeploy the application. The following code fragment undeploys the application:

    <target name="undeploy" depends="sun-taskdef" description="Perform remote undeploy to localhost">
        <sun-appserv-undeploy
             name="${appname}"
             user="admin"
             passwordfile="${password.file}"
             host="${admin.host}"
             port="${admin.port}"
         />
    </target>

That’s it. Good luck with your application deployments!

Did you like my blog post?

Signup now and receive an email when I publish new luv2code content.

I agree to have my personal information transfered to AWeber ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

1 Comment

  1. Comments  raj   |  Thursday, 12 September 2013 at 3:57 pm

    simple and best examples.

Leave a Reply