一些系统服务是通过java -jar xx.jar xx.xxx.Aaa param1 param2来启动服务,问题就接踵而来了,
xx.jar的jar包依赖怎么解决?
解决方式一:
1、通过配置maven依赖,将所有依赖打成一个jar包;
该方式存在问题,如果某些依赖需要通过<scope>system</scope><systemPath>${project.basedir}/lib/neo4j-server-1.9.2-static-web.jar</systemPath>解决,
将所有依赖打成一个jar包就会有问题
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server-web</artifactId>
<version>1.9.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/neo4j-server-1.9.2-static-web.jar</systemPath>
</dependency>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.sniper.neo4j.util.Neo4jServerUtil</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
2、通过maven插件打包的时候设置classpath路径,将依赖的jar包打成路径放到MANIFEST.MF中,但是system方式的依赖同样存在问题
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.sniper.neo4j.util.Neo4jServerUtil</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
3、通过java -cp .;lib/* xx.xx.Aaa的方式运行就可以解决
只要将打成的jar包、依赖的jar包都拷贝到lib文件夹下就可以
注意,windows下的分割符是分号;,linux下的分隔符是冒号:
<!-- 打包时将依赖jar包拷贝到指定目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 如果不设置路径,默认将依赖jar包导出到:target/dependency,可自定义路径,比如:target/export -->
<!--
<outputDirectory>target</outputDirectory>
-->
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>