我的Java项目有许多依赖项,例如Box2d
,OpenGl
和OpenAl
我在plugin
中定义了pom.xml
的以下列表,因此使用Maven来管理项目。
注意:我在pom.xml
底部列出了依赖项,但不在此处。
我按照此文档开始使用:http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source/>
<target/>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.app.game.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
当我运行mvn package
时,会创建jar
,即target/test-0.1.jar
它实际上包含所有jar
个依赖项,并在包的根目录中包含原生.so
和.dll
个库。
但是当我尝试从CLI运行应用程序时,我得到UnsatisfiedLinkError
例外
我错过了pom
中的内容吗?
[rob@work target]$ java -jar test-0.1.jar
log4j:WARN No appenders could be found for logger (com.app.game.Main).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Catched FileNotFoundException: /home/rob/git/test/target/test-0.1-natives-linux-amd64.jar (No such file or directory), while addNativeJarLibsImpl(classFromJavaJar class com.jogamp.common.os.Platform, classJarURI jar:file:/home/rob/git/test/target/test-0.1.jar!/com/jogamp/common/os/Platform.class, nativeJarBaseName test-0.1-natives-linux-amd64.jar): [ file:/home/rob/git/test/target/test-0.1.jar -> file:/home/rob/git/test/target/ ] + test-0.1-natives-linux-amd64.jar -> slim: jar:file:/home/rob/git/test/target/test-0.1-natives-linux-amd64.jar!/
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/rob/git/test/target/libgluegen-rt.so (/home/rob/git/test/target/liblibgluegen-rt.so.so: cannot open shared object file: No such file or directory)
at java.lang.ClassLoader.loadLibraryWithPath(ClassLoader.java:1217)
答案 0 :(得分:0)
在plugins
中使用以下一组POM.xml
实现了我的目标......
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>verify</phase> <!-- bind to the verify phase, to execute after package phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<programs>
<program>
<mainClass>com.app.game.Main</mainClass>
<id>thegame</id>
</program>
</programs>
<repositoryName>lib</repositoryName>
<repositoryLayout>flat</repositoryLayout>
<assembleDirectory>${project.build.directory}/${project.artifactId}</assembleDirectory>
</configuration>
<executions>
<execution>
<id>prepare-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the package phase -->
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
src/assembly/assembly.xml
..
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/${project.artifactId}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>/**</include>
</includes>
<excludes>
<exclude>bin/*</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/${project.artifactId}/bin</directory>
<lineEnding>keep</lineEnding>
<useDefaultExcludes>true</useDefaultExcludes>
<outputDirectory>bin</outputDirectory>
<includes>
<include>*</include>
</includes>
<fileMode>755</fileMode>
</fileSet>
</fileSets>
</assembly>