Get UML class diagrams generation integrated to your maven build.

Came across an extremely poorly coded Java module having 0% KT or documentation in my current engagement. There has been no test cases to begin with, really difficulty to even understand where to start.

“Why not try reverse engineering”, a quick idea, and things started getting fair. 🙂

Here is quick way (maven builds) to get class diagrams generated along with java docs:

1. Download and Install Graphviz from http://www.graphviz.org/Download.php/

2. Add  GRAPHVIZ_HOME environment variable.

3. Add following plugin to your maven POM:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-javadoc-plugin</artifactId>
	<version>2.8.1</version>
	<configuration>
		<doclet>org.umlgraph.doclet.UmlGraphDoc</doclet>
		<docletArtifact>
			<groupId>org.umlgraph</groupId>
			<artifactId>doclet</artifactId>
			<version>5.1</version>
		</docletArtifact>
		<additionalparam>-views</additionalparam>
		<useStandardDocletOptions>true</useStandardDocletOptions>
	</configuration>
</plugin>

Detailed list of additional params can be found at http://www.umlgraph.org/doc/cd-opt.html, customize it based on your need.

4. mvn javadoc:javadoc will get the docs and class diagrams generated.

Continue exploring more and keep posted.

Links:

http://maven.apache.org/plugins/maven-javadoc-plugin/examples/alternate-doclet.html

http://www.umlgraph.org/doc/cd-umldoc.html

 

 

 

 

 

 

Creating executable jar using Maven and System dependencies

I recently came across a project setup which required me to explore little more bout maven plug-ins. The situation is characterized with:
1. Project had dependencies on 3rd party library that doesn’t already have a pom.xml file defined.
2. Project need to be packaged as an executable jar with dependencies as library which also required manifest customization.

Maven’s System dependency scope can be used to our rescue from situation described in task #1, though using the System scope is discouraged. And here I’m also going to demonstrate how using System dependencies resulted in increased implications for task #2.

All you need is to add a dependency with scope system and path to that jar as depicted below, better if you can keep it under src/main/ so that it will be available with your code and not everyone who want to build your code have to search for those 3rd party jar. However this approach will not not solve dependency resolution problem if you are going to have this project as a dependency in any other project.

		<dependency>
			<groupId>anything you wish</groupId>
			<artifactId>anything you wish</artifactId>
			<version>anything you wish</version>
			<scope>system</scope>
			<systemPath>${basedir}/src/main/lib/sonic_XA.jar</systemPath>
                        <!-- ${basedir} signifies here the project base directory. -->
		</dependency>

Now let’s talk bout task #2, defining an executable JAR file involves the following steps:
a. Define a main class in your JAR’s MANIFEST.MF file that defines the executable class.
b. Find all of the libraries on which your project depends.
c. Include these libraries in your MANIFEST.MF file so that your application classes can find them.

The maven-jar-plugin can be used to modify the contents of a default MANIFEST.MF file, a sample configuration:

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-jar-plugin</artifactId>
		<version>2.3.1</version>
		<configuration>
			<archive>
				<manifest>
					<!-- adds a Class-Path element to the MANIFEST.MF file, and includes 
						all dependencies in that Class-Path -->
					<addClasspath>true</addClasspath>
					<!-- location where all dependencies will be kept -->
					<classpathPrefix>lib/</classpathPrefix>
					<!-- executable class that contains main method -->
					<mainClass>com.sushantworld.core.Loader</mainClass>
				</manifest>
			</archive>
		</configuration>
	</plugin>

But as I was using System dependencies (those must be provided by env. and that was not a case for me), and hence we needed a more customized MANIFEST.MF and INDEX.LIST files so that we can include those 3rd party jar in the classpath, hence configured the maven-jar-plugin to use your custom MANIFEST.MF and INDEX.LISTfiles, with manually added system dependencies:

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-jar-plugin</artifactId>
		<version>2.3.1</version>
		<configuration>
			<useDefaultManifestFile>true</useDefaultManifestFile>
			<archive>
				<index>false</index>
				<manifest>
					<addClasspath>true</addClasspath>
					<classpathPrefix>lib/</classpathPrefix>
					<mainClass>com.sushantworld.core.Loader</mainClass>
				</manifest>
			</archive>
		</configuration>
	</plugin>

Now all you need to do use copy-dependencies goal of maven-dependency-plugin to copy your dependencies to the directory of your choice (in our case, lib directory as configured to classpathPrefix in maven-jar-plugin) under the build directory.

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-dependency-plugin</artifactId>
		<version>2.1</version>
		<executions>
			<execution>
				<id>copy</id>
				<phase>install</phase>
				<goals>
					<goal>copy-dependencies</goal>
				</goals>
				<configuration>
					<outputDirectory>
						${project.build.directory}/lib
					</outputDirectory>
				</configuration>
			</execution>
		</executions>
	</plugin>

Now after successful building, you can launch the application with a simple command:

java -jar jarfilename.jar

Links:
All about jar

Note: I do not suggest use of System scope for dependencies unless provided by the environment at runtime.

Getting started with maven2

As I prefer maven as build management tool, I thought to provide a get started article for those who are willing to learn it. I am not going to reinvent the wheel but evaluate available online resources. And maven3 is around the corner and much hyped but I’ll start with maven2 and will be posting comparative analysis between maven2 and maven3 sometime later.
Maven 2 can be download form http://maven.apache.org/index.html and setup only requires to extract the zip file and add maven bin directory to your path environment variable for windows.
If you are behind a firewall you can edit settings.xml provided under conf folder, all you have to do is add/uncomment <proxy> tag under <proxies> and provide host IP, and port and other details based on proxy settings for your network.
After doing the setup you may go through maven-in-five-minutes which provides you good information to begin with.

After having little insight, if you are ready for more comprehensive details concerning the actions you have just performed. Check out the Maven Getting Started Guide.

To make life easier for open source developers there is an plugin named m2eclipse which can be integrated with you Eclipse IDE. The update site for the plugin is http://download.eclipse.org/technology/m2e/releases and complete instruction to install plugin are provided here.

Video tutorial by http://www.sonatype.com provides you breakthrough to move full fledged development using maven and m2eclipse. You can visit all video tutorials here. And significant tutorials you can watch right here:

Creating a project:

Overview of m2eclipse:

Managing multi-module project:

I think that’s enough for the beginning, you can explore further as get comfortable with it.