Create a EJB3 WebService with Maven2 running on Glassfish

A year ago I posted how you could create a EJB3 webservice and deploy it on JBoss with Maven. Currently I am doing a similar thing at my project, but this time I deploy it to Glassfish. The creation of the web serive is similar but for the deployment you will need another plugin (if you want Maven to deploy the webservice, of course).
The ‘settings.xml’ file for Maven2 looks like:

<settings>
<profiles>
  <profile>
      <id>develop</id>
      <properties>
        <glassfish.home>/Applications/NetBeans/glassfish-v2ur2</glassfish.home>
         <hibernate.show_sql>true</hibernate.show_sql>
      </properties>
      <pluginRepositories>
        <pluginRepository>
            <id>maven.java.net</id>
            <name>Java.net Maven2 Repository</name>
            <url>http://download.java.net/maven/2</url>
        </pluginRepository>
      </pluginRepositories>
  </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>develop</activeProfile>
  </activeProfiles>
</settings>

Lateron we’ll see that Maven uses the Glassfish plugin and the property for the deployment of our application.
I have created a project ‘my-ejb-project’ with a simple EJB session bean. The bean (and it’s interface) looks like this:

package net.pascalalma.services;

import javax.ejb.Remote;

@Remote
public interface EchoService {

    public String sayHello(String subject);
}

and its implementation:

package net.pascalalma.services.impl;

import javax.ejb.Stateless;
import net.pascalalma.services.EchoService;

@Stateless
public class EchoServiceBean implements EchoService {

    public String sayHello(String name) {
        return "Hi " + name + "!";
    }
}

To build this project I have this in the project’s pom.xml file:

<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>net.pascalalma</groupId>
    <artifactId>my-ejb-project</artifactId>
    <packaging>ejb</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>my-ejb-project</name>
    <url>http://www.redstream.nl</url>
    <dependencies>
        <dependency>
            <groupId>glassfish</groupId>
            <artifactId>appserv-rt.jar</artifactId>
            <version>LATEST</version>
            <scope>system</scope>
            <systemPath>${glassfish.home}/lib/appserv-rt.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>glassfish</groupId>
            <artifactId>javaee.jar</artifactId>
            <version>LATEST</version>
            <scope>system</scope>
            <systemPath>${glassfish.home}/lib/javaee.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <finalName>my-ejb-project</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <configuration>
                    <ejbVersion>3.0</ejbVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

As you can see I am referring to the libraries of Glassfish of my local installation. It appears that the libraries you get from the global Maven repositories only contain the interfaces and no implementation so these can not be used when you want (unit) test your beans outside the container.

Besides this ejb-project I have also created a ear-project that is used to create an ear file containing the EJB’s jar file (and optionally other deliverables like war-file etc). The pom file of this project looks like (the project doesn’t contain anything else):

<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>net.pascalalma</groupId>
  <artifactId>my-ear-project</artifactId>
  <packaging>ear</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-ear-project</name>
  <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>net.pascalalma</groupId>
            <artifactId>my-ejb-project</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>ejb</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-ear-plugin</artifactId>
                <configuration>
                    <displayName>My Service</displayName>
                    <description>My EJB Component for Echo Service</description>
                    <version>1.4</version>
                    <modules>
                        <ejbModule>
                            <groupId>net.pascalalma</groupId>
                            <artifactId>my-ejb-project</artifactId>
                        </ejbModule>
                    </modules>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.glassfish.maven.plugin</groupId>
                <artifactId>maven-glassfish-plugin</artifactId>
                <version>2.1</version>

                <configuration>
                    <glassfishDirectory>${glassfish.home}</glassfishDirectory>
                    <user>admin</user>
                    <passwordFile>${glassfish.home}/domains/domain1/config/domain-passwords</passwordFile>
                    <autoCreate>false</autoCreate>
                    <debug>true</debug>
                    <echo>true</echo>
                    <terse>false</terse>
                    <domain>
                        <name>domain1</name>
                        <httpPort>8080</httpPort>
                        <adminPort>4848</adminPort>
                        <reuse>true</reuse>
                    </domain>
                    <components>
                        <component>
                            <name>${project.artifactId}</name>
                            <artifact>${project.build.directory}/${project.build.finalName}.ear</artifact>
                        </component>
                    </components>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

With all this in place we are able to build the EJB3 project with the command:
mvn clean install
and to package and deploy the EAR project with:
mvn clean package glassfish:deploy
In the Admin Console of Glasfish you will see the deployed project if everything went well:

To make our EJB3 bean available as a webservice, we have to add some annotations to the source, so the code of the interface and the bean will become something like this:

package net.pascalalma.services;

import javax.ejb.Remote;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@Remote
@WebService
@SOAPBinding(style = javax.jws.soap.SOAPBinding.Style.DOCUMENT
            ,use = javax.jws.soap.SOAPBinding.Use.LITERAL
            ,parameterStyle = javax.jws.soap.SOAPBinding.ParameterStyle.WRAPPED)
public interface EchoService {

    public String sayHello(String subject);
}

and the implementation class:

package net.pascalalma.services.impl;

import javax.ejb.Stateless;
import javax.jws.WebService;
import net.pascalalma.services.EchoService;

@Stateless
@WebService(endpointInterface = "net.pascalalma.services.EchoService",serviceName="EchoService")
public class EchoServiceBean implements EchoService {

    public String sayHello(String name) {
        return "Hi " + name + "!";
    }
}

That’s it! Now rebuild and redeploy the packages and it is done!
To test the webservice I ‘ll use the Admin Console of Glassfish again. You will see under the heading ‘Web Services’ that there is added a web service:

Now select that web service and you will get a screen where you can enter the Test page for this service:

After clicking the button we can enter a value as input parameter for our service:

And the Admin Console will show the result of our call by showing the soap messages (request+ response):

Of course, this test functionality is much more limited then we have in SoapUI but to quickly check if the service is running it will do just fine.

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in EJB3, Web Service and tagged , , . Bookmark the permalink.

3 Responses to Create a EJB3 WebService with Maven2 running on Glassfish

  1. glassfish newcomer says:

    Thanks for the example.I’m just starting to learn ejb and also glassfish.Wondering how to integrate all this with maven.You save me a few hours configuring
    😀

  2. glassfish newcomer says:

    warning for vista users,
    add this to your host file
    127.0.0.1 localhost

    C:WindowsSystem32driversetchost

    it seem that maven glassfish plugin automatically deploy to localhost(not 127.0.0.1) but since vista didn’t map localhost to 127.0.0.1 by default,the deployment will fail silently and you will be wondering for hours what have you done wrong as I did :p

  3. Pascal Alma says:

    Hi Glassfish newcomer,
    I am glad I could help and thank you for your Vista tip. Luckily I have replaced my windows machine with a MacBook a few months ago, a very wise choice 🙂

Comments are closed.