1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Maven在POM中使用profile方便的切换war和jar的制作

Maven在POM中使用profile方便的切换war和jar的制作

时间:2022-05-24 16:29:40

相关推荐

Maven在POM中使用profile方便的切换war和jar的制作

1 问题描述

由于在工作中,经常需要把项目制作成war包,然后部署到容器中,而在IDEA中可以很方便的直接调试,便有了如下的两种情形

在开发过程中,不需要频繁的部署到远程容器中,而需要不断的通过Debug调试自己的程序。而在开发调试到了一定阶段,又要直接把项目制作成war包,部署到远程容器

之前遇到一种情形,由于POM中为制作war包,需要修改POM,并移除Tomcat,并且要修改启动类具体过程参见配置SpringBoot方便的切换jar和war,Springboot项目(包含第三方jar包)打成war包方法,Springboot项目(包含第三方jar包)打成jar包方法。

从jar包调试到war包部署,然后再从war包部署到jar包调试,总是要频繁的修改POM,那么有没有一种方式,可以避免修改POM,以一种类似profile多环境的方式,可以在执行mvn命令时通过指定参数(Profile)的形式自动切换呢?

2 POM修改

内容参见配置SpringBoot方便的切换jar和war

2.1 主要修改对象为POM

2.1.1pom文件修改

packaging配置由jar改为war

<packaging>${pom.package}</packaging>

排除tomcat等容器的依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!--移除嵌入式tomcat插件--><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency>

修改build

<build><finalName>ROOT</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target><skip>true</skip><encoding>UTF-8</encoding></configuration></plugin><!--加入plugin--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.1.0</version><configuration><!--如果想在没有web.xml文件的情况下构建WAR,请设置为false。--><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins></build>

配置profile

<profiles><profile><!-- 开发环境 --><id>jar</id><activation><activeByDefault>true</activeByDefault></activation><properties><pom.package>jar</pom.package></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.0.1.RELEASE</version></dependency></dependencies></profile><profile><id>war</id><properties><pom.package>war</pom.package></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.0.1.RELEASE</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency></dependencies></profile></profiles>

2.1.2 入口类修改

添加ServletInitializer

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;import org.springframework.ponentScan;import org.springframework.context.annotation.Profile;/*** 启动类** @Owner: SongQuanHeng* @Time: /6/11-14:24* @Version:* @Change:*/@SpringBootApplication@ComponentScan(value = {"com.example.profile"})public class App extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(App.class, args);System.out.println("程序启动成功");}@Profile(value = {"war"})@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application){return application.sources(App.class);}}

2.2 完整的POM

完整的POM对象如下所示:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/><!-- lookup parent from repository --></parent><!-- mvn dependency:sources -DdownloadSources=true -DdownloadJavadocs=true --><groupId>com.example</groupId><artifactId>profile</artifactId><version>1.3-SNAPSHOT</version><name>profile</name><packaging>${pom.package}</packaging><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- /artifact/org.apache.maven/maven-plugin-api --><dependency><groupId>org.apache.maven</groupId><artifactId>maven-plugin-api</artifactId><version>3.5.0</version></dependency><!-- /artifact/org.codehaus.cargo/cargo-maven2-plugin --><dependency><groupId>org.codehaus.cargo</groupId><artifactId>cargo-maven2-plugin</artifactId><version>1.7.6</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!--移除嵌入式tomcat插件--><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency></dependencies><repositories><repository><id>nexus</id><name>Nexus3</name><url>http://localhost:8081/repository/maven-central/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository></repositories><distributionManagement><repository><id>nexus-release</id><name>Local Nexus Repository</name><url>http://localhost:8081/repository/maven-releases/</url></repository><snapshotRepository><id>nexus-snapshot</id><name>Local Nexus Repository</name><url>http://localhost:8081/repository/maven-snapshots/</url></snapshotRepository></distributionManagement><properties><java.version>1.8</java.version></properties><build><finalName>ROOT</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target><skip>true</skip><encoding>UTF-8</encoding></configuration></plugin><!--加入plugin--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.1.0</version><configuration><!--如果想在没有web.xml文件的情况下构建WAR,请设置为false。--><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins></build><profiles><profile><!-- 开发环境 --><id>jar</id><activation><activeByDefault>true</activeByDefault></activation><properties><pom.package>jar</pom.package></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.0.1.RELEASE</version></dependency></dependencies></profile><profile><id>war</id><properties><pom.package>war</pom.package></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.0.1.RELEASE</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency></dependencies></profile></profiles></project>

3 POM使用

3.1 IDEA直接调试

3.2 IDEA制作war包

3.2.1 选择profile制作war包

控制台打印如下:

"C:\Program Files\Java\jdk1.8.0_131\bin\java" -Dmaven.multiModuleProjectDirectory=D:\600-Git\profile\profile "-Dmaven.home=C:\Program Files\apache-maven-3.5.4" "-Dclassworlds.conf=C:\Program Files\apache-maven-3.5.4\bin\m2.conf" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA .1\lib\idea_rt.jar=53936:C:\Program Files\JetBrains\IntelliJ IDEA .1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\apache-maven-3.5.4\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=.1 -s C:\Users\lenovo\.m2\settings.xml package -f pom.xml -P war[INFO] Scanning for projects...[WARNING] [WARNING] Some problems were encountered while building the effective model for com.example:profile:war:1.3-SNAPSHOT[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework.boot:spring-boot-starter-web:jar -> duplicate declaration of version (?) @ line 52, column 15[WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.[WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.[WARNING] [INFO] [INFO] ------------------------< com.example:profile >-------------------------[INFO] Building profile 1.3-SNAPSHOT[INFO] --------------------------------[ war ]---------------------------------[INFO] [INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ profile ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Copying 4 resources[INFO] Copying 0 resource[INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ profile ---[INFO] Changes detected - recompiling the module![INFO] Compiling 3 source files to D:\600-Git\profile\profile\target\classes[INFO] [INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ profile ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory D:\600-Git\profile\profile\src\test\resources[INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ profile ---[INFO] Not compiling test sources[INFO] [INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ profile ---[INFO] No tests to run.[INFO] [INFO] --- maven-war-plugin:3.1.0:war (default-war) @ profile ---[INFO] Packaging webapp[INFO] Assembling webapp [profile] in [D:\600-Git\profile\profile\target\ROOT][INFO] Processing war project[INFO] Webapp assembled in [3895 msecs][INFO] Building war: D:\600-Git\profile\profile\target\ROOT.war[INFO] [INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) @ profile ---[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 55.344 s[INFO] Finished at: -08-24T15:37:08+08:00[INFO] ------------------------------------------------------------------------Process finished with exit code 0

3.2.2 拷贝war到webapps

在演示时,使用Tomcat容器来进行war正常工作

拷贝ROOT.war到Tomcat的webapps目录下

执行bin目录下的startup.bat

特别注意:当改成war包的时候,application.properties配置的server.port和server.servlet.context-path就无效了,遵从war容器的安排。

上述执行图示也说明了该问题。虽然在application.properties中指定了端口为8086,但实际项目运行时,使用的端口为8080

3.2.3 Postman测试

3总结

上述的过程就是通过在POM中添加profiles的方式,通过外部命令指定profile的方式灵活的生成war包和jar包,并且方便快速的在IDEA中调试的过程。

工作在于积累和提高效率,如果一个问题,你可以在第一次解决的时候把解决的过程记录下来,那么下次当你遇到相同的问题,能够使问题快速得到解决,那么这就是成长。

关于态度,当领导突然给你一个任务时,千万不要顺口就反对和排斥,工作时应该抱持的态度是开放,而不是抵制,所以先不要急着反对,而是静下来,当你遇到相同的情境时,没有张口就反对,那么这就是成长。

看电视剧《全职高手》,昨天兴欣战队失利,叶修千机伞被毁,团队成员都变得失落,沮丧,吃饭时大家相互推诿责任,彼此指责,这个时候不要也随着自己的情绪而相互职责,而是静下来承受,因为只有做的不好的时候,才会出现抱怨彼此,不妨静下来,不让自己陷入抱怨别人的情境中,就算被别人抱怨,也别急着反击,而是承担下来,增加自己的心量,让自己的胸怀更加宽广,我一直相信,一个人胸怀有多大,就会有多大的前景,所以无论是工作,还是生活,都最好不要抱怨和牢骚,因为抱怨和牢骚对于破冰毫无益处,而只会拉低自己的能量状态,不如好好反思检讨。

4 代码下载

Git地址

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。