Getting started with Glassfish using Maven
In this post I detail simple instructions for installing Glassfish v3 onto Ubuntu (although it will work just as well with Redhat, debian, and anything else). I also show how to create and deploy a simple WAR using Maven.
Installing software
Start by checking that the correct packages are installed - for distro's other than Ubuntu you will need to modify this command:
# aptitude install sun-java6-jdk maven2
Download glassfish into your opt directory. Get the non-platform specific ZIP file. uncompress the zip within your /opt/ directory, which should then decompress to /opt/glassfishv3
Start the glassfish application server like this:
# /opt/glassfishv3/bin/asadmin start-domainAfter that, you can access the glassfish control panel by going to: http://www.your-server.com:4848/
Creating your Java Project
To create a project, use maven as shown below. For more info on using maven, have a look at my previous posts, Getting started with Maven and Spring and Hibernate Tutorial part 4 - annotations and maven. Running just the below command will generate a 'hello world' JSP example, that will work for this example without modification.
$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp \ -DgroupId=.uk.co.pookey.test -DartifactId=hello-world -Dversion=1.0-SNAPSHOT
Modify your pom.xml along the lines of the below. This will allow the use of the glassfish plugin which is from https://maven-glassfish-plugin.dev.java.net/.
<?xml version="1.0"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>.uk.co.pookey.test</groupId> <artifactId>hello-world</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>hello-world Maven Webapp</name> <url>http://maven.apache.org</url> <pluginRepositories> <pluginRepository> <id>maven.java.net</id> <name>Java.net Maven2 Repository</name> <url>http://download.java.net/maven/2</url> </pluginRepository> </pluginRepositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>hello-world</finalName> <plugins> <plugin> <groupId>org.glassfish.maven.plugin</groupId> <artifactId>maven-glassfish-plugin</artifactId> <version>2.1</version> <configuration> <user>admin</user> <adminPassword>password</adminPassword> <glassfishDirectory>/opt/glassfishv3/</glassfishDirectory> <components> <component> <name>${project.artifactId}</name> <artifact>${project.build.directory}/${project.build.finalName}.war</artifact> </component> </components> <domain> <name>${project.artifactId}</name> <adminPort>4848</adminPort> <httpPort>8080</httpPort> <httpsPort>8443</httpsPort> </domain> </configuration> </plugin> </plugins> </build> </project>
Building and deploying
The following commands are reasonably self describing - glassfish:deploy will deploy a WAR, and glassfish:redeploy will redeploy it - as trying to deploy over an existing application will fail.
$ mvn war:war glassfish:deploy $ mvn war:war glassfish:redeploy
You can then access your application by going to http://www.your-server.com:8080/hello-world/