Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

thanks,

Jeff

like image 698
Jeff Storey Avatar asked Jul 02 '09 00:07

Jeff Storey


People also ask

How do I deploy a Maven project to production?

xml , create a tag in the VCS, promote the versions to a new SNAPSHOT (e.g. 1.1-SNAPSHOT) in the pom. xml , commit the new pom. xml in the VCS. The entire process require some work but this can be automated using the Maven Release Plugin.

What is deploying in Maven?

deploy:deploy is used to automatically install the artifact, its pom and the attached artifacts produced by a particular project. Most if not all of the information related to the deployment is stored in the project's pom. deploy:deploy-file is used to install a single artifact along with its pom.

How do I run Maven deploy?

If you want to execute a specific execution from the command line, you'll need mvn deploy:deploy-file@deploy-file , i.e. @ the id of the configured execution, and make sure you're using at least Maven 3.3.

Can we deploy using Maven?

The maven deploy plugin can be used to deploy either files or projects to the remote repository for sharing it with other developers and projects. There are various methods can be used to deploy your artifact to the remote repository by using a maven deploy plugin.


1 Answers

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

like image 131
Newtopian Avatar answered Sep 29 '22 13:09

Newtopian