Deploying a maven project


I have a maven project and I'd like to create a distribution of it with the dependencies. I've tried the maven-assembly-plugin and built the jar with dependencies, but that unpacked all of the jars and repackaged them all into a big, single jar. What I'd like is something like my jar file and a lib folder that has all of the dependencies. Then when I run it, I could run "java -cp lib/* my.package.MainClass".

What's the best way to go about doing this with maven? Or the recommended way to deploy?



Best Solution

I have used the Maven assembly just for that in my project.

First enable your plugin in your POM and call your assembly config :

            <!--I recommend 2.1 as later versions have a  bug that may
                                Duplicate files in your archive
            <!--Executes the packaging along with the mvn package phase
                    <!--Relative path to your descriptor -->

Then in your descriptor you can decide how you want your layout to be before you package the whole thing

    <!-- this will create an extra resource, you can
         choose jar as well in the format-->
    <!-- Insert here extra files as configs or, batch files, resources, docs etc-->
            <!-- I like to integrate the jre as well... simplifies my deployement -->
    <!-- This will scrub your dependencies and add them to your lib folder, I excluded
         Test stuff as it is not needed, could have declared the resource as a test
         only phase as well would not have had to exclude it here

This will create a zip file with the layout you have specified in your output directory config, package the whole thing as a zip file (you can choose zip, jar, war ...) and deploy it in my repository with the rest.

I skipped bits and pieces to make it simpler but my package expands to include batch files, dlls, config, doc and the JRE so everything needed is in the same zip... all is needed to run the thing is extract and click start.bat !

I could also probably make it in to a jar properly formatted with METADATA and just double click the jar itself to start it all, I did not need or have time to toy around this option but you may try it as well.

Beware of versions above 2.1 of the assembly plugin, it will create duplicate entries if your directives enable it to find the same file in different locations, this will give you a lib folder with the same jars repeating twice. not very dangerous as unzipping will collapse them but still annoying to have the unzip ask you if you want to overwrite files. Plus the fact that you do not know which won if somehow they turned out to be different in content.

Maven is great but I find that it is sometimes frustrating to get it working, Plus documentation can sometimes be hard to find and use. However, used appropriately it will save you tons of time.

good luck