Bhim
Bhim

Reputation: 51

Executable Jar with external jar and dependency using maven

There are two Java applications: application 1 and application 2.

I want to use jar file of application 1 in application 2. I want to create executable jar of application 2 so that I can execute application 2 as jar by command line.

Application one: There are classes in application 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

Upvotes: 1

Views: 1329

Answers (1)

Conffusion
Conffusion

Reputation: 4465

In application2.jar you can specify to add application1.jar to its classpath. In the META-INF/manifest.mf file of application2.jar add:

Class-Path:application1.jar

if the application1.jar is stored aside (in the same directory) of application2.jar it will be added to the classpath by the runtime.

To realize this in your maven-build:

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

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

Upvotes: 1

Related Questions