Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

dependencies - Create a independent jar with maven and without assembly plugin

I have a jar project that uses some .jar libs to run. In old days we use Eclipse to build the jar, but now my company is migrating to Maven. I didn't know how hard is to create an independent jar file that contains its dependencies (or some of them) inside itself like before xD.

I've read that there's an "assembly" maven plugin that "unpack" the dependencies and put them inside the independent jar, but my company has its own maven repository and they don't have that plugin. So, I'm using the "dependency-plugin" to achieve that.

The problem is that I can't get my independent jar to run.

I have this in my pom:

...
<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>.</classpathPrefix>
        <mainClass>jar.App</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

...

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>3.1.2</version>
  <executions>
    <execution>
      <id>copy</id>
      <phase>package</phase>
      <goals>
        <goal>copy</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <artifactItems>
      <artifactItem>
        <groupId>com.example.jar2</groupId>
        <artifactId>jar2</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>jar</type>
        <overWrite>false</overWrite>
        <outputDirectory>${project.build.directory}/classes</outputDirectory>
      </artifactItem>
    </artifactItems>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <overWriteReleases>false</overWriteReleases>
    <overWriteSnapshots>true</overWriteSnapshots>
  </configuration>
</plugin>
<plugin>

Actually, the content of the pom.xml is not important because I get what I wanted: my jar with a structure like this:

jar1.jar
  |
  - com/something/my-classes.class
  |
  - META-INF
  |    |
  |    - MANIFEST
  |    - maven
  |        |
  |        - A lot of maven stuffs
  |
  - jar2-1.0-SNAPSHOT.jar

And my MANIFEST file has these entries:

Class-Path: ./jar2-1.0-SNAPSHOT.jar
Main-Class: jar.App

I thought it was enough to have my jar dependencies copied inside my independent jar and indicate the Class-Path in my MANIFEST to tell to java "hey! here are my dependencies!", but, when I try to run the jar with:

java -jar my-jar.jar

I get:

Exception in thread "main" java.lang.NoClassDefFoundError: jar2/Operations

In the line where I'm trying to use the classes of my dependencies.

Why is that? What I need to get an independent jar with Maven (without using the assambly plugin)? I supposed I'm missing something to indicate to Java where my dependencies are, but I'm not sure... Or maybe I need an extra class that "loads" every class that I'm using?

Thanks a lot!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...