GlassFish v2 provides support for remote deployment of application. In fact, you have two options for deploying applications remotely
- GlassFish admin web application
- 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.
[xml]
<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>
[/xml]
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:
[code]
AS_ADMIN_PASSWORD=mysupersescretpassword
[/code]
Deploying the Application
Now that we have the environment setup, we can cover the steps for deploying the application.
[xml]
<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>
[/xml]
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:
[xml]
<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>
[/xml]
That’s it. Good luck with your application deployments!
simple and best examples.