The correct way to import dependencies with specific requirements for names and classes?

I am developing a project that is dependent on a third-party library packaged as a JAR, which has some resources other than java. I don't have access to its sources, but apparently the way the library gets certain resources in it is to specify a URI relative to the JAR of that library. In other words: code only works if the jar library has a specific name.

This gives me a lot of headaches since I use Maven to create my project. Usually I install third-party libraries in my local repository and import them as dependencies in the POM, and I don't care if the jar name in the dependency library has been changed or not Maven (usually adding a version to the name).

What is the correct way to solve this problem? I have an intuition, I have to somehow import a third-party library with the expected name and somehow add to the class path the parent directory into which the library jar was imported (so that the jar can be found at runtime with its name) . It's right? If so, how could I do this?

To complicate matters, the project that I am developing is also the library itself, so it must also be imported by other projects using Maven. For example, at first I thought of declaring the library as conditional , and then copying it to a file (with the required name) in a directory in my project with something like:

<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.7</version> <executions> <execution> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>theGroup</groupId> <artifactId>theArtifact</artifactId> <version>theVersion</version> <type>jar</type> <outputDirectory>${project.build.directory}/lib</outputDirectory> <destFileName>EXPECTED_NAME.jar</destFileName> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> 

But this will not work, since the copied file is not considered a transitive dependency when importing my library into another project.

UPDATE:

I think something that might work is to find a third-party jar in the resources folder (with the expected name), and then somehow add the contents of the jar to the class path so that this class path is also taken into account with transitive dependencies. In case this solution can work, what is the easiest way to do this?

+4
source share
1 answer

You can copy this jar to your project folder and add depenece with the system area.

Let's say your name in the bank is "specificLib.jar". You can create a folder called external-libs under your main project and copy the jar inside. You can declare a dependency as follows:

 <dependency> <groupId>particular</groupId> <artifactId>particularLib</artifactId> <version>1</version> <scope>system</scope> <systemPath>${basedir}/external-libs/partucularLib.jar</systemPath> </dependency> 
0
source

Source: https://habr.com/ru/post/1499971/


All Articles