如何将含软链接的第三方JNI库上传至本地Nexus仓库
Alright, let's tackle this problem step by step. Since you can't modify the original foo.jar or the .so files, we need to work around the soft link issue first (because soft links won't work reliably in a Nexus repository) and then get everything uploaded properly.
First: Fix the Soft Link Problem
Soft links are environment-specific—once you upload them to Nexus and download them elsewhere, the link target won't exist. So we need to replace the soft link with an actual copy of the file:
- Make a duplicate of
libfoo.so.1.0and rename it tolibfoo.so. Now you have two standalone files instead of a link and its target, which will work across environments.
Option 1: Package JAR and SO Files into a Single Artifact
If you want all related files to live as one unit in Nexus, this is the way to go:
Create a temporary Maven project with this structure:
temp-foo-project/ ├── pom.xml └── src/ └── main/ └── resources/ └── META-INF/ └── native/ ├── libfoo.so └── libfoo.so.1.0(You don't need any Java code here—this project just wraps the existing files.)
Add this
pom.xmlto the project, adjusting paths and Nexus details to match your setup:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.your-org</groupId> <artifactId>foo-with-native</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <dependencies> <!-- Pull in the original foo.jar as a system dependency --> <dependency> <groupId>com.foo-group</groupId> <artifactId>foo</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/path/to/your/foo.jar</systemPath> </dependency> </dependencies> <build> <plugins> <!-- Package everything into a single JAR --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.4.2</version> <executions> <execution> <id>assemble-all</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <appendAssemblyId>false</appendAssemblyId> </configuration> </execution> </executions> </plugin> <!-- Deploy to Nexus --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.0.0-M2</version> <configuration> <repositoryId>nexus-thirdparty</repositoryId> <url>http://your-nexus-url/repository/thirdparty/</url> </configuration> </plugin> </plugins> </build> </project>Run this command to build and upload the artifact to Nexus:
mvn clean deployNote: Make sure your Maven
settings.xmlhas credentials for thenexus-thirdpartyrepository ID.
Option 2: Upload JAR and SO Files as Separate Artifacts
If you prefer to manage the Java library and native libraries independently, you can upload them separately:
Upload the original foo.jar:
Use Maven'sdeploy-filecommand directly:mvn deploy:deploy-file \ -DgroupId=com.foo-group \ -DartifactId=foo \ -Dversion=1.0.0 \ -Dpackaging=jar \ -Dfile=/path/to/your/foo.jar \ -DrepositoryId=nexus-thirdparty \ -Durl=http://your-nexus-url/repository/thirdparty/Upload the native libraries:
Package the two.sofiles into a JAR (or ZIP) and upload it as a separate artifact:# First, package the SO files into a JAR jar cvf foo-native-1.0.0.jar libfoo.so libfoo.so.1.0 # Then upload to Nexus mvn deploy:deploy-file \ -DgroupId=com.foo-group \ -DartifactId=foo-native \ -Dversion=1.0.0 \ -Dpackaging=jar \ -Dfile=foo-native-1.0.0.jar \ -DrepositoryId=nexus-thirdparty \ -Durl=http://your-nexus-url/repository/thirdparty/Later, when using the library, just add dependencies on both
fooandfoo-nativein your project'spom.xml.
Key Notes
- Always replace soft links with actual file copies—this avoids broken links when downloading from Nexus to other environments.
- Double-check your Nexus repository settings to ensure it allows uploads to the third-party repo, and that your Maven
settings.xmlhas the correct username/password for the repository ID. - When using the library in your project, you'll need to ensure the native libraries are on the
java.library.pathor use a JNI loader that can find them (like from theMETA-INF/nativedirectory if you used Option 1).
内容的提问来源于stack exchange,提问作者Ramón J Romero y Vigil

