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

Protobuf for lazy Java coders.

Sometimes you are just plain lazy and would rather have Maven create the Java files out of .proto. The alternative would have been to download the protoc, fire up the terminal and issue one simple command. Not a big deal at all. But as I said sometimes you are just plain lazy.

So how do you go about it. Follow the steps given below.

Think up a super critical message that you want to send across over the wire.
If you dont know how to write a .proto file, perhaps one of the links below would help.
https://developers.google.com/protocol-buffers/docs/proto
https://developers.google.com/protocol-buffers/docs/proto3
The one below is the super critical hello world that I want to send across.

#1

Create a vanilla maven project. 
Say thanks to open source and add protoc-jar-maven-plugin to the project. 

#2

This would create the java files.
However they will complain that the google libraries are not around. 
So you will have to add the protobuf libraries. 

#3

The proof of the pudding is in eating it. 
Let's write a test to serialize and deserialize the super critical message. 

#4

That's it. You have a Java project that does the basic steps i.e. serialization / deserialization using protobuf. Hope that is helpful. If you know a easier, quicker way please share as well. 

Either ways your comments will be appreciated. Please take a couple of minutes and write back. 

Maven Tips

I am a bit of a Maven fanboy. Actually I am more of a - use anything available to make your task easier, smoother and more efficient - person.  And Maven has fit the bill perfectly for near about 3 - 4 years now.

I had written earlier about Maven here. In a very recent conversation around standardization of toolkit for software project / product lifecycle, I realized that Maven was perhaps one of the lowest common denominators to start from (speaking of Java world). I simultaneously - as I was rattling off the virtues of adopting Maven enterprise wide, to the audience - realized that I had not really written enough about it. And hence this article.

With something as widely used as Maven, I don't see much reason to write, yet again, about how to set up, how to use etc. You will find almost everything very neatly described in the Maven website. I have found loads of very good articles on Maven at this link as well. However, I will share a couple of tips here.

Tip 1
The beauty of Maven (and indeed most of the Java based software) is that the installation is too simple. You just put the jar in a folder of your choice and the software just works. Most of the default configurations are good enough for first use. However, there is one bit that I suggest you change. Change the localRepository - the folder where Maven would download dependencies for your code from internet and store in your harddisk - to something that suits you. I like to put it in a drive that is not my primary drive i.e. not the drive in which I have Windows installed. So, in case the windows dies on me, or something happens, this folder with all the dependencies have a better chance of being retained. As you write code, this dependencies folder tends to grow and should you lose it you will have to spend considerable amount of time and bandwidth to download all the stuff.  So, all that I suggest you do is add the following configuration to the default configuration.

File: \apache-maven-3.0.4\conf\settings.xml
<localRepository>D:/mavenrepo</localRepository>

Tip 2
I dislike polluting (some people will disagree that this is "polluting") my system settings with product specific stuff e.g. JAVA_HOME etc. I have always found it much easier to use a simple batch file instead. I use that with Maven as well. I tend to have this (or some variation of this) batch file, which when run (after taking off the appropriate REMs) will do the basic stuff e.g. creating a java application, creating a web application, running them, executing a specific unit tests etc. These instructions are easily available, but I personally found it useful to have it handy and ready.
ECHO OFF 

REM =============================
REM Set the env. variables. 
REM =============================
SET PATH=%PATH%;C:\ProgramFiles\SpringSource\apache-maven-3.0.4\bin;
SET JAVA_HOME=C:\ProgramFiles\Java\jdk1.7.0_09

REM =============================
REM Check the versions. 
REM =============================
REM mvn --version

REM =============================
REM Get a list of all template projects. 
REM =============================
REM mvn archetype:generate

REM =============================
REM Standalone java application. 
REM =============================
call mvn archetype:generate ^
    -DarchetypeArtifactId=maven-archetype-quickstart ^
    -DinteractiveMode=false ^
    -DgroupId=foo.bar ^
    -DartifactId=corejava

 
REM =============================
REM Standalone java web application. 
REM =============================
REM call mvn archetype:generate ^
REM     -DarchetypeArtifactId=maven-archetype-webapp ^
REM     -DinteractiveMode=false ^
REM     -DgroupId=foo.bar ^
REM     -DartifactId=web001


REM =============================
REM Run the main class. 
REM =============================
REM mvn exec:java -Dexec.mainClass="foo.bar.Main"
REM mvn exec:java -Dexec.mainClass="foo.bar.Main" -Dexec.args="arg0 arg1 arg2" 

REM =============================
REM Run unit tests. 
REM =============================
REM mvn test
REM mvn -Dtest=myUnitTest test

pause

This is all that I had time for today, but I will come back and add more tips to this page.

If you want to get in touch, you can look me up at Linkedin or Google+ .

Move to Maven 3.0.4

Have you updated to Maven 3.0.4 yet? 1.1 It's been available in Maven Central since 17-Jan-2012.

I have been using Maven for ages now. I was running on 3.0.3 till last week. Being a Maven evangelist it is unlike of me to have not moved onto the latest version for so long. But I did linger on with 3.0.3 longer than I was comfortable. And the reason was whenever I tried to move to 3.0.4 it will simply not download the dependencies, while working behind proxy. I would get a long list of warnings and errors starting with the following.

Failed to retrieve plugin descriptor …

This was irritating to say the least. This weekend I rolled up my sleeves and set out to find what I was doing wrong. Turned out my beloved Maven was doing it wrong this time – and not me for a change, which is good. Apparently they did not add a jar that is required for working behind proxy [JIRA].

So, what do we do to fix it? Like most of the thing with Maven, it is quite simple. Download the requisite jar. Copy it at [your directory structure]\apache-maven-3.0.4\lib\ext. And that's it. You are all set. Start using Maven the way you have always done.

A humble request to Maven guys – we all know what an awesome job you have done and continue to do. Guess it is only normal to have a couple of bugs like this slip through. However, it has been there for six months now. Fix it please. I have been a passionate evangelist of this software and it helps my job if things like this do not linger on.

Suggested further reading ...

  1. How Maven enforces good habits

If you want to get in touch, you can look me up at Linkedin or Google + .