javamavenjava-native-interfacenativemaven-install-plugin

Why does Maven trim the "lib" prefix when installing native libraries when Java expects this convention for linking?


I have been reviving two projects lately involving JNI-based (Java with native libraries) code. The build system is legacy (Makefile based, not Maven or Gradle), but that's ok given the age of the projects (their inception predates Maven), and because some significant parts of the code are not written in Java.

There is no plan for replacing the Makefile (or configure) scripts with Maven or Gradle configuration scripts (the build systems I inherited work fine, and would probably not been made simpler with other tools, given the fact that the softwares are not pure Java).

However, deploying the jars (even built with another tool) in the Maven repo is straightforward with the Maven installation plugin, and makes their use by third party code much simpler, so I went this way.

I can make everything work, but I have been unable to figure out why Maven (or more specifically, the Maven install plugin) behaves like it does, that seems to make things more complex than they need to be. Most probably, my understanding of Maven's behaviour or of the Java linking system is lacking.

Here is the issue: for installing a jar and its native library, one can use the following commands.

mvn install:install-file -Dfile=$(jarFile) -DgroupId=$(groupId) -DartifactId=$(artifactId) -Dversion=$(libraryVersion) -Dpackaging=jar

mvn install:install-file -Dfile=$(nativeLibraryFile) -DgroupId=$(groupId) -DartifactId=$(artifactId) -Dversion=$(libraryVersion) -Dpackaging=$(soExtension)

Here soExtension can be dll, so, dylib depending on the operating system. The two files end up in the same location of the Maven local repository, namely .m2/repository/groupId/artifactId/versionId

However, I observed that the native library, initially called, say libmylibrary.so, was systematically renamed by Maven mylibrary.so in the repository. As a consequence, even when java is given the proper library path (the -Djava.library.path option), it issues a link error because it cannot find the right library name (it does expect the "lib" prefix, at least in my available implementations).

The link error can be solved by several means:

The two previous solutions are a rather clumsy way to go, because they assume a knowledge of the internals of the Maven repository (going against the encapsulation principle).

Does anyone know why the Maven installation system behave the way it does? And if there is a workaround for it?

Thank you


Solution

  • The name of artifacts in a repository is generated out of artifactId and no there is no possibility to change that. If you like or need to change thatyou have to change the artifactId.

    For running that on a particular platform I would create a kind of installation package which contains the correctly named artifacts.