最近在用intellij IDEA创建maven项目的形式写些代码,依赖了一些库,通过pom.xml添加依赖解决。最终需要打包使用的时候,使用java命令运行其中的Class会报错,因为找不到依赖。
我在 http://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven 找到了解决方法。
可以通过添加一个插件来解决:
在pom.xml添加一个与<dependencies/>平级的节点:
<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
如此,使用 mvn package打包,jar会在target目录中生成,然后用java命令执行就行了:
java -cp target/example-1.0-SNAPSHOT-jar-with-dependencies.jar path.to.ExampleClass
本文介绍如何使用Maven将项目的依赖库打包到一个单独的Jar文件中,以便于项目的部署和运行。通过在pom.xml中配置maven-assembly-plugin插件实现。

660

被折叠的 条评论
为什么被折叠?



