Maven project with its own dependency and file copying

I have the following script:

mylib is a library (for which I have sources, so I would like to put them in a Maven mylib project: mylib, for example). There is a jar dependency in this library, for which I only have a jar, and it cannot be found in the Maven repository (and I don't want to install it there). To compile it, something like this will work: add the jar file to the mylib project in the "lib" folder, for example. "lib / thirdpartylib.jar" and in mylib pom.xml, add the dependency with the group / artifact / version you selected and the entry " <scope>system</scope><systemPath>${project.basedir}/lib/thirdpartylib.jar</systemPath> ". The mylib project will compile fine.

Note that mylib also has a dependency on the executable in the dll file, say thirdparty.dll. But this is not important for compilation.

However, now I am wondering how to achieve the following:

Any other projects, for example. the "X" project that uses mylib will need

 - mylib.jar - thirdpartylib.jar - thirdpartylib.dll 

and he will have to install java.library.path in the directory (for example, "."), so that the third-party drum and dll will be found by the executing virtual machine.

My concern is this: I would like the jar / dll Thirdparty stuff to be responsible for the mylib project. That is, I want to determine the knowledge that you need to copy the thirdparty and dll jar to the target folder and that java.library.path refers to them to be part of the mylib project (mylib pom knows how it should be used in other projects). However, I need this knowledge (for example, copying instructions, no matter how accurately they are executed in Maven), to be transitively passed to any other project using mylib, for example X. Is this possible?

[My solution for hacking now will be that I have an instance of third-party things in X, but even then I don’t know how to copy / process the dll file, so I would have to write readme that said the dll file must be copied to the bin folder of the executing virtual machine).

Any suggestions are welcome!

+6
source share
2 answers

I would suggest including files in Maven modules by installing them in a local repository using the Maven installation plugin

 mvn install:install-file \ -DgroupId=com.demo \ -DartifactId=thirdpartylib \ -Dversion=1.0 \ -Dfile=thirdpartylib.jar mvn install:install-file \ -DgroupId=com.demo \ -DartifactId=thirdpartylib-runtime \ -Dversion=1.0 \ -Dpackaging=dll -Dfile=thirdpartylib.dll 

One neat thing about this approach is that the Maven POM will be automatically generated.

The mylib project now declares third-party modules as regular dependencies in it POM file:

 <dependencies> <dependency> <groupId>com.demo</groupId> <artifactId>thirdpartylib</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.demo</groupId> <artifactId>thirdpartylib-runtime</artifactId> <version>1.0</version> <scope>runtime</scope> </dependency> </dependencies> 

Now, when the mylib module refers to other modules (as a dependency), third-party modules will also load as transitive dependencies.

+13
source

The basic idea is as follows:

  • Maven does a good job with one result on Maven POM.
  • Access to libraries and dependencies is only possible in your local repositories.

So you should take the following steps:

  • Define a separate project (or module in your project) for the additional library and define the library as the result.
  • Modify your POM so that this POM is now dependent on a new project.
  • Follow the same steps for your DLL (see Managing DLL Dependencies with Maven ) for how to do this).
  • Deploy your additional library and your DLL to the local repository.

Now you can use Maven again for the build process, and with additional plugins like the maven-assembly-plugin , you can put everything together.

+2
source

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


All Articles