2017-06-16 105 views
0

我有一個項目與兩個類命名爲Test1Test2一個jar與一個類和從依賴瓶主要類

Both Test1 and Test2不是主要類別。現在我有一個名爲的依賴項cloudexe.jar它有一個主類ClassExecuter。現在我的問題是,我想ClassExecuter作爲主要類爲test1.jartest2.jar

test1.jar應該只包含的Test1類和它所有的依賴關係,包括cloudexe.jar同樣test2.jar應該只包含的Test2類和它所有的依賴關係,包括cloudexe.jar

現在,當我的包我的pom.xml我得到test1.jar和test2.jar,但我越來越像下面所示

Exception in thread "main" java.lang.NoSuchMethodException: com.uber.Test1.main([Ljava.lang.String;) 
     at java.lang.Class.getMethod(Class.java:1678) 
     at com.simontuffs.onejar.Boot.run(Boot.java:339) 
     at com.simontuffs.onejar.Boot.main(Boot.java:166) 

的pom.xml下面

<build> 
    <plugins> 
    <plugin> 
    <groupId>com.jolira</groupId> 
    <artifactId>onejar-maven-plugin</artifactId> 
    <version>1.4.4</version> 
    <executions> 
     <execution> 
     <id>build-first</id> 
      <configuration> 
      <mainClass>com.uber.Test1</mainClass> 
      <attachToBuild>true</attachToBuild> 
      <classifier>onejar</classifier> 
      <filename>test1.jar</filename> 
      </configuration> 
      <goals> 
      <goal>one-jar</goal> 
      </goals> 
     </execution> 
     <execution> 
     <id>build-second</id> 
      <configuration> 
      <mainClass>com.uber.Test2</mainClass> 
      <attachToBuild>true</attachToBuild> 
      <classifier>onejar</classifier> 
      <filename>test2.jar</filename> 
      </configuration> 
      <goals> 
      <goal>one-jar</goal> 
      </goals> 
     </execution> 
     </executions> 
    </plugin> 
    </plugins> 
</build> 

<pluginRepositories> 
    <pluginRepository> 
    <id>onejar-maven-plugin.googlecode.com</id> 
    <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url> 
    </pluginRepository> 
</pluginRepositories> 

給出任何人都可以請幫我在這

回答

1

你需要有2子項目父:

<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>com.example</groupId> 
    <artifactId>parent</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>pom</packaging> 

    <modules> 
    <module>test1</module> 
    <module>test2</module> 
    </modules> 

</project> 

然後,你必須2項目test1和test2項目,他們將生產罐子,使用陰影插件來執行主要類別ClassExecuter:

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

    <parent> 
    <groupId>com.example</groupId> 
    <artifactId>parent</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    </parent> 

    <artifactId>test1</artifactId> 
    <packaging>jar</packaging> 

    <dependencies> 
     ... all your dependencies including cloudexe.jar 
    </dependencies> 

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

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-shade-plugin</artifactId> 
       <version>3.0.0</version> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>shade</goal> 
         </goals> 
         <configuration> 
          <transformers> 
           <transformer 
             implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
            <manifestEntries> 
             <Main-Class>....ClassExecuter</Main-Class> 
            </manifestEntries> 
           </transformer> 
          </transformers> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 

     </plugins> 
    </build> 

</project> 
+0

感謝您的回覆,但其中'pom.xml'我將定義模塊' test1的 test2的 ' –

+0

你需要太看的Maven多模塊項目。你有一位家長,然後有兩個孩子。每個孩子都在自己的目錄中。在構建父項時,它將構建它的每個子項,在您的情況下,每個子項都將在其目標文件夾中包含一個可執行jar。 –

+0

但我需要一個類創建一個模塊項目.....如果我有10個類......那麼我需要創建10個模塊項目......是否有任何其他解決方案 –