Maven 处理本地sdkjar
项目中,需要引入本地自己的sdk,因为对maven不熟悉,自己查看了许多其他朋友的资料,
下面总结记录下,希望可以帮忙到其他朋友。
maven打包main函数运行的jar方式
1. maven-shade-plugin
2. maven-assembly-plugin
3. maven-onejar-plugin
因为现在项目中有使用Spring,使用第二种方式的话,将会出现spring加载的错误,所以不使用。
现在主要介绍1和3两种。
1. maven-shade-plugin
现在项目中使用的就是 1打成jar文件,具体pom文件如下。
<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.shiyi.kmtask</groupId> <artifactId>shiyi-kmtask</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <cxf.version>2.6.1</cxf.version> <spring.version>3.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.7</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.33</version> </dependency> <dependency> <groupId>com.sun.jna</groupId> <artifactId>jna</artifactId> <version>3.0.9</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!-- <dependency> <groupId>com.qiniu</groupId> <artifactId>sdk</artifactId> <version>[6.1.7, 6.999]</version> </dependency> --> <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>[7.0.0, 7.0.99]</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> </dependencies> <build> <finalName>KMTaskService</finalName> <plugins> <plugin> <artifactId>maven-shade-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> <exclude>**/config.properties</exclude> <exclude>**/checkdll.dll</exclude> <exclude>**/EnDeCrypinfo.h</exclude> <exclude>**/EnDecryption.dll</exclude> <exclude>**/libcheck.so</exclude> <exclude>**/libEnDecryption.so</exclude> <exclude>**/libmad.so</exclude> <exclude>**/libmad.so.0</exclude> <exclude>**/libmad.so.0.2.1</exclude> <exclude>**/rdck.cfg</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>com.shiyi.autoservice.main.TaskServiceMain</Main-Class> <Build-Number>1.0.2</Build-Number> <X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK> <X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
现在需要加入本地的sdk,
(1)不去本地仓库查找,在自己环境中
<dependency>
<groupId>monitor</groupId>
<artifactId>monitor</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/monitor.sdk.java.jar</systemPath>
</dependency>
因为使用的是maven-shade-plugin插件,打包后,你将发现实际上,本地的sdk并没有存在。
原因是因为,使用shade插件的时候,它将直接无视scope为system的jar !!!
所以此方法失败了。
(2)将sdk加入本地仓库,然后正常使用,步骤如下:
1.将sdk加入本地仓库
mvn install:install-file -Dfile=D:\Users\User\workspace\KMFtpDataClient\src\main\lib\monitor.sdk.java.jar -DgroupId=km -DartifactId=monitor
-Dversion=1.0 -Dpackaging=jar
2.maven中使用
<dependency>
<groupId>km</groupId>
<artifactId>monitor</artifactId>
<version>1.0</version>
</dependency>
这个方法可以使用,但是如果其他同事svn引入代码的时候,因为他的本地仓库没有这个jar,将出现错误,所以
最后将它加入自己的中央仓库中。
3. maven-onejar-plugin
先在pom中加入
<dependency>
<groupId>monitor</groupId>
<artifactId>monitor</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/monitor.sdk.java.jar</systemPath>
</dependency>
然后打包build中代码如下:
<build> <finalName>KMTaskService</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.shiyi.autoservice.main.TaskServiceMain</mainClass> </manifest> </archive> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> <exclude>**/config.properties</exclude> <exclude>**/checkdll.dll</exclude> <exclude>**/EnDeCrypinfo.h</exclude> <exclude>**/EnDecryption.dll</exclude> <exclude>**/libcheck.so</exclude> <exclude>**/libEnDecryption.so</exclude> <exclude>**/libmad.so</exclude> <exclude>**/libmad.so.0</exclude> <exclude>**/libmad.so.0.2.1</exclude> <exclude>**/rdck.cfg</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>com.jolira</groupId> <artifactId>onejar-maven-plugin</artifactId> <version>1.4.4</version> <executions> <execution> <configuration> <attachToBuild>true</attachToBuild> <classifier>onejar</classifier> </configuration> <goals> <goal>one-jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
以上方法也可以正常运行。至于两种方法的优势,就不是很清楚了。
另外,在运行的过称中,出现了大量的重复类。
查看依赖树并没有发现有重复冲突的依赖包(依赖树介绍:http://ian.wang/106.htm)
后来才发现,原来自己本地的sdk中有出现在实际项目中一个的jar,然后并没有以依赖包的形式存在,
而是直接编译在sdk中。
情况大致如下:
本地sdk的pom.xml与项目的pom.xml都有下面依赖存在
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
这种情况,依赖树并不能检查出重复,但是编译的时候,却出现了类重复的警告。
以上对策:
1.无视它,目前没发现对实际运行有影响
2,注释掉项目中与sdk重复的依赖包(额,自己去找,也是很坑的)
3.修正sdk,最好依赖的包别直接编译进sdk中
以上,都是个人的见解,如果有更好的对策或者不对的地方,欢迎各位朋友告知,谢谢。