Deploying a maven project

deploymentmaven-2

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?

thanks,

Jeff

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 :

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <!--I recommend 2.1 as later versions have a  bug that may
                                Duplicate files in your archive
                            -->
                            <version>2.1</version>
            <!--Executes the packaging along with the mvn package phase
                            -->
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptors>
                    <!--Relative path to your descriptor -->
                    <descriptor>src/main/assembly/package.xml
                        </descriptor>
                </descriptors>
            </configuration>
        </plugin>

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

<assembly>
    <!-- this will create an extra resource project-1.1.1-package.zip, you can
         choose jar as well in the format-->
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <!-- Insert here extra files as configs or, batch files, resources, docs etc-->
    <fileSets>
        <fileSet>
            <directory>src/main/assembly/files</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/conf/*.*</include>
                <include>**/doc/*.*</include>
            </includes>
        </fileSet>
            <!-- I like to integrate the jre as well... simplifies my deployement -->
        <fileSet>
            <directory>target/jre</directory>
            <outputDirectory>/jre</outputDirectory> 
        </fileSet>
    </fileSets>
    <!-- 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
    -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>          
            <excludes>
                <exclude>junit:junit</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

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