Jaffer Wilson
Jaffer Wilson

Reputation: 7273

How to create a Maven project jar with dependencies which are used by the buildpath?

I am trying to create a Maven project jar file. I know about the Maven repository and the pom.xml file configuration. But my query is a bit different from that.

I have created a Maven repository. I have included the respective dependency jar files by downloading them and then given in the build path of the project (as I am using Eclipse Neon).

Now when I run as --> Maven install, it creates a jar file. But that jar is of the only project and not the dependencies that I have included.

I have checked and got suggestions that I need to include them in the Maven pom.xml, but I do not know exactly which jar is getting used in my program.

So I would like to know how can I automatically make the Maven project detect the dependencies and write them to the pom.xml file without my interference, if possible.

Kindly let me know how is this possible?

Upvotes: 0

Views: 1059

Answers (2)

freedev
freedev

Reputation: 30057

Another solution can be use directly Maven to start your application, in this way maven transparently add all the dependencies configured. You have just to take care before of that to execute the Maven package goal.

 # create your jar file with maven
 mvn clean package

 # execute your class where is main 
 mvn exec:java -Dexec.mainClass="your.package.App" -Dexec.args="your app params"

You can even wrap this line into a shell script run.sh:

#!/bin/bash

PARAMS="$*"

if [ "A$PARAMS" == "A" ]
then
  PARAMS="--help"
fi

mvn exec:java -Dexec.mainClass="your.package.App" -Dexec.args="$PARAMS"

Upvotes: 0

freedev
freedev

Reputation: 30057

I have solved this problem using the maven-dependency-plugin.

This plugin make a copy of all dependencies needed by your jar file into the directory ${project.build.directory}/lib.

When you need to start your jar file you have to specify the classpath -cp /path/to/your-jar-file.jar:/path/to/your/lib/*

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Upvotes: 1

Related Questions