In this section, we will compile, package, and deploy the JAX-RS application jboss-jaxrs
with Maven. A pom.xml
file gets created when a Java EE Web Project is created. In the pom.xml
file, the jboss-jaxrs
Artifact ID is specified with packaging as war
. The JAX-RS API is provided by WildFly 8.1:
<dependency> <groupId>org.jboss.spec.javax.ws.rs</groupId> <artifactId>jboss-jaxrs-api_1.1_spec</artifactId> <version>1.0.1.Final</version> <scope>provided</scope> </dependency>
As we are using the Jersey JAX-RS RI, include the dependencies for Jersey, which are available in the Group ID com.sun.jersey
:
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18</version> </dependency>
Add the Maven Compiler plugin and the Maven WAR plugin and specify the output directory as the deployments
directory of WildFly 8.1 installation. The pom.xml
file is listed as follows:
<?xml version="1.0" encoding="UTF-8"?> <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>org.jboss.jaxrs</groupId> <artifactId>jboss-jaxrs</artifactId> <version>1.0.0</version> <packaging>war</packaging> <name>WildFly JAX-RS</name> <description>A starter Java EE 7 webapp project for use on JBoss WildFly / WildFly, generated from the jboss-javaee6-webapp archetype</description> <url>http://wildfly.org</url> <properties> <!-- Explicitly declaring the source encoding eliminates the following message: --> <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- JBoss dependency versions --> <version.wildfly.maven.plugin>1.0.2.Final </version.wildfly.maven.plugin> <!-- Define the version of the JBoss BOMs we want to import to specify tested stacks. --> <version.jboss.bom>8.1.0.Final</version.jboss.bom> <version.arquillian.container>8.1.0.Final </version.arquillian.container> <!-- other plugin versions --> <version.compiler.plugin>3.1</version.compiler.plugin> <version.war.plugin>2.1.1</version.war.plugin> <!-- maven-compiler-plugin --> <maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.source>1.7</maven.compiler.source> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.wildfly.bom</groupId> <artifactId>jboss-javaee-7.0-with-tools</artifactId> <version>${version.jboss.bom}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-web-7.0</artifactId> <version>1.0.0.Final</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- First declare the APIs we depend on and need for compilation. All of them are provided by JBoss WildFly --> <!-- Import the CDI API, we use provided scope as the API is included in JBoss WildFly --> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <scope>provided</scope> </dependency> <!-- Import the Common Annotations API (JSR-250), we use provided scope as the API is included in JBoss WildFly --> <dependency> <groupId>org.jboss.spec.javax.annotation</groupId> <artifactId>jboss-annotations-api_1.2_spec</artifactId> <scope>provided</scope> </dependency> <!-- Import the JAX-RS API, we use provided scope as the API is included in JBoss WildFly --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.jboss.spec.javax.ws.rs</groupId> <artifactId>jboss-jaxrs-api_1.1_spec</artifactId> <version>1.0.1.Final</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18</version> </dependency> </dependencies> <build> <!-- Maven will append the version to the finalName (which is the name given to the generated war, and hence the context root) --> <finalName>${project.artifactId}</finalName> <pluginManagement> <plugins> <!-- Compiler plugin enforces Java 1.6 compatibility and activates annotation processors --> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.jvnet.jax-ws-commons</groupId> <artifactId>jaxws-maven-plugin</artifactId> <versionRange>[2.2,)</versionRange> <goals> <goal>wsimport</goal> </goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${version.compiler.plugin}</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>${version.war.plugin}</version> <configuration> <outputDirectory>C:wildfly-8.1.0.Finalstandalonedeployments</outputDirectory> <!-- Java EE 7 doesn't require web.xml, Maven needs to catch up! --> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <!-- The WildFly plugin deploys your war to a local WildFly container --> <!-- To use, run: mvn package wildfly:deploy --> <plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-maven-plugin</artifactId> <version>${version.wildfly.maven.plugin}</version> </plugin> <plugin> <groupId>org.jvnet.jax-ws-commons</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <id>HelloWorldService</id> <phase>compile</phase> <goals> <goal>wsgen</goal> </goals> <configuration> <sei>org.jboss.jaxws.service.HelloWorld</sei> <genwsdl>true</genwsdl> <servicename>HelloWorldService</servicename> <keep>true</keep> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.jvnet.jax-ws-commons</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>2.3</version> <executions> <execution> <id>HelloWorldService</id> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <configuration> <packagename>org.jboss.jaxws.service</packagename> <target>2.0</target> <keep>true</keep> </configuration> <dependencies> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-tools</artifactId> <version>2.2.8</version> <exclusions> <exclusion> <groupId>org.jvnet.staxex</groupId> <artifactId>stax-ex</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.jvnet.staxex</groupId> <artifactId>stax-ex</artifactId> <version>1.7.6</version> <exclusions> <exclusion> <groupId>javax.xml.stream</groupId> <artifactId>stax-api</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </plugin> </plugins> </pluginManagement> </build> </project>
Before we install the jboss-jaxrs
application, delete org.jboss.jaxrs.rest.HelloRESTApplication.java
, which gets created automatically. Also, delete the resources and test directories. The directory structure of the jboss-jaxrs
application is shown here:
Right-click on pom.xml
and select Run As | Maven install, as shown here:
The jboss-jaxrs
application gets compiled, packaged into jboss-jaxrs.war
and outputs to the deployments
directory. Maven install outputs the message BUILD SUCCESS
, as shown here:
Start the WildFly 8.1 server if not already started. By default, the root resource classes are scanned for in the WEB-INF/lib
and WEB-INF/classes
directories. The org.jboss.jaxrs.rest.HelloWorldResource
root resource class gets found. The jboss-jaxrs.war
application gets deployed to the server. Now, log in to the WildFly 8.1 Administration console and click on Manage Deployments. The jboss-jaxrs.war
file should be listed as deployed, which is shown as follows: