2016-09-14 102 views
1

我想創建一個基於maven的項目,它有一個父pom,然後是兩個模塊。一個用於商業,一個用於依賴於業務的命令行應用程序。Maven和父項目

父pom.xml的

<?xml version="1.0" encoding="UTF-8"?> 
<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>dk.fitfit.triangle.parent</groupId> 
    <artifactId>triangle-parent</artifactId> 
    <packaging>pom</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <modules> 
     <module>Business</module> 
     <module>Cmd</module> 
    </modules> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-jar-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <archive> 
         <manifest> 
          <addClasspath>true</addClasspath> 
          <classpathPrefix>lib/</classpathPrefix> 
          <mainClass>dk.fitfit.triangle.cmd.Main</mainClass> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

然後,我有業務POM

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <parent> 
     <artifactId>triangle-parent</artifactId> 
     <groupId>dk.fitfit.triangle.parent</groupId> 
     <version>1.0-SNAPSHOT</version> 
    </parent> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>dk.fitfit.triangle.business</groupId> 
    <artifactId>triangle-business</artifactId> 

    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.7</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

最後的命令行應用程序

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <parent> 
     <artifactId>triangle-parent</artifactId> 
     <groupId>dk.fitfit.triangle.parent</groupId> 
     <version>1.0-SNAPSHOT</version> 
    </parent> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>dk.fitfit.triangle.cmd</groupId> 
    <artifactId>triangle-cmd</artifactId> 

    <dependencies> 
     <dependency> 
      <groupId>dk.fitfit.triangle.business</groupId> 
      <artifactId>triangle-business</artifactId> 
      <version>1.0-SNAPSHOT</version> 
      <scope>compile</scope> 
     </dependency> 
    </dependencies> 

</project> 

的問題是當POM文件我嘗試運行我的jar文件我收到以下錯誤消息

Exception in thread "main" java.lang.NoClassDefFoundError: dk/fitfit/triangle/business/Triangle 
at dk.fitfit.triangle.cmd.Main.main(Main.java:17) 
Caused by: java.lang.ClassNotFoundException: dk.fitfit.triangle.business.Triangle 
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
... 1 more 

我試圖運行我的罐子使用以下命令

mvn install && mvn package && java -jar Cmd/target/triangle-cmd-1.0-SNAPSHOT.jar 1 2 3 

什麼我做錯了任何線索?

回答

2

每個模塊都會生成一個單獨的jar,也就是說,您有一個business-jar和一個cmd-jar。當你運行java命令時,你只能指定cmd-jar。

現在你有兩個選擇:

4

在你的命令行應用程序的pom,用下面的代碼替換maven-jar-plugin。現在

<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <version>2.4.1</version> 
     <configuration> 
       <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <archive> 
         <manifest> 
          <mainClass>dk.fitfit.triangle.cmd.Main</mainClass> 
         </manifest> 
       </archive> 
     </configuration> 
     <executions> 
      <execution> 
        <id>make-assembly</id> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
      </execution> 
     </executions> 
</plugin> 

,你會得到一個胖罐子其中將包含所有可以使用的依賴關係。

+0

這正是我已經做的,但謝謝! – user672009