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.

26 comments:

Numenor said...

Good topic, a practical information on why to choose between .NET and Java. While open source is encouraged in Java, open-source application in .NET is just written again(well a better version ofc) by Microsoft which kills the open source community of .NET.

Anonymous said...

As a Java developer I understand your exceitment about the Maven ;)
For the next topic of this kind you can try OSGi (Apache Felix, for instance). BTW, Maven 3 (which is a beta right now) is build on top of this technology.

Anonymous said...

As a java developer mavin is great and perhaps the best we've got for remote dependecy management. And yeah, when you are joing a new project or just checking out a new open source one, it's the cat's ass. However, maintaining a manven build file is as much fun as smashing your head into concrete.

Anonymous said...

Go Java!

Vitaliy Tsvayer said...

@ivanobulo,
To be honest, OSGi is one of the first technologies that really caches my attention to Java. We even researched on how it could be implemented in .Net. Unfortunately, concept of Class Loaders in Java is very limited in .Net and it is not possible to achieve the dynamic nature of OSGi in .Net (as assemblies cannot be unloaded once loaded in to memory, Domains can be used but you will need serialization for inter domain communication). Tutorials of OSGi (I have used Eclipse equinox) just blow my mind and it definitely one of those technologies that I would like to share with .Net community. By the way, Terracotta (especially its implementation of underline communication) is also one of those WOW moments in my Java journey. The list can go on...

Vitaliy Tsvayer said...

@Anonymous "However, maintaining a manven build file is as much fun as smashing your head into concrete."

Maintaining XML configuration files was always a problem, not specific to Maven. That is why google-guice takes another approach. Another significant example is Apache Camel with its DSL API. For Maven, you should try m2eclipse plugin, it is as simple as Add Reference in Visual Studio (even with much better search capabilities) and graphical representation of dependencies. Another option is IntelliJ IDEA, with its intellisense just everywhere, directly managing xml configuration files becomes not so much a problem.

Neill Alexander said...

Yet another option is NetBeans. Version 6.8 has just been released. It has fantastic Maven integration. No need for a mvn netbeans:netbeans command. Just point NetBeans at the directory containing your pom file and voilĂ , you are up and running.

Anonymous said...

I work at a place where we moved to maven. Have Eclipse and are trying Netbeans 6.8 thanks to maven process. Netbeans seems to be handling maven so much cleaner then eclipse. Since it does not generate files such as .project or .classpath. it uses the pom directly to understand the project. I think this is the best approach for supporting maven.

Vitaliy Tsvayer said...

Although I have very little experience with NetBeans, it has left very good impression on me (doesn't it provide Maven support since much earlier versions?). I am not sure if I get you right and what is exactly wrong with Maven support in Eclipse? Yes, it generates some Eclipse specific files and directories at the top level project directory (.settings, .classpath and .project, anything else?) but how does it interfere with Maven process? I have worked in a team with developers working on the same project but from different IDEs(Eclipse and IDEA) all managed by Maven without any significant annoyances. Competition between these 3 IDEs during the last years played its role and I do not see so much difference in functionality or level of comfort as before, although IDEA users might disagree :) Maybe one significant difference (at least important for me) is the lack of good support for Adobe Flex in NetBeans.

RogerV said...

There was also an article appearing in dzone not long ago on using Groovy to write Maven pom files. Yet another alternative to using XML syntax

Anonymous said...

So far Netbeans and Maven work so good together in my experience =)

Anonymous said...

I was a Java developer for many years, but am now using the .NET platform. I would not return to Java, as all the innovation is now occuring on the .NET platform not the Java platform which seems to have stagnated.

Vitaliy Tsvayer said...

@RogerV
Good point. DSL implementations in dynamic languages like Groovy is very appealing alternative to raw XML configuration files. More and more projects are moving this way.

Vitaliy Tsvayer said...

@Anonymous
"...as all the innovation is now occuring on the .NET platform..."

Well, then you should probably start a parallel series of posts to show just that.

Anonymous said...

Vitaliy your main focus is easy development and definitely .Net platform is the best. I used eclipse and NetBeans but in large projects you have great problems on handling your files. Second problem is a Java application has great memory problems. As a team we wrote three different web services that handles user actions sent from mobile phones. Two of them were written in Java, and the last one was written in .Net C#. Our team has qualified Java developers and they are new at C# but our java applications consumes more memory and fails frequently. Definitely the web service written in C# works better. You should consider the performance also.

Vitaliy Tsvayer said...

@Anonymous
You should define what you mean by "easy development". I remember how much infrastructure code, that is no business value code, we have written in .Net just because there is no choice (please note I am talking about middleware, on desktop .Net shines with availability of third party components like DevExpress). Please go to Apache Foundation and explore available solutions, those more or less define the current scope of Java world. What do you mean by "handling files"? When it comes to memory problems, I cannot comment on your specific case, but back in 2003-2004 we have implemented distributed system with some parts written in Java (mainly Web front, some middleware and public web services) and some parts in .Net (1.1 at that time) talking over web services. We did not have any memory related issues except one, which was specific commercial ADO.Net Provider purchased for specific Oracle functionality not provided by standard Oracle and Microsoft assemblies. And it leaked memory, leaked very bad.

Vitaliy Tsvayer said...

...and when it comes to performance, it is more often right architecture which defines performance than an underlying technology.

Anonymous said...

Maven is great. But why not http://www.gradle.org/ ?

More Java/Groovy build tools: http://karussell.wordpress.com/2009/09/29/evolution-of-build-systems/

Anonymous said...

I'm trying to switch from 5 years of .net to Java, too.
- Linux servers are free and better than windows servers.
- eclipse is free, vs.net is not (express version is a toy)
- there is no mature distrubuted caching soultion for free in .net (velocity is not released, sharedcache is not mature or featureful). java has terracotta, infinispan,..
- projects like hibernate, lucene are better in java, .net versions are following behind.
- link to sql and entity framework are failed projects, they can have more success by supporting nhibernate. you have sharding, lucene indexing and 2nd level caching support if you use hibernate.
....
and lots of other reasons,
regards, sirmak

Anonymous said...

What do you think about Scala ?

Vitaliy Tsvayer said...

@karussel
"But why not http://www.gradle.org/"

It looks like a viable alternative to XML. I am both hands up for DSLs. Although I understand that gradle is simply a groovy script and with IDE supporting groovy you automatically get some support for gradle, do you get code completion for available artifacts from Maven and other repositories?

Vitaliy Tsvayer said...

@Anonymous
"What do you think about Scala ?"

I am reading "Programming Scala" right now. I am at Chapter 6. So far, it looks like a great language for building DSLs. And of course being a SCALAble language, its support for concurrent programming, message passing, immutability, actors, good integration with Java, these are great notable features. But we have groovy, ruby, python, clojure, erlang, F#, boo... And we need all of them! We need them to learn and experiment. Don't get me wrong, these languages are absolutely production ready (as we can see from successful startups). I will take first opportunity to use one of them in the next project (I was very satisfied with Django in the past). Readability of the code is always my first priority where these languages shine. But I know developers, who switched to Ruby on Rails from Java/C#, learned lots of great staff and switched back to Java/C# writing code in absolutely new way. Main stream languages themselves start to get features of dynamic languages as well, just look at evolution of C#. I expect that in the future projects will be implemented in the mixture of static and dynamic languages. Last versions of java IDEs show just that, adding and improving support for more and more languages, giving support to mixture different languages in the same project etc...

Anonymous said...

@Vitaliy
Great post, thank you.

Anonymous said...

I switched from .net to java around 2004. I was happy with C# itself and also some of the frameworks built on top of it. But if something didnt interest microsoft then there were either no or too limited alternative options - often you just had to live without it. Take for instance refactoring. Beginning of the century it was the buzz but microsoft took years before they caught on to it - java devs had all the goodies but VS didnt have native support for it - some tried intellisense but it was too slow and limited to be usefull. Version control and build systems for big enterprise systems - hmm really didnt exist? And how about design patterns for enterprise systems? Yes you could download sample applications - you were never really sure if they were an example of "best practice" or someting the intern had flicked together. The samples didnt really address entrise stuff - they only showed one technology in isolation. Java books and websites about patterns were abundant. So how to build a layered app with MTS, ASP and everything else - go figure it out yourself - patterns were of no interest of microsoft. And then there whole community thing, stuff like lucene, junit, all the mvc frameworks, the 30+ ci tools, 40+ good caching solutions (distributed, server edge esi and many other flavors). Everytime I time I stepped outside the protective world of VS ASP.NET I hit the wall called "invent it yourself". C# keeps improving but it dosent look like its ever going to develop a community the size of java? The project im currently work on is a distributed system for a big insurrance company. Theres a healty mix of java, c#, mainframe and you name it. Every time the project desides to go with some internet standard rfc and all, the java devs have several options at hand commercial and open source, the c# guys however have been rolling their own frameworks again and again - I dont have anything against C# I just dont miss it.

Anonymous said...

Great post, exactly what I needed, because I too am planning to ditch .NET for an open source alternative (I absolutely agree with you that all the interesting stuff is coming from the open source world). Keep 'em coming!

Vitaliy Tsvayer said...

@Anonymouse
"I switched from .net to java around 2004..."

Exactly as you said, one of the biggest problems in .Net is "limited alternative options" and "invent it yourself". There is very little changed from VS 2003 to VS 2008, the only valuable option is the support for different .Net Frameworks, that is 2.0, 3.0, 3.5. For refactoring there is JetBrains ReSharper available since 2003.

On the other side, what Microsoft does it does it well and documents it very well. Visual Studio is VERY stable IDE (although with some SP installation annoyances). MSDN documentation is just excellent. I like .Net community, bu there is no Open Source .Net community, and that is a problem.

Post a Comment