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.
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.