Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Build with Dependencies [duplicate]

I've downloaded a source code from internet and I'm trying to build it with maven. Here is the pom.xml file that came with the source code:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>myArtifact</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myArtifact</name>
<url>http://maven.apache.org</url>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>myArtifact.Main</mainClass>
                    </manifest>
                </archive>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.4-701.jdbc4</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
</project>

The mvn install command is generating the jar file but I'm unable to execute this file because of the missing dependencies (class not found exception related to postgresql dependency in this example).

I've noticed that maven has downloaded the dependencies correctly (the jar libraries are all at the local maven repository directory) but mvn install is not copying these libraries into the generated jar file. How can I do this?

Thanks.

like image 937
Fernando Magalhães Avatar asked Dec 08 '11 02:12

Fernando Magalhães


People also ask

How do I delete duplicate dependencies in maven?

Use the <exclusions> tag into <dependency> tag of the pom to exclude that duplicate dependencies from maven project. Show activity on this post. Run mvn clean It will tell you your duplicate dependencies. Then delete them.

How do I remove duplicate dependencies?

Removing Duplicate Dependencies Once we have identified our duplicate dependencies, the simplest way to remove them is to delete them from pom. xml and keep only those unique dependencies that are used by our project.

How does maven resolve transitive dependencies?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.

Which dependency lets maven distinguish between multiple artifacts that are generated from the same project?

Maven can automatically bring in these artifacts, also called transitive dependencies. Version collision happens when multiple dependencies link to the same artifact, but use different versions.


1 Answers

Remove the descriptorRefs and archive tag from your maven-compiler-plugin section.

In order to generate a JAR with all the dependencies self contained. You need to put this into the build plugins section of your pom.xml:

<build>     <plugins>         <plugin>             <artifactId>maven-assembly-plugin</artifactId>             <configuration>                 <archive>                     <manifest>                         <mainClass>                             com.test.your.main.class.goes.Here                         </mainClass>                     </manifest>                 </archive>                 <descriptorRefs>                     <descriptorRef>jar-with-dependencies</descriptorRef>                 </descriptorRefs>             </configuration>         </plugin>     </plugins> </build> 

Create the JAR file by running the following at the command prompt:

mvn clean compile assembly:single 
like image 116
Neeraj Verma Avatar answered Sep 22 '22 05:09

Neeraj Verma