网上说法比较多的有
maven-shade-plugin
和maven-assembly-plugin
maven-assembly-plugin打包的很有可能出现:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop] Offending resource: ServletContext resource [/WEB-INF/webConfig.xml]Unable to locate Spring NamespaceHandler for XML schema namespace
比较详细的解决方案是: https://www.cnblogs.com/icewee/articles/3703491.html,说是Spring的一个命名空间的名称空间处理程序没有找到,需要引入一些spring dependence,但是还是没有解决问题。后来在博客: https://blog.youkuaiyun.com/qing0706/article/details/51612040中找到了原因。原因如下:
jar包下的META-INF目录下,有两个跟spring相关的文件:spring.handlers、spring.schemas,其中包含了打包后spring的一些东西,aop丶context等,如果在项目中对spring有多个依赖比如同时依赖aop,context,打包后仅仅会保存最后一个依赖。所以有时候是没有
[http://www.springframework.org/schema/aop]
,有时候没
[http://www.springframework.org/schema/context]
需要加上一段配置如打包步骤2中transformer那个配置,这段配置意思是把spring.handlers和spring.schemas文件以append方式加入到构建的jar包中。但是配置了这个用assembly还是不好使,经过多次尝试失败后换了shade.
最后选用
maven-shade-plugin
.
二丶打包步骤
探索了2天,终于打包成功,有可能步骤中有多余,但因为工作时间比较紧,成功了就没再尝试.
pom文件中,导入插件依赖:
<dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2</version> </dependency>
maven-shade-plugin
尝试了3.0.0和3.0.1都提示有xxx类缺失问题,怀疑是和maven-compiler-plugin
版本适配问题,但网上也没搜索到两者相关的版本号,于是我把shade降到了2.4.2可以正常打包.添加插件
maven-shade-plugin
:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2 </version> <configuration> <!-- put your configurations here --> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!-- 如果有启动主类,此处可以具体填写该类地址,https://blog.youkuaiyun.com/h70614959/article/details/41697189 --> <mainClass></mainClass> </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> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin>
此处
transformers
如上assembly插件处。添加
maven-compiler-plugin
插件:
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
单独的使用
maven-shade-plugin
,eclipse里用maven update project的时候,会把项目jdk默认成1.5.终端使用mvn clean package
也会提示JDK版本的问题.所以用这个指定JDK版本.
三丶打包问题
eclipse maven更新步骤:
右击项目->maven->update project->勾上Force Update of Snapshots/Releases ,确认网络没有问题(小心自己的网代理),点击OK.
其余各种遇到的坑如上.下面附上最终的pom.xml完整版.
<?xml version="1.0" encoding="UTF-8"?> <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.littlersmall.rabbitmq-access</groupId> <artifactId>rabbitmq-access</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-hadoop</artifactId> <version>2.5.0.RELEASE</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2</version> </dependency> </dependencies> <build> <!-- 打包后的jar名字 --> <finalName>rabbitmq</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>true</filtering> </resource> </resources> <outputDirectory> ${basedir}/src/main/webapp/WEB-INF/classes </outputDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2 </version> <configuration> <!-- put your configurations here --> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!-- 如果有启动主类,此处可以具体填写该类地址,https://blog.youkuaiyun.com/h70614959/article/details/41697189 --> <mainClass></mainClass> </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> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>