Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Friday, April 16, 2010

How To: Tomcat 6, BlazeDS 3.2, Hibernate 3.5 (JPA 2), Spring 3, Maven 2 – Part 1

In the following series of articles I want to explore how to set up a RIA project based on the latest stable Java and Flex stack using Maven and Eclipse. One would expect it to be easy enough and it is, but as always evil is in the details.

Prerequisites: recent version of Eclipse(3.4, 3.5) and m2eclipse plugin from SonaType.

Creating Web Project in Eclipse with Maven Dependency Management

In one of my previous posts I have tried to express my love for Maven. Using it from command prompt is a pleasure: quickly generating projects from templates(archetypes), building, running test, packaging and deploying, all is good. These are repetitive steps that need to be automated and maven handles that quite good. But we also need our old good IDEs with all the features we are used to have and got addicted to. So we need to look at our project from several different perspectives. To do that we need to set our project so that Maven and Eclipse understand the format/structure.

There are two ways to go: create Dynamic Web Project in Eclipse and enable Maven Dependency Management in it or create new Maven Project and enable Dynamic Web Module Facet in it.

First approach is more appropriate when you have some project in progress and want to start managing its dependencies with Maven. You can accomplish this from Project context menu/Maven/Enable Dependency Management. But it is not enough, you will have to modify folder structure according to established Maven project structure standards. Your sources should be in src/main/java and web content should be in src/main/webapp. Then you will have to edit some configuration files. For example you need to edit wb-resource parameters in org.eclipse.wst.common.component file located in .settings folder, and some more.

It looks like too much manual modification of Eclipse managed and hidden(by default) settings files and that is why I prefer the second way (with some maven plug-in help of course). Here is a step by step explanation:

1. Create new Maven Project. Check “Create a simple maven project(skip archetype selection)” option and set packaging to WAR. You have a working maven project structure. No we would like to work with the project as if it is native eclipse dynamic web project.

2. But first you should modify your pom.xml file to enable Java version 1.5 (or 1.6, what ever you need). It is necessary,  because  by default Maven compiles java code as Java 1.4. We also need this configuration for the next step. Just add the following xml in you pom.xml file:

<build> 
<plugins> 
<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-compiler-plugin</artifactId> 
<configuration> 
<source>1.5</source> 
<target>1.5</target> 
</configuration> 
</plugin> 
</plugins> 
</build>

It may look like too much custom xml, but after you work with maven for some time those plugin/dependency/groupId/artifactId elements just become part of your programming lexicon and m2eclipse plug-in with its code completion really helps here.

3. Now from Project context menu select Run As/Maven build… with the following options:

Goals = eclipse:eclipse

Parameter Name = wtpversion, Parameter Value = 2.0

After you run this maven build you get standard Eclipse Dynamic Web Project with Java Facet and Dynamic Web Module Facet enabled. Check it out from Project Properties / Project Facets section. Now you can configure Targeted Runtime (Tomcat 6 in our case) and other possible configurations you need just the same way you did it before maven era.

In Part2 we will explore how to further edit pom.xml file and add BlazeDS support using excellent Spring framework.

Friday, December 11, 2009

Would You Consider Moving from .Net to Java?

First, I should clarify that I am a long time .Net developer with quite a few high grade projects behind. And I am in fact still heavily using excellent .Net Framework. There is certainly place for both of them, no flame war intended. But you know, everyone has a favorite one. With this post I start a series of articles in which I will try to explore some really cool Java technologies, which not so long time ago made Java my framework of choice. The primary target audience are those .Net developers who started to have that strange feeling that there should be something more than standard .Net framework and Visual Studio with all its conveniences. I strongly encourage you to try examples by hand and feel that excitement of learning something new. Some of you might argue that there ARE many exciting development done in .Net as well. Although I might agree, we have to admit that almost any .Net related open source project (and open source IS the driving force of innovation) is in some respect a reflection of the same project started years ago in Java or some other alternative technology like RoR. Anyway, lets start and you decide on yourself.

Everything started with Apache Maven

I remember my first acquaintance with Maven. My reaction was: “I want this tool in .Net !”. I was so much excited with it that I have spent couple of days playing with it and searching for .Net equivalent. Without any success I started to consider building one. How naive I was! First let me explain what is all the fuss about.

Lets imagine we are about to build a web application. How do you set your project/solution/workspace? What is your directory structure? Where do you put source files, resources, tests, configuration files, etc? How do you add/resolve references/dependencies to external assemblies/libraries?  What about different versions of those libraries? Hopefully you have some templates setup in Visual Studio to reuse them later. But are you consistent from project to project? Are your different developer teams consistent with each other? It is quite a long list of questions and we are just about to create our project. Well, Maven is the tool from Apache Foundation to answer all of these and more. But you have to try it yourself to see all the possibilities and flexibility it provides.

Lets install Maven first and try some examples. I expect you already have a recent JDK installed on your system. Now go to Maven web site download latest release and follow simple steps to install it (unzipping and PATH environment variable settings). Now create some working folder for our examples , open command prompt in it and execute the following:

1: >mvn archetype:generate

You will see lots of text as an output. First it may confuse you, but explore it a little bit. You will realize that Maven gave us a list of project types (archetypes in Maven terminology). Some keywords in the list may sound familiar to you, struts (one of the first web frameworks in Java world) or maybe spring (popular IoC container with support for integration with many other technologies). Lets just choose #18 (in your case number may vary) which is maven-archetype-webapp. Now Maven will prompt you for groupId, artifactId and version. These are the properties which make any module/library/assembly unique. For this example we can enter something like com.tsvayer, web-app,1.0 accordingly. For further prompts simply accept default values.

What we got as a result is a web application template with standard folder structure for our source files, standard Java web application folders like WEB-INF,  tests etc. Now execute the following two commands:

1: >cd web-app
2: >mvn tomcat:run

You will see lots of text output and Maven will download some files. Simply ignore it for now. After you see this last message “INFO: Starting Coyote HTTP/1.1 on http-8080” open your browser and direct to http://localhost:8080/web-app. You should see running Hello World web application. Cool.

Now stop it pressing CTRL-C and execute the following command:

1: >mvn jetty:run

Again you will see lots of output and some downloads and after “Started Jetty Server” browse to http://localhost:8080/web-app

Lets stop and analyse what we have done so far. Before this Maven thing all we had is JDK installed. Then we installed some little tool called Maven, executed few commands and got a working web application hosted on a real Web Server. Wait, on two different real web servers, that is Tomcat and Jetty (in fact, you can try others as well, just run mvn jboss:run to run under JBoss, but be prepared, it will download lots of Mbs). Some of you may notice similarity with Ubuntu/Debian linux apt-get utility.

Now execute the following command:

1: >mvn package

After some more strange output you can find a target folder created and web-app.war file in it. What we have done is building our application, running unit tests on it(notice No Tests to Run info messages in the output) and packaging it in a standard Java WAR archive ready to be deployed on a production web server.

Just think about it, it is quite a lot for a few commands. By simply typing a few commands you get the whole application stack downloaded from internet with right external dependencies with right versions, all ready to develop, build, test, run and deploy ANY kind of application in ANY kind of hosting environment. Just imagine how easy it is now to have a standard development environment in your team, standard way of structuring your applications, standard way of building/packaging and deploying. In case you are not excited as much as I am, remember the day a new junior developer has joined your team with a fresh installed machine and how frustrating it was to set that machine to the same “comfortable” level as yours. Another question to consider: “Are you afraid to format you machine?” :)

To conclude this part, I just want to introduce you one more Maven command. Maybe some of you are not so much comfortable with command prompt and maybe you even say: “Man, wasn’t it supposed to eas the whole development process? Will I have to develop my web application from command prompt?”. As an answer for this, just execute the following command:

1: >mvn eclipse:eclipse

And if you have an Eclipse IDE installed on your system, your project is ready to be imported. Now you can enjoy all the conveniences of modern IDE. All of the commands we just learned above are available through mouse clicks.

“But wait, I heard IntelliJ IDEA is the best Java IDE out there. I want that instead“. Just execute the following:

1: >mvn idea:idea

and you are ready to go. In fact, all the modern IDEs support Maven generated projects directly.

In conclusion I want to clarify why building such a tool in .Net won’t be so much a cool idea as it is in Java. The reason is open source community. Just google how many libraries of any kind out there in Java world freely available to you and then you start to understand how invaluable Maven is. Just go to Apache Foundation web site and see it. In contrary, what we have in .Net world? Well, you answer that. Although I see certain benefits of using such a tool internally for in house built assemblies, and all that version conflict resolutions etc, the real benefit of Maven is in the availability of a central repository available over internet anytime anywhere.