A programmer's tale

Wednesday, January 17, 2007

Making with Maven2 machinery | Part1: configuration & making executable jar

I've mentioned in my previous blog that Maven2 is my favorite build tool. I love it because of it's
object oriented approach in building projects; better to say it's philosophy to build projects. Moreover Maven2 is not only just a simple build tool, it's more than that. I will discuss them one by one in my blog.
Now the "getting started", "5 minutes tutorials", "basic examples" etc. all are available in maven site or some of the blogs. but I have not fount any link that describes full build configuration of Maven2, working examples that are really useful in production environment. I will try to give you some tips which can be helpful for the people who build projects for living.
Well, I'll not going to give you any introduction of Maven2; "what maven is", "what can be done with Maven2" etc. In first part, I will discuss about maven2 configuration and creating jar, executable jar with Maven2.

Tip1: Maven2 configuration :
Settings and configuration of Maven2 can be customized "globally" in "setting.xml" file in your M2_HOME\conf directory. Here "globally" has special meaning that the setting can be changed "locally" in your project module that override the "global" setting. Now I often confused with the local repository. If you are the first time Maven2 user, then you may face the difficulty to find the local repository. Maven2 makes the local repository in your home directory by default e.i. in Windows operation system it is under "C:\Documents and Settings\" directory. If you see the "settings.xml" file, you will find that the default value of this is: ~/.m2/repository. Hence Maven will make .m2\repository in your home directory. But sometimes this makes problems. I think it is better to set the local repository some other drive than C: such as D:\DEV\m2\repository. It's more convenient to view all the jars here rather than default directory.
How to do that?
Just add the following tag just below the default repository tag:

<!-- My local repository -->
<localrepository>D:/DEV/m2/repository</localrepository>

and make sure that the other repository tag/tags disabled.

similarly you can set the proxy setting, server and profiles. the best way to change some setting is to copy the whole tag, paste bellow the original, change as necessary and then comment out the original. In this way if you make anything wrong, then you can revert back the thing. For example you want to change the proxy setting, now search for under tag, copy the whole tag as:

    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>proxyuser</username>
        <password>proxypass</password>
        <host>proxy.host.net</host>
        <port>80</port>
        <nonproxyhosts>local.net,some.host.com</nonproxyhosts>
    </proxy>

just below the original one. Now change the values like:

    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>username</username>
        <password>password</password>
        <host>host IP</host>
        <port>port</port>
        <nonproxyhosts>local.net,some.host.com</nonproxyhosts>
    </proxy>

make sure the unnecessary part is commented like:

<!--
    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>proxyuser</username>
        <password>proxypass</password>
        <host>proxy.host.net</host>
        <port>80</port>
        <nonproxyhosts>local.net,some.host.com</nonproxyhosts>
    </proxy>
-->

Tip2: Making executable jar without dependencies:
It is very simple tip for creating executable jar for your project. But there is a drawback. It can't make executable jar for your project if your project depends upon other jar/jars. It can be done other way. I will discuss that in another blog entry.

Well, the main thing you need is "maven-jar-plugin" and you will have to configure this plug in properly. you will have to mention the main class for your projects i.e. the class that contains the main method like:

    <plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-jar-plugin</artifactid>
        <configuration>
        <archive>
        <manifest>
        <mainclass>com.yourcompany.pack.Main</mainclass>
        <addclasspath>true</addclasspath>
        </manifest>
        </archive>
        </configuration>
    </plugin>

If you don't need the executable jar, then you don't need the above mentioned plug in anymore, just don't include the plug in.

Here are some nice links for Mave2:
1. Maven2 home.
2. Free book: Better build with Maven
3. IBM developerworks: Introduction to Apache Maven2

2 Comments:

  • Who knows where to download XRumer 5.0 Palladium?
    Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

    By Anonymous Anonymous, at 1:51 AM  

  • [url=http://kfarbair.com][img]http://www.kfarbair.com/_images/_photos/photo_big8.jpg[/img][/url]

    בית מלון [url=http://www.kfarbair.com]כפר בעיר[/url] - אינטימיות, [url=http://kfarbair.com/services.html]שקט[/url] . אנו מספקים שירותי אירוח מגוונים כמו כן יש במקום שירות חדרים הכולל [url=http://www.kfarbair.com/eng/index.html]סעודות רומנטיות[/url] במחירים מיוחדים אשר מוגשות ישירות לחדרכם...

    לפרטים אנא גשו לאתרנו - [url=http://kfarbair.com]כפר בעיר[/url] [url=http://www.kfarbair.com/contact.html][img]http://www.kfarbair.com/_images/apixel.gif[/img][/url]

    By Anonymous Anonymous, at 11:37 PM  

Post a Comment

<< Home