简体   繁体   中英

Maven: Export jar with 3rd-party jars added to classpath in manifest

I am developing a Java maven project with Eclipse and want to export a jar that includes all referenced libraries . These referenced libraries fall into one of these two categories:

  1. They are explicit (or implicit) dependencies in the pom.xml

  2. I have some libraries not available as maven artifacts and have put them in /lib (and added them to the build path in Eclipse)

There's the maven-assembly-plugin , which works fine for 1). However, I'm unable to find a maven plugin that also includes non-maven-dependencies, eg "all jars in /lib" .

Then there's the Eclipse FatJar plugin, which sort of works, but hasn't been updated since 2009, so it seems unmaintained. Also I prefer an export method that I can directly specify in the pom.xml .

Can anyone point me to a maven plugin or the like to export all referenced libraries , including those from case 2) ? That only needs to involve putting them in the jar and referencing them in the manifest's classpath.

Thanks!

I think the best way to handle this is to include your custom libs into a local maven repository. Now you can inlcude your libraries as maven dependencies and you can export all your dependencies specified in your pom with the maven-assembly-plugin.

Here is a tutorial, how to put your libs into a local repository in maven to use it in your pom. http://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/

And in your pom.xml:

<!-- setup jar manifest to executable with dependencies -->
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>your.main.class</mainClass>
      </manifest>
    </archive>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
       <goal>single</goal>
      </goals>  
    </execution>
  </executions>
</plugin>

This looks like a task for Tycho . It is a set of maven plugins that allows to create eclipse plugins with maven. Tycho considers manifest entries as build dependencies.

However I'm not aware of how to package all those dependencies in a single jar. This may also be conflicting with the osgi spec. But if you wish to ignore osgi you could just try the jar-with-dependencies assembly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM