Setting up a Java REST Service in Eclipse with Maven
Information on how to migrate Java Web projects to maven.
Prepare
cd {EclipseWorkspace}
mvn archetype:generate -DgroupId=ch.htwchur.sii.rest -DartifactId=HelloRest -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
cd HelloRest
mvn eclipse:eclipse -Dwtpversion=2.0
Import the project in Eclipse: File -> New… -> Java Project: Project Name: HelloRest
mvn eclipse:clean
HelloRest -> F5
HelloRest -> Configure -> Convert to Maven Project…
HelloRest -> Maven -> Update Project…
Configure Maven
pom.xml:
- add dependency: jersey-bundle
- add property: project.build.sourceEncoding UTF-8
- specifiy jvm version:
<build>
....
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
HelloRest -> Maven -> Update Project…
HelloRest -> Properties -> Project Facets -> Java -> Change version: 1.7
Code
- Create the folder
src/main/java
- Create a package in
src/main/java
, e.g.: ch.htwchur.sii.rest - Add a new Class: e.g.: HelloWorldService
package ch.htwchur.sii.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/hello")
public class HelloWorldService {
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Jersey say : " + msg;
return Response.status(200).entity(output).build();`
}
}
Register
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>`Restful Web Application`</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>ch.htwchur.sii.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Build
HelloRest -> Run: Maven Build: package
Check target/.../WEB-INF/...
contains jersey-bundle jar
Run
Servers -> doubleclick Tomcat -> Tab: Modules -> Add external web module:
- Document base: W:\EclipseWorkspace\HelloRest\target\HelloRest
- Path: HelloRest
Fire up: http://localhost:8088/HelloRest/rest/hello/world
Run “Maven build: package” again after changes to the code, the web app will be reloaded automatically.
Resources
- http://javaposts.wordpress.com/2012/01/14/maven-rest-jersey-eclipse-tutorial/
- http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/
Converting a Java REST Service in Eclipse to use Maven and deploy to the package
Prerequisites
- Update Eclipse and installed plugins
- install m2e-wtp from http://download.eclipse.org/m2e-wtp/releases/juno/
- open view: maven repositories. Sometimes, downloading all packages to the local folder takes some time
Convert
Sample Project: jeremia
Configure -> Convert to Maven Project:
- Group Id:com.weblyzard
- Artifact Id:jeremia
- Packaging: war
- Name: Jeremia
Configure Maven
Add the following entries to pom.xml to:
- change type to web app
- define jre/jdk v.7
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
run
mvn clean eclipse:eclipse
refresh the project
Reorganize the code
add folders
- src/main/java
- src/main/resources
- src/test/java
- src/test/resources
Maven -> Update Project
- Move source code files from src/com… to src/main/java
- Move all Files containing org.junit to src/test/java
- Move properties and config files to …/resources
Prepare for deployment
Add to pom.xml:
<repositories>
<repository>
<id>code.semanticlab.net</id>
<url>[http://code.semanticlab.net/deploy/](http://code.semanticlab.net/deploy/)</url>
</repository>
</repositories>
Add dependencies
- junit
- apache jcs (without the pom)
- stanford-postagger-models (manually in xml)
Run tests
Try to run the tests both ways:
- run test package as junit
- run as maven build… with goal: package
the tests’ setups are different. So if something fails, see Maven#Troubleshooting
Deploy
Once you run maven build package, there might still be some warnings:
- add missing versions by running: mvn help:effective-pom
- add default log4j.properties
- …?
Add the wagon to pom.xml
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-webdav-jackrabbit</artifactId>
<version>1.0-beta-7</version>
</extension>
</extensions>
...
run
mvn deploy:deploy-file -DpomFile=pom.xml -Dfile=target\jeremia-0.0.1-SNAPSHOT.war -DrepositoryId=code.semanticlab.net -Durl=
dav:http://code.semanticlab.net/deploy/
Hint
To deploy on Windows to a passwordprotected url consider:
- credentials set in [user folder]\.m2\settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>code.semanticlab.net</id>
<username>alibaba</username>
<password>sesam open up</password>
</server>
</servers>
</settings>
- java_home set to a up-to-date Java Version (i.e. JDK 1.7)
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_25
- then just run:
mvn deploy
Troubleshooting
sometimes when resources in packages i.e. com.weblyzard…splitter.data wont get loaded:
- maven clean
- project clean
- run tests as junit
Publish Libraries to Maven Repository
- To publish a third party library to a repository, the following command can be used:
mvn
deploy:deploy-file -DgroupId=
- Example: Deploy the jar-file stanford-parser.jar to the semanticlab maven repository
mvn deploy:deploy-file -DgroupId=com.weblyzard.thirdparty -DartifactId=stanford-parser -Dversion=3.3.1 -Dpackaging=jar -Dfile=stanford-parser.jar
-DrepositoryId=code.semanticlab.net -Durl=
dav:http://code.semanticlab.net/deploy/
Resources
- http://rolfje.wordpress.com/2011/01/29/your-maven-java-web-project-in-eclipse-wtp/
- http://stackoverflow.com/questions/4123044/maven-3-warnings-about-build-plugins-plugin-version
- http://www.mkyong.com/maven/maven-3-need-to-know-the-plugin-version/
- http://stackoverflow.com/questions/4342245/disable-maven-warning-message-selected-war-files-include-a-web-inf-web-xml-wh
- http://pastebin.com/JH6BHAd8
- http://www.mkyong.com/webservices/jax-rs/classnotfoundexception-com-sun-jersey-spi-container-servlet-servletcontainer/
- http://stackoverflow.com/questions/8444107/classnotfoundexception-when-starting-tomcat
<dependency>
<groupId>`com.sun.jersey`</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.13</version>
</dependency>` `
- http://stackoverflow.com/questions/2229757/maven-add-a-dependency-to-a-jar-by-relative-path/2230464#2230464
- http://mail-archives.apache.org/mod_mbox/maven-users/201103.mbox/%3CAANLkTikz9rQ5_R8EmWtUXJGUYsj6rvVwCMAf+1O+1SoM@mail.gmail.com%3E
- http://docs.codehaus.org/display/MAVENUSER/Deploying+3rd+Party+Jars+With+WebDAV
- http://stackoverflow.com/questions/6871494/mvn-deployfile-to-different-repositories-for-snapshot-and-release-version
- http://stackoverflow.com/questions/4538180/deployment-issue-with-maven-plugin
Leave a comment