Sunday, April 24, 2016

Starting Java web development with Maven and Spring

When beginner asks seasoned Java developer about Java web development you will get a lot of positive feedback. Great mature tools and framework. Big community. Great job opportunities. Cross platform support. One plus after another.

 But want you won't hear is that starting can be pretty hard if you want to do it right.

"How would I start building a simple web application?" you would ask.

"Simple." for some reason it always starts with simple. "Just use maven to create an webapp project. Set up Spring and other dependencies in pom. Import it in [insert his/hers favorite IDE here] and just start coding.

If you are a person completely new to this you can spend weeks reading documentation, trying and failing until you manage to set up everything together before you can even get to a part you were interested in the first place. Coding the actual application.

I know this because I was the one doing the asking years ago. And this was the answer I got. The answer people still get. And they get it because after spending a lot of time learning all these things and building a lot of project we simply forget that all of that looks alien to somebody new.

A former colleague asked me how to set up a simple java web application. And his reason is not because he wants build a web application. Its because he wants to practice writing test. He is a QA engineer and a rather good one, but he didn't spent years doing Java web development.

I decided to try to answer a question in a different way. By creating a fully setup boilerplate application that he can compile and deploy right away. That way he has something that is already working and can just start adding to without too much trouble. Later as he learns he can start to replace bit by bit until he knows how to do all by himself.

In this blog I will give a short introduction to spring demo, a java boilerplate application that has spring, spring security, junit and logging set up with maven and that should be a good starting point for beginners. You can download the application on my github repo here.

This post assumes you have good Java knowledge. If you don't I recommend one of many Java book. This application might be little to advances for you if you never coded in Java.

This post assumes you have java and maven and a java web server installed on your machine. For web server I recommend Apache Tomcat because it is simple to set up and use.

If you miss any of these things you can download them from the internet and find instructions how to set them up.

How to get the application to Work


Once you download it open the console and go to the root folder, the one that contains the pom.xml file. This file is used by maven to build your application and set up all dependencies. More on that later. Once you are in the root folder enter maven clean install. This will create a target folder that will contain springdemo.war file. This is your application. Deploy it on your server, google how for the server you have. Start the server and go to http://localhost:8080/springdemo/ with your browser. You should see a login page.

Explaining the application structure


Application follows the standard maven structure. You can read more about it and how to use maven on official maven site. Pom file contain spring, spring security, junit and logging dependencies. It has also stuff needed for Eclipse, IDE I use, but you should be able to use an IDE with it.

Application has Spring context file in /src/main/resources/, test context file in /src/test/resource/, servlet context file and spring security setup file in /src/main/webapp/WEB-INF/. If you are unfamiliar with spring, spring security and junit go to official sites. I may write some posts on each but adding explanations here will make this post too long and will dilute the purpose of making explanation simple. Tests are in /src/test/java/ and application code is in /src/main/java/.

If you don't want to bother to much you can just add application code to /src/main/java/ updated the /src/main/resources/applicationContext.xml with your new classes and update /src/main/webapp/WEB-INF/aplicationContext-security.xml for new pages you added.

Final notes


This boilerplate app is just a starting point. It is meant to save you time in the beginning and get you to coding part faster. In order to get where you want to go you will have to possess some knowledge about java web programming, spring, spring security and other technologies you will use.

I hope this application will help people get to java development with spring and maven faster. If you have, question, comments, complaints, suggestion please leave them in the comment section.

Happy coding.

No comments:

Post a Comment