maven打包jar包含依赖jar(spring项目,其余可参考)maven-shade-plugin

本文介绍了如何使用maven-shade-plugin来打包包含依赖的jar,特别是解决Spring项目中由于多个依赖导致的NamespaceHandler找不到的问题。通过添加特定的transformer配置,将spring.handlers和spring.schemas文件append到jar包中,避免了BeanDefinitionParsingException错误。并提供了详细的maven-shade-plugin配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一丶打包工具选择

  1. 网上说法比较多的有maven-shade-pluginmaven-assembly-plugin

  2. 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.

  3. 最后选用 maven-shade-plugin.

二丶打包步骤

探索了2天,终于打包成功,有可能步骤中有多余,但因为工作时间比较紧,成功了就没再尝试.

  1. 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可以正常打包.

  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插件处。

    1. 添加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版本.

三丶打包问题

  1. eclipse maven更新步骤:

    右击项目->maven->update project->勾上Force Update of Snapshots/Releases ,确认网络没有问题(小心自己的网代理),点击OK.

  2. 其余各种遇到的坑如上.下面附上最终的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>
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值