外部jar的可执行Jar和使用maven的依赖

时间:2014-08-01 11:33:43

标签: java

有两个java应用程序:应用程序1和应用程序2。     我想在应用程序2中使用应用程序1的jar文件。我想创建应用程序2的可执行jar,以便我可以通过命令行执行应用程序2作为jar。

Application one :
There are classes in apllication 1 as ClassA.java, ClassB.java
pom.xml : 

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

I created jar file of application 1 using mvn clean compile assembly:single

Now I have added jar file of application 1 created before as an external jar in application 2.
In application 2 There is a main class : migration.DataMigration.java
There are dependencies also in pom.xml of application 2.
DataMigration class is using ClassA.java and ClassB.java.


Now I want to create a executable jar of application.
I tried to create this using maven-assembly-plugin but I got error : ClassA.class not not found, ClassB.class not found : it means jar of application 1 is not being available during executable jar creation.

but when I run application 2 in eclipse it executes correctly without error.

Can any one suggest how to create executable jar of application 2

1 个答案:

答案 0 :(得分:0)

在application2.jar中,您可以指定将application1.jar添加到其类路径中。 在application2.jar的META-INF / manifest.mf文件中添加:

Class-Path:application1.jar

如果将application1.jar存储在application2.jar的旁边(在同一目录中),它将被运行时添加到类路径中。

要在你的maven-build中实现这一点:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    ...
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
        </manifest>
      </archive>
    </configuration>
    ...
  </plugin>

来源:http://maven.apache.org/shared/maven-archiver/examples/classpath.html

相关问题