0

With a friend we work on a project for our school and we are at the end. The Probleme is that with Eclipse, our java program work correctly, and with the JAR generated by eclipse too. But we need to, when you exec the command mvn package, to generate a JAR. This JAR don't find classes of External JAR, it's the first time we use Maven so can you please help me to make a good pom.xml that when we do mvn package it generat a JAR using External JAR ? There is the pom :

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>coo.project</groupId>
  <artifactId>COO-DONJON</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>COO-DONJON</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies>

  <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
 </dependency>
 <dependency>
     <groupId>javazoom</groupId>
     <artifactId>jlayer</artifactId>
     <version>1.0.1</version>
 </dependency>
  </dependencies>
  <build>
 <plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
     <execution>
      <id>copy-dependencies</id>
      <phase>prepare-package</phase>
      <goals>
       <goal>copy-dependencies</goal>
      </goals>
      <configuration>
       <outputDirectory>${project.build.directory}</outputDirectory>
       <overWriteReleases>true</overWriteReleases>
       <overWriteSnapshots>true</overWriteSnapshots>
       <overWriteIfNewer>true</overWriteIfNewer>
      </configuration>
     </execution>
    </executions>
  </plugin>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-javadoc-plugin</artifactId>
   <version>2.10.4</version>
  </plugin>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <version>3.0.2</version>
   <configuration>
    <archive>
     <manifest>
      <addClasspath>true</addClasspath>
      <mainClass>coo.project.App</mainClass>
     </manifest>  
    </archive>
   </configuration>
  </plugin>
 
 </plugins>
 </build>
</project>
BlaCkOmN
  • 81
  • 1
  • 8

2 Answers2

1

You will have to use the Maven Assembly Plugin.
What you need is an executable jar which should contain all your dependencies. Check below for a sample code.

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>coo.project.App</mainClass>    // your main class here.
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

This will produce two jars. One of them will be a regular jar, and the second one will be a fat jar with -jar-with-dependencies appended to its name. This is the jar that you should be using now. This fat jar will include your code as well as all the dependencies that your code needs.

If you do this, I don't think you will have to configure the maven-dependency plugin as well as the maven jar-plugin as done in your pom.xml. You can ignore that.

RITZ XAVI
  • 3,633
  • 1
  • 25
  • 35
0

You need to use Maven shade plugin to add all dependencies inside jar by creating uber/fat jar.

Check this http://howtodoinjava.com/maven/maven-shade-plugin-create-uberfat-jar-example/

Noman Khan
  • 920
  • 5
  • 12