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

1 问题描述

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

  1. 在开发过程中,不需要频繁的部署到远程容器中,而需要不断的通过Debug调试自己的程序。
  2. 而在开发调试到了一定阶段,又要直接把项目制作成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.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
			
			<exclusions>
				<exclusion>
					<groupId>org.springframework.bootgroupId>
					<artifactId>spring-boot-starter-tomcatartifactId>
				exclusion>
			exclusions>
		dependency>

修改build

<build>
		<finalName>ROOTfinalName>


		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
			<plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.1version>
				<configuration>
					<source>1.8source>
					<target>1.8target>
					<skip>trueskip>
					<encoding>UTF-8encoding>
				configuration>
			plugin>
			
			<plugin>
				<groupId>org.apache.maven.pluginsgroupId>
				<artifactId>maven-war-pluginartifactId>
				<version>3.1.0version>
				<configuration>
					
					<failOnMissingWebXml>falsefailOnMissingWebXml>
				configuration>
			plugin>
		plugins>

	build>

配置profile

<profiles>
		<profile>
			
			<id>jarid>
			<activation>
				<activeByDefault>trueactiveByDefault>
			activation>
			<properties>
				<pom.package>jarpom.package>
			properties>
			<dependencies>
				<dependency>
					<groupId>org.springframework.bootgroupId>
					<artifactId>spring-boot-starter-tomcatartifactId>
					<version>2.0.1.RELEASEversion>
				dependency>
			dependencies>
		profile>
		<profile>
			<id>warid>
			<properties>
				<pom.package>warpom.package>
			properties>
			<dependencies>
				<dependency>
					<groupId>org.springframework.bootgroupId>
					<artifactId>spring-boot-starter-tomcatartifactId>
					<version>2.0.1.RELEASEversion>
					<scope>providedscope>
				dependency>
				<dependency>
					<groupId>javax.servletgroupId>
					<artifactId>javax.servlet-apiartifactId>
					<version>3.1.0version>
					<scope>providedscope>
				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.context.annotation.ComponentScan;
import org.springframework.context.annotation.Profile;

/**
 * 启动类
 *
 * @Owner: SongQuanHeng
 * @Time: 2019/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"})
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
        return application.sources(App.class);
    }
}

2.2 完整的POM

完整的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.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.0.1.RELEASEversion>
		<relativePath/>
		
	parent>
 
	
	<groupId>com.examplegroupId>
	<artifactId>profileartifactId>
	<version>1.3-SNAPSHOTversion>
	<name>profilename>
	<packaging>${pom.package}packaging>


	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starterartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-configuration-processorartifactId>
			<optional>trueoptional>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		
		<dependency>
			<groupId>org.apache.mavengroupId>
			<artifactId>maven-plugin-apiartifactId>
			<version>3.5.0version>
		dependency>

		
		<dependency>
			<groupId>org.codehaus.cargogroupId>
			<artifactId>cargo-maven2-pluginartifactId>
			<version>1.7.6version>
		dependency>


		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
			
			<exclusions>
				<exclusion>
					<groupId>org.springframework.bootgroupId>
					<artifactId>spring-boot-starter-tomcatartifactId>
				exclusion>
			exclusions>
		dependency>
		<dependency>
			<groupId>javax.servletgroupId>
			<artifactId>javax.servlet-apiartifactId>
			<version>3.1.0version>
			<scope>providedscope>
		dependency>

	dependencies>

	<repositories>
		<repository>
			<id>nexusid>
			<name>Nexus3name>
			<url>http://localhost:8081/repository/maven-central/url>
			<snapshots>
				<enabled>falseenabled>
			snapshots>
			<releases>
				<enabled>trueenabled>
			releases>
		repository>
	repositories>


	<distributionManagement>
		<repository>
			<id>nexus-releaseid>
			<name>Local Nexus Repositoryname>
			<url>http://localhost:8081/repository/maven-releases/url>
		repository>
		<snapshotRepository>
			<id>nexus-snapshotid>
			<name>Local Nexus Repositoryname>
			<url>http://localhost:8081/repository/maven-snapshots/url>
		snapshotRepository>
	distributionManagement>

	<properties>
		<java.version>1.8java.version>
	properties>
	<build>
		<finalName>ROOTfinalName>


		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
			<plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.1version>
				<configuration>
					<source>1.8source>
					<target>1.8target>
					<skip>trueskip>
					<encoding>UTF-8encoding>
				configuration>
			plugin>
			
			<plugin>
				<groupId>org.apache.maven.pluginsgroupId>
				<artifactId>maven-war-pluginartifactId>
				<version>3.1.0version>
				<configuration>
					
					<failOnMissingWebXml>falsefailOnMissingWebXml>
				configuration>
			plugin>
		plugins>

	build>
	<profiles>
		<profile>
			
			<id>jarid>
			<activation>
				<activeByDefault>trueactiveByDefault>
			activation>
			<properties>
				<pom.package>jarpom.package>
			properties>
			<dependencies>
				<dependency>
					<groupId>org.springframework.bootgroupId>
					<artifactId>spring-boot-starter-tomcatartifactId>
					<version>2.0.1.RELEASEversion>
				dependency>
			dependencies>
		profile>
		<profile>
			<id>warid>
			<properties>
				<pom.package>warpom.package>
			properties>
			<dependencies>
				<dependency>
					<groupId>org.springframework.bootgroupId>
					<artifactId>spring-boot-starter-tomcatartifactId>
					<version>2.0.1.RELEASEversion>
					<scope>providedscope>
				dependency>
				<dependency>
					<groupId>javax.servletgroupId>
					<artifactId>javax.servlet-apiartifactId>
					<version>3.1.0version>
					<scope>providedscope>
				dependency>
			dependencies>
		profile>
	profiles>

project>

3 POM使用

3.1 IDEA直接调试

Maven在POM中使用profile方便的切换war和jar的制作_第1张图片

3.2 IDEA制作war包

3.2.1 选择profile制作war包

Maven在POM中使用profile方便的切换war和jar的制作_第2张图片
控制台打印如下:

"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 2018.1\lib\idea_rt.jar=53936:C:\Program Files\JetBrains\IntelliJ IDEA 2018.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=2018.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: 2019-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

Maven在POM中使用profile方便的切换war和jar的制作_第3张图片

特别注意:当改成war包的时候,application.properties配置的server.port和server.servlet.context-path就无效了,遵从war容器的安排。
上述执行图示也说明了该问题。虽然在application.properties中指定了端口为8086,但实际项目运行时,使用的端口为8080

3.2.3 Postman测试

Maven在POM中使用profile方便的切换war和jar的制作_第4张图片

3总结

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

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

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

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

4 代码下载

Git地址

你可能感兴趣的:(Git,Maven)