Thursday, April 12, 2012

Working with .NET

I have been working with Java most of my professional life, and I have to say that not always has it been easy. In fact, most of the times it was rather difficult to build up knowledge, find and use dependencies, write unit tests and apply correct patterns.

A few days ago, I needed to build an application in .NET as we needed to print and process some Word documents. I have been using C# before but it is not my area of expertise. Obviously, I needed to refresh my knowledge and build an application that can perform well. I was simply amazed how easy was it to pick up basics, start development in Visual Studio and create variations of the application be it, console, form or web. It took mere hours to understand basic concepts, get to know Visual Studio and to be productive. For Java, to achieve the same for the person coming from .NET would take days to achieve same results (think Swing)! I am not a big fan of Microsoft products (being able to run only on Windows machines) but BRAVO Microsoft for making development so easy! It is no wonder that this platform is so widely accepted.

Java development tools are getting there, but having so much diversity can be good and bad at the same time and we will just have to wait and see. Until then, listen to this song (my friend sent this to me) We Code Hard :).

Wednesday, March 07, 2012

Moving from Ant to Maven


I have to admit that I have not used Maven a lot until perhaps mid last year, when I realised how difficult it is becoming to maintain dependencies and builds for J2EE projects using Ant. Many of my colleagues were faced with same problems but most of them still choose to stay with Ant.

Ant was here, it seems, forever. It is well established and mature product and almost all developers understand it and know how to finish the job with it. On the other hand, when Maven came on the first look it seems that developers could not define explicitly what needs to happen when building the project (or at least this was my experience). Everything was implicit with an option to override the default settings. This was a confusing part opposing to the Ant where you needed to define everything explicitly. Not all understood what a project lifecycle was and why it was important. What Maven is today is described here, "What is Maven".  In simple words, these are few benefits that Maven provided to me:
  • Default setting out of the box with need to change build and deployment setting where needed
  • Dependency management, where if I need one dependency, Maven will automatically include all other needed dependency files and will exclude conflicted dependencies
  • Ability to define which dependencies are used for deployment and which are used for test (for example, you need to start in memory database for unit tests and do not want to deploy those jar files to production)
  • If we want to change version of the main dependency, all other relative dependencies will be updated automatically
  • Standardizes project builds through build lifecycle
  • Allows easy separation of code and unit tests

Maven does a lot more, but if we take only items listed, it will be clear how much more your life will become easier. One of the major problems in switching to Maven is how we are thinking about referenced projects (for example if we have dependencies on other projects in the workspace). Maven will not support this (there are workarounds but the only proper way is to create local repository). Maven needs to work with artifacts (basically versions of jar files). To achieve this, all you need to do is to setup in-house Maven repository (for example Sonatype Nexus or Artifactory from JFrog Artifactory) for local files and proxy repositories that are on the Internet. This will enable you to have you own repository with your artifacts (libraries). Many developers will usually avoid this (versioning) as when including them with it would mean that every time they change library project updates with dependencies will need to happen (for every library). On the other hand, working with same version would help avoid updating references. Developers would usually track libraries in the version control systems (or similar) where files are always given the same version under a different release. This, of course, has to change if we want to use Maven, and we need to start versioning the libraries and then uploading them to a local Maven repository. Then we can start including those dependencies in our project, but not as a referenced projects, but as a Maven dependencies. In the case that we need a quick build of the library, we can always install it in our local repository (in Win 7 \Users\User\.m2). This way, it will become available to be included in a project that needs this dependency and later on, it can be published in centralized repository for the rest of the team. This, of course, requires change of culture in the organization. In the end, it pays off big time.

One of the other problems developers are faced with is setting up local environment when starting a new development and making it easy to start being productive (business-wise) and not spending time resolving technical issues with environments and dependencies. Maven does this in an easy way. It not only saves you trouble of uploading you jar files to CVS or SVN, it has different plugins (like Eclipse plugin) that can start projects very easily and where it will setup dependencies from repository based on what is in the pom.xml file. Try it one, and you will never go back to Ant.

So basically, for Maven to work correctly, we need correct directory structure and correctly setup pom.xml file. For directory structure, you can reference this Maven directory layout and for POM setup, this Maven POM. Please note that POM can be simple as just as adding a few lines to describe your artifact and group. (This is like the project name and the package). If you have, for example, m2e plugin for Eclipse, you can see an example of Effective POM that will have a lot of information that you can use to customize your POM. It is up to you how much customization you want or need.

In my experience, even though Maven has an Ant plugin, mixing two of them is not a good idea as it might interfere with Maven build lifecycle (Maven build lifecycle).

And for the end, just to mention that Maven helps with one of the biggest problems that developers have when debugging the code. What happens when the source code from the 3rd party libraries is missing? Maven gets this for you automatically. That means that you can now see what you are debugging when going in depth to figure out you problems.

Please note that this text was written in very simple terms and if you would like to fully understand how Maven works, please refer to Maven site for further documentation. This article is not comparison of Ant vs Maven, but rather it reflects on benefits of having Maven builds in your environment. All projects can be built with both Maven and Ant.

Tuesday, February 07, 2012

Spring and JiBX

When I first heard about JiBX I could not have know that this would be one of the greatest tools for processing XML files that I would use.

It all started when we had a project where a speed and flexibility of XML processing was required and we started of with using XMLBeans as this was already packed with Weblogic server. A little did we know that JiBX will beat XMLBeans in almost every aspect. We had about 100 schemas to process with average of 100 tags and tons of imports. XMLBeans compiled 1 schema for almost a minute and created file that was more that 1MB. JiBX compiled all schemas in a little more than a minute. One jar file was packed at 10 times less space than XMLBeans. When thinking about loading all those files into memory, you will end up using quite a bit of PermGen space. Don't get me wrong, I still use XMLBeans today for some tasks, but I believe that if you can choose a tool for XML processing, choose JiBX and you will not be sorry.

To start off, you would create an XML file (do not forget namespace as this will be used as a package in the jar file) as this is easier than creating XSD from scratch. After that, you can use XMLBeans inst2xsd to convert it to XSD.

..\inst2xsd -design rd -enumerations never -outPrefix test test.xml

When choosing design, you might want to experiment with options. Russian Doll Design is simplest one, so you might want to start with that until you understand how types and elements are used not just in regards to XSD but also how is this going to be reflected in your classes. Turn off enumerations as this will mess up the file. You can add them later manually.

At this point you can copy this to your JiBX directory and create a custom XML (using <custom> - optional) if you have to specifically process some elements and leave others out (cool feature if you need to keep the size of your jar file down, especially if a lot of types and imports are used). One of the advantages of the JiBX is it's high speed and adaptability. For example, if you wanted to pass some custom date in XML, you would need to use string type. But not with JiBX. You can specify how you want to serialize/deserialize the elements when binding them. This is generated by either using predefined classes or creating your own  (you can do that by extending org.jibx.runtime.JodaConvert and then creating your Utility class). A very handy thing to use to adapt to different environments and clients.

JiBX comes with both ANT and MAVEN support and it is very easy to setup. You can find a lot of examples on JiBX site.

When integrating with Spring, it is extremely simple and you can use it to custom build your beans, web services or anything that deals with XML. Spring comes with a lot of support for different Object/XML Mapping and you can read about it here.

Thursday, January 26, 2012

Representing Java classes in diagrams

You probably wanted to display relationships from the Java classes in diagram if you needed to understand how the projects and files are related to each other. Eclipse can show you Call and Type hierarchy, but seeing it all in one graph can help you understand how project is structured. I have created the diagram for the docx4j using small application (using Apache BCEL 5.2) to create it (to import it I converted CSV file to XLSX and used yEd):


Application code that is creating CVS is this:

Once you have run this code, open file in Excel, save as XLSX and then open file in yEd. After that, choose Edge List and fill in your ranges. Import as Circular and then change Layout to Organic (I think that it is faster this way and it looks better). With graphs big as this one, some layouts will be extremely slow. What is useful in the end is that you can select neighboring nodes, and create new document out of that. That should give you a good overview of what your class is using or where it is being used.

Tuesday, January 24, 2012

Ruby log analyzer: rula

This would be my first Ruby application. Name of the application is rula, meaning RUby Log Analyzer. I started this project as I needed to do several searches on the log files that would include previous filters and that would not consume a lot of memory (I intended to run this on the server). I have submitted the code to Git, to location https://github.com/bcavlin/rula, so please feel free to check it out. Application is only working in console mode right now, but I intend to create curses support too. As far as the version, this would be version 0.1. Please feel free to develop it to suite your needs. Here is the image:

Friday, January 20, 2012

SyntaxHighlighter and blogger

If your blog uses SyntaxHighlighter from alexgorbatchev.com then setting it to display items correctly might require a small addition in styles. I needed this to make it look correct (additional style is to limit the length for mobile devices):

JSF, ICEfaces and resetting component values

I just spent some time trying to figure out how to reset the value of the HtmlSelectOneMenu when cancel button is clicked using ICEfaces. The solution is to add actionListener to your ice:commandButton (in this case Cancel) and to find any component that you want to reset. My reset code was: