栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Maven打包常见问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Maven打包常见问题

Maven打包常见问题 1. Maven 打包时,无法将scope为system的jar文件打包进war和jar中。

分析问题:

    通过对maven打包的生命周期进行分析,在package阶段未将scope为system的jar打包到war或者jar中。

    maven打包生命周期,通过执行mvn clean package -Dmaven.test.skip=ture -X 打印Debug信息,可知生命周期为

    clean -> validate -> compile -> test -> package -> verify -> install -> deploy

    [DEBUG] Lifecycle default -> [validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-te
    st-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration
    -test, verify, install, deploy]
    [DEBUG] Lifecycle clean -> [pre-clean, clean, post-clean]
    [DEBUG] Lifecycle site -> [pre-site, site, post-site, site-deploy]
    [DEBUG] Lifecycle default -> [validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-te
    st-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration
    -test, verify, install, deploy]
    

    也可以从IDEA中Maven中Lifecycle中显示的顺序得知

    查看pom.xml中执行package命令的plugin插件。对如下pom.xml文件进行分析

    maven-compiler-plugin: maven compile插件 ,compile阶段使用。spring-boot-maven-plugin:Spring Boot的maven插件,使spring boot工程能够以maven的方式运行。提供了一下命令:

    spring-boot:repackage: 默认是goal,在mvn package打包之后,重新打包现有的 JAR 和 WAR 档案,以便它们可以执行
    从命令行使用 java -jar。参考spring boot maven plugin 官方说明

    Repackage existing JAR and WAR archives so that they can be executed from the command line using java -jar. With layout=NONE can also be used simply to package a JAR with nested dependencies (and no main class, so not executable).

    spring-boot:run: 运行spring boot工程

    spring-boot:start: 在mvn integration-test阶段,进行Spring Boot应用生命周期的管理

    spring-boot:stop: 在mvn integration-test阶段,进行Spring Boot应用生命周期的管理

    spring-boot:build-info: 生成使用的构建信息文件build-info.properties

    
        
            
                
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    8
                    8
                    UTF-8
                
            
            
                
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.7.RELEASE
                
                    org.xxx.scheduledemo.ScheduleDemoApplication
                    WAR
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    

    分析spring-boot-maven-plugin插件,查看插件官网信息,可知

    spring-boot:repackage命令在package阶段打包时,默认只会对scope为compile+runtime的依赖进行打包。默认打包时不包括scope为system的依赖。可通过修改默认属性值解决:includeSystemScope 为 true。


解决方法: 使用spring-boot-maven-plugin插件,设置includeSystemScope为true即可。

2. 使用maven-war-plugin插件之后,spring-boot-maven-plugin插件的includeSystemScope不生效,无法将scope为system的依赖打包进war或者jar。

如下pom.xml文件


        
            
                com.rimerosolutions.maven.plugins
                wrapper-maven-plugin
                0.0.5
                
                    true
                    MD5
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${springboot.version}
                
                    ${mainClassName}
                    true
                    ${isExecutable}
                    WAR
                
                
                    
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.6
                
                    cas
                    false
                    false
                    
                        false
                        ${manifestFileToUse}
                    
                    
                        
                            org.apereo.cas
                            cas-server-webapp${app.server}
                            
                                **/hibernate-validator-5.4.1.Final.jar
                                **/validation-api-1.1.0.Final.jar
                                **/el-api-2.2.jar
                            
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.3
            
        
        cas
    

分析问题:

    分析上面使用的plugin插件,可以知道在package阶段执行的插件为maven-war-plugin、spring-boot-maven-plugin.猜测:执行顺序为maven-war-plugin -> spring-boot-maven-plugin. 实际是maven-war-plugin先将工程打包成war文件,然后在通过spring-boot-maven-plugin插件,使war文件可以通过命令行java -jar xxxx.war的形式运行。

    wrapper-maven-plugin:Wrapper是一个maven插件,用于封装提供maven项目构建时所需的一切;使用maven wrapper就可以很好的确保所有参与项目者使用相同的maven版本,同时还不会影响其它项目spring-boot-maven-plugin:Spring Boot的maven插件,使spring boot工程能够以maven的方式运行。提供了一下命令maven-war-plugin:收集web应用程序的依赖,class和resource文件,并将其打包成war。在package阶段运行。maven-compiler-plugin: maven compile插件 ,compile阶段使用。

    验证maven-war-plugin -> spring-boot-maven-plugin的执行顺序,确保猜测是正确。

    查看maven-war-plugin插件的打war时的scope范围为compile+runtime.

    原因显而易见: maven-war-plugin打包成war时,未将scope为system的依赖打包至其中。


解决办法:

maven-war-plugin插件中不存在类似于spring-boot-maven-plugin插件中includeSystemScope的属性,但可通过添加webResources属性将需要jar打包到war中。添加如下配置:


    org.apache.maven.plugins
    maven-war-plugin
    2.6
    
        cas
        false
        false
        
            false
            ${manifestFileToUse}
        
        
            
                org.apereo.cas
                cas-server-webapp${app.server}
                
                    **/hibernate-validator-5.4.1.Final.jar
                    **/validation-api-1.1.0.Final.jar
                    **/el-api-2.2.jar
                
            
        
        
        
            
                /src/main/resources/lib
                WEB-INF/lib/
                
                    **/*.jar
                
            
        
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756251.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号