Tuesday, May 23, 2017

JavaEE: TomEE and TomEE Maven Plugin

TomEE makes JavaEE development much easier.  On this post, I will show a simple and concise pom.xml file for TomEE configuration.

On a previous post, I included a javaee-api to write a simple RESTful web service.  Instead of including individual dependency, we can include a tomee-embeded, which includes all jars included in the TomEE server.

TomEE maven plugin is also included here to run an application with a maven command.


<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/xsd/maven-4.0.0.xsd">
 
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.jihwan.jee</groupId>
   <artifactId>demo</artifactId>
   <version>0.1-SNAPSHOT</version>
   <packaging>war</packaging>
 
   <properties>
     <tomee.version>7.0.2</tomee.version>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <failOnMissingWebXml>false</failOnMissingWebXml>
     <maven.compiler.target>1.8</maven.compiler.target>
     <maven.compiler.source>1.8</maven.compiler.source>
   </properties>
 
   <dependencies>
      <dependency>
         <groupId>org.apache.tomee</groupId>
         <artifactId>tomee-embedded</artifactId>
         <version>${tomee.version}</version>
         <scope>provided</scope>
      </dependency>
    </dependencies>
    
    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomee.maven</groupId>
        <artifactId>tomee-maven-plugin</artifactId>
        <version>${tomee.version}</version>
        <configuration>
          <tomeeVersion>${tomee.version}</tomeeVersion>
          <tomeeClassifier>plus</tomeeClassifier>
        </configuration>
      </plugin>
    </plugins>
    </build>
</project>

After changing the pom.xml from the previous post, we can run the following maven command to run the maven plugin TomEE server.

mvn clean install tomee:run

Let's look at some logs shown later parts of the logs.

1
2
3
4
5
INFO [localhost-startStop-1] org.apache.openejb.server.cxf.rs.CxfRsHttpListener.logEndpoints REST Application: http://localhost:8080/demo-0.1-SNAPSHOT/       -> org.apache.openejb.server.rest.InternalApplication@1abb025c
INFO [localhost-startStop-1] org.apache.openejb.server.cxf.rs.CxfRsHttpListener.logEndpoints      Service URI: http://localhost:8080/demo-0.1-SNAPSHOT/hello  -> Pojo work.rest.HelloService
INFO [localhost-startStop-1] org.apache.openejb.server.cxf.rs.CxfRsHttpListener.logEndpoints               GET http://localhost:8080/demo-0.1-SNAPSHOT/hello/ ->      String message()
INFO [localhost-startStop-1] sun.reflect.DelegatingMethodAccessorImpl.invoke Deployment of web application archive /Users/jihwan/workspace/TomEE/demo/target/apache-tomee/webapps/demo-0.1-SNAPSHOT.war has finished in 106 ms
INFO [main] sun.reflect.DelegatingMethodAccessorImpl.invoke Server startup in 819 ms

As shown on the line 4 and 5, the deployment is done in 106 ms and the server started in 819 ms. Simple and fast!
TomEE also shows available service URIs as shown on the line 3.  If you are a careful reader, you may notice that the URI is http://localhost:8080/demo-0.1-SNAPSHOT/hello instead of http://localhost:8080/demo/hello 


Having a project version in your context path is very annoying.  To avoid this, we need to define a custom context path in an <webapp> tag under the <configuration> of the tomee-maven-plugin. : This is a way of using the tomee maven plugin configuration, but it has a limitation, which will be addressed on another post.

   <plugin>
      <groupId>org.apache.tomee.maven</groupId>
      <artifactId>tomee-maven-plugin</artifactId>
      <version>${tomee.version}</version>
      <configuration>
         <tomeeVersion>${tomee.version}</tomeeVersion>
         <tomeeClassifier>plus</tomeeClassifier>
         <webapps>
            <webapp>${project.groupId}:${project.artifactId}:${project.version}?name=${project.artifactId}</webapp> 
         </webapps>
      </configuration>
   </plugin>


No comments:

Post a Comment

Java 9: Flow - Reactive Programming

Programming world has always been changed fast enough and many programming / design paradigms have been introduced such as object oriented p...