2016-10-14 107 views
0

與朋友一起,我們爲我們的學校開展一個項目,我們最後。 問題是,在Eclipse中,我們的java程序正常工作,並且使用eclipse生成的JAR。但是我們需要在執行命令mvn package時生成一個JAR。這個JAR沒有找到外部JAR的類,這是我們第一次使用Maven,所以你可以幫我做一個很好的pom.xml,當我們使用外部JAR生成一個JAR時,它會生成一個JAR文件? 還有就是POM:問題使用MAVEN使用外部JAR製作JAR

<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> 
 

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

回答

1

你將不得不使用Maven Assembly Plugin
你需要的是一個可執行的jar包,它應該包含你所有的依賴關係。請查看下面的示例代碼。

<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> 

這會產生兩個罐子。其中一個將是一個普通的罐子,第二個將是一個名字後面加上-jar-with-dependencies的胖罐子。這是你現在應該使用的罐子。這個胖的jar包會包含你的代碼以及你的代碼需要的所有依賴。

如果你這樣做,我認爲你不必像在你的pom.xml中那樣配置maven-dependency插件以及maven jar-plugin。你可以忽略它。

+0

它的工作原理,THKü – BlaCkOmN

+0

:-)沒有問題。 –