Home » Java » java – Can I add a directory containing a jar and its dependant jars as a maven dependency?-Exceptionshub

java – Can I add a directory containing a jar and its dependant jars as a maven dependency?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am working on a small codebase (maven project) which requires libraries provided by the creator of the system my project integrates with. Now the library comes in the following form:

com.example.library.client_1.2.3.v20190123/ (just a directory)
├── lib/
│   ├── some-dependency-3.2.1.jar
│   ├── ....jar
│   └── another-dependency-1.2.3.jar
├── META-INF/
│   └── MANIFEST.MF
└── some.library.jar

How was this library built?
How can I add a “proper” dependency to such a package using maven so that I can later build a big/uber jar? I know with a single jar there are a few ways e.g. I can install it to my local repository using mvn install:install-file .... But how can I add the above structure as a dependency to also include the libraries inside the lib directory (the transitive dependencies)? Can I repackage the above strucutre for better usage?

Would I need to add all those lib jars to my local maven repository individually?

If of interest, the manifest has the following form:

Manifest-Version: 1.0
Bundle-SymbolicName: com.example.library....;singleton:=true
Export-Package: com.example.library...
Bundle-Name: ...
Bundle-Version: 1.2.3.v20190123
Bundle-ClassPath: lib/some-dependency-3.2.1.jar,lib/...
.jar,lib/another-dependency-1.2.3.jar
Bundle-ManifestVersion: 2
Bundle-ActivationPolicy: lazy
Bundle-Vendor: ...
How to&Answers:

The Manifest tells you that the jar of this library contains the dependencies. If you install this library jar into OSGi it should work as it has all dependencies embedded.

If you plan to use this library outside of OSGi then this kind of packaging with embedded libraries makes no sense. Maybe they also offer just the library and you can install the dependencies via maven (if they are well known open source libs).

Answer:

Install all the dependencies mentioned in the manifest file that are available from public maven repositories directly from there as mentioned by @Christian Schneider. Those that are not available can be installed manually to a project-local maven repository in the project scope. To do this, define the in-project maven repository in your parent pom.xml file:

<repositories>
    <repository>
        <id>in-project</id>
        <name>In Project Repo</name>
        <url>file://${project.basedir}/lib</url>
    </repository>
</repositories>

Then install the library some.library.jar and the dependencies listed in the manifest that are not openly available to the in-project repository:

mvn install:install-file \
    -Dfile=path/to/some-library-1.2.3.jar \
    -DgroupId=com.example \
    -DartifactId=some-library \
    -Dversion=1.2.3 \
    -Dpackaging=jar \
    -DlocalRepositoryPath=lib \
    -DcreateChecksum=true

This will then generate the structure in your local maven repo (lib) and also generate the checksums for maven.