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

After 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/.


  4.0.0
  .uk.co.pookey.test
  hello-world
  war
  1.0-SNAPSHOT
  hello-world Maven Webapp
  http://maven.apache.org
  
    
      maven.java.net
      Java.net Maven2 Repository
      http://download.java.net/maven/2
    
  
  
    
      junit
      junit
      3.8.1
      test
    
  
  
    hello-world
    
      
        org.glassfish.maven.plugin
        maven-glassfish-plugin
        2.1
        
          admin
          password
          /opt/glassfishv3/
          
            
              ${project.artifactId}
              ${project.build.directory}/${project.build.finalName}.war
            
          
          
            ${project.artifactId}
            4848
            8080
            8443
          
        
      
    
  

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/

4 thoughts on “Getting started with Glassfish using Maven”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.