This tutorial helps you on how to setup Eclipse for 3 tier application development. Let’s start with the assumptions we made, which are not that many. So let us see what you need and what we are trying to install/configure and so on :)Assumptions:
- Installed JDK (1.7.x)
- Windows 8
- Apache Tomcat 8
- Eclipse (Mars)
- Chrome 34.0.1847.92
- Maven 3.x
- Jersey 2.7
- Jackson 2.3.2
- Angular JS
- LiveReload
So just follow the step-by-step instructions below in order to install Maven on your machine:
- Download Maven from http://maven.apache.org/download.cgi (Binary zip)
- make sure “JAVA_HOME” variable is set as an environment variable
- extract Maven into a folder (note: the path to this folder must not contain white spaces and should not be too deep e.g. C:/../../../../Maven/)
- add “MAVEN_HOME” to your environment variables. It should point to your Maven directory
Although many of the plugins we need are already bundled in the JEE version, two are still missing.
The first is the Angular JS plugin that provides us an HTML editor which supports Angular JS expressions and directives. This great plugin was written by Angelo Zerr (Thank you very much!!!)
The second plugin we need is a LiveReload server, so that we don’t have to refresh our webpage by hand every time we change the code. Later in this tutorial we also will install the LiveRelod plugin for Chrome, which connects to this server and therefore gets notified whenever the sourcecode on our server has changed. Great tool which saves you a lot of time.
As already done at the Maven installation, you just follow those steps below in order to install and configure Eclipse, the Angular JS plugin and the LiveReload server.
- Download Eclipse Kepler JEE -> https://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplersr2
- Install Angular JS Plugin -> https://github.com/angelozerr/angularjs-eclipse // Update Site: http://oss.opensagres.fr/angularjs-eclipse/0.1.0/
- Install JBoss Tools Livereload -> Help -> Eclipse Marketplace -> Search for JBoss Tools -> Select distribution for Kepler -> deselect everything but livereload
- Go to your Eclipse installation directory -> open “eclipse.ini”-file -> Xms512m // Xmx1024m, add line -XX:MaxPermSize=1024m (otherwise eclipse will often fail on builds)
After setting up our IDE we need a server on wich our logic shall run later on. I decided to use Apache Tomcat, as it is lightweight in the first place, but can be extended to a full blown JEE Application Server if needed (http://tomee.apache.org/apache-tomee.html).
- Setup Apache Tomcat
- In Eclipse -> Window -> Preferences -> Server -> Runtime Environments -> Add
- Choose Apache Tomcat v7.0 -> Next
- Choose a Name
- Click “Downlad and Install…” and choose a directory where you want to install tomcat -> Finish
As already mentioned above, we install the LiveReload plugin for our browser (in my case Chrome) for a more seamless development cycle regarding GUI development. What does that mean? Well in fact that you always will see the results of what you’re coding with nearly no delay. When changing the code of your webapp in eclipse, the IDE will recognize that you’ve just changed something. It will then hot redeploy your new code to the server (Apache Tomcat). The LiveReload server that runs in parallel to Apache Tomcat recognizes this change and notifies the browser which shows your webapp and forces it to reload the page with your webapp. This whole process is a matter of a few seconds and therefore you will always see the changes you made immediately reflected in your browser.
- Install LiveReload Plugin for your Browser (e.g. for Chrome from Marketplace https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei )
Now that everything we need is installed, we have to configure some little things so that everything works perfectly together. For example: Maven does not always show all available artifacts when seraching for them at first. So we need to configure it’s behaviour regarding updating of its repositories. Just follow the instructions below and you should be fine. If your deeper into eclipse and those plugins you’re free to make your own preference settings 😉
- Configure Apache Tomcat, preferred Browser, Maven and LiveReload in Eclipse
- Window -> Preferences -> Server -> Runtime Environments -> Add
- Choose Apache Tomcat v7.0 -> Next
- Chose a Name -> Browse to your Tomcat installation directory -> Finish
- Window -> Show View -> Other -> Server ->Servers
- R-Click in Server View -> New ->Server -> Apache ->Tomcat v7.0 Server -> Finish
- R-Click in Sever View -> New -> Server -> Basic -> LiveReload Server -> Finish
- Window -> Preferences -> General -> Web Browser -> Check Default system web browser (i assume it’s chrome) (everytime you click “run on server” this browser gets opened)
- Window -> Preferences -> Maven -> check “Download Artifact Sources” and “Download Artifact JavaDoc” (if you have an already existing project: R-Click project -> Maven -> Download Sources/Download JavaDoc)
- Window -> Show View -> Other -> Maven -> Maven Repositories -> Global Repositories -> R-Click on central -> Update Index
- Window -> Preferences -> Maven -> Download repository index updates on startup
At first lets create and configure a new Dynamic WebProject in Eclipse. Afterwards I will give some explanations to regarding the project setup and using the different plugins.
- Create Dynamic Web Project with Angular Support, Maven Dependency Management, Jersey Support, Jackson Support
- Create Project
- Right Click -> New -> Other -> Web -> Dynamic Web Project
- Project name: de.uniaugsburg.mdsd.<your-project-structure>
- Target runtime Apache Tomcat v7.0 -> Finish
- Configure Angular JS Support
- R-Click Project -> Configure -> Convert to Angular JS Project
- R-Click Project ->Properties -> Validation -> HTMLSyntax -> Check “Enable Project specific Settings” -> Attributes -> Undefined Attribute name -> Ignore
- HTML/CSS/JS Files come into the WebContent Folder of the Project
- Configure Maven
- R-Click Project -> Configure -> Convert to Maven Project
- Configure Jersey and Jackson
- R-Click project -> Maven -> Add Dependency -> jersey-container-servlet / jackson-jaxrs-json-provider
- Copy config of jersey_web (see below) snippet into WEB-INF/web.xml and replace <package-where-your-services-are> to your respective packagename
- Create Project
- Run Dynamic Web Project
- R-Click on LiveReload Server -> Start
- R-Click on Project -> Run as… -> Run on server
- in Chrome type URL of your Webservice
- in Chrome enable LiveReload Plugin
- go out there and CODE 🙂
The server side logic normally consists of a bunch of webservices and a litle bit of business logic. So how do we create those webservices? Here, Jersey comes into play. We do nothing more than writing a POJO and annotate it with Jersey’s annotations in order to mark it as a Webservice:
1: @Path("/fooservice")
2: public class FooService {
3: @Path("/getsomefoo")
4: @Produces(MediaType.APPLICATION_JSON)
5: public Foo getSomeFoo() {
6: Foo foo = new Foo("fooname");
7: return foo;
8: }
9: }
That’s easy, isn’t it?
This is just a basic webapp layout and you are free to change it to fit your needs.One of the last questions I had when I sat up this IDE was: “how to manage my dependencies in the JavaScript part of the webapp?”. Originally, Maven was a tool that evolved strongly in the Java world and had less to nothing to do with JS. Therefore it’s not the first choice when it comes to managing dependencies in JS. Tools that are preferred here are e.g. Grunt. Nevertheless I wanted to use Maven for my dependency management in Java as well as in JS. Fortunatly I found the webjars project ( http://www.webjars.org/ ), that ideally fitted my needs. Now I can add libraries like angular.js, bootstrap and all the like just like normal Java jars. To see how to add those libraries to your webapp just follow the instructions below and add a web library of your choice.
- Add Javascript Libs
- R-Click Project -> Maven -> Add dependency -> Type Name (we work with org.webjars)
- in index.html add line with <script src=”webjars/<path/to/your/file.js>”></script>
- the path can be looked up when you go to JavaResources -> Libraries -> Maven Dependencies -> <Your Library> -> META-INF -> resources -> webjars -> <everything from here on is your path>
- Add Java Libs
- R-Click Project -> Maven -> Add dependency -> Type Name
- use it