Starting a Web Application
Please note this article is currently a work in progress.
When I building Java web applications I like to work with Eclipse and pretty use the same open source stack:
- Spring Core
- Spring Security
- Spring Web Flows
- JSP/Servlet API
Note: I am not settled on a persistance layer yet. Sometimes I use Hibernate, sometimes JDBC templates.
There are options out there like Spring Roo or Appfuse that generate you a base application using a stack similar to this.
My issue with these is more of a personal preference than a practical reason. I like wiring things together myself and seeing how things work because as an open source consultant in the Java space, I often have to trouble shoot things or set things up from scratch (Roo or Appfuse may not be an option in these situations). I am wary of becoming too reliant on tools and also a little resistant of the learning curve involved. Its a tough decision because I have played with both tools and they both seem to be useful. Perhaps at a later point I might switch to one of these. But until then here is my more manual approach.
Creating the inital structure with Maven
I use Maven to create my project structure and manage my dependancies. I'm not going to go through the maven set up here, this can be viewed on the Maven project page.
Instead I will review the Maven commands involved with generating a web application:
mvn archetype:create -DgroupId=com.mysite.myproject -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-webapp
This creates a folder structure that looks like the following:
- myproject
- src
- main
- resources
- webapp
- WEB-INF
- web.xml (application configuration here)
- index.jsp
- pom.xml (maven dependancies go here)
If you run 'mvn package' a folder called target will be generated at the main level of the project containing the war.
Another option for creating this structure is to use the m2eclipse plugin for eclipse. This plugin allows you to create maven projects and manage dependancies from with Eclipse. Using this allows you to skip import into Eclipse step coming up. It also saves you some typing and gives you the same result. This is the approach I usually take.
Here we can see picking the project type in the m2Eclipse new project wizard.
Here we can see configuring the project.
Setting Up Java and Test Directories
Next we need somewhere to add the Java code and then to create test cases for that code. Inside of the main folder I create two more folders. Java and Tests:
C:\mvn-test\myproject>cd src/main C:\mvn-test\myproject\src\main>mkdir java C:\mvn-test\myproject\src\main>mkdir tests
Now we can develop Java code in the java folder and Test Cases in test. To compile and package the application now we can run 'mvn install'. This will compile all the java code and also package up the war.
Update the POM file
Now we can add some dependancies and plugins to the pom.xml.
In the build section I like to add the following plugins. This allows for code to be compiled using Java 6 and the Cargo plugin deploys the war file to a running instance of Tomcat. I use this to deploy my application to my "production" server.
I also have a list of dependancies that I usually use in project based on similar tasks I almost always perform. I usually have to:
- Send Emails
- Connect to a DB (I mostly work with mySQL)
- Generate reports (always JasperReports)
- Spring Core and Spring Security
I have included a sample pom.xml with these configurations for reference. Obviously over time you would need to update the versions of these dependancies.
Add Ant
I usually like to add Ant to my projects. There are a few common tasks I do with Ant. But also you never know when it might come in handy.
Here are some of the more common tasks.
- Deploy my application locally (this is just a lot of deleting and copying of files)
- Change settings in properties/xml files before deployment
- Work with SCM
- Other random tasks that Ant is awesome for
Attached is an ant sample file and properties file that contains a few tasks I like to do.
Import the project into Eclipse
Finally the code can be imported into Eclipse.
If you have the m2Eclipse plugin you can import it using this, (and of course if you created it in Eclipse using the plugin there is no need to import).
Otherwise you can manually import the project using Eclipse's Import functionality.
If this is the first time you using Maven in Ecliplse and you don't have an Eclipse plugin installed you will need to let Eclipse know where the Maven repository is:
mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo
However, I reccomend using m2Eclipse as it greatly simplifies working with Maven project in the Eclipse IDE.
Conclusion
As I stated this is most likely a work in progress. Over time I am sure I will refine my process for creating Java applications, perhaps embracing something like Roo or Appfuse.Please continue to check the site for more articles or tutorials. Please send me comments or questions on this article here.
Useful links: JasperReports | iReport | Jasperserver