Spring Boot 2关于maven打包插件的使用

spring-boot-maven-plugin插件

https://docs.spring.io/spring-boot/docs/current/maven-plugin/index.html

该插件可以进行运行、打包spring boot项目

1、直接引用

mvn clean compile -T 4 -D maven.test.skip=true
mvn jar:jar -T 4 -D maven.test.skip=true
mvn jar:test-jar -T 4
mvn package
  • 生成springboot相关的jar包
  • 复制resources文件
  • mybatis配置文件与mapper文件放在一起
<build>
	<resources>
		<!--资源文件复制-->
		<resource>
			<directory>${project.basedir}/src/main/resources</directory>
			<targetPath>${project.build.directory}/classes</targetPath>
		</resource>
		<!--mybatis配置文件与mapper文件放在一起-->
		<resource>
			<directory>${project.basedir}/src/main/java</directory>
			<targetPath>${project.build.directory}/classes</targetPath>
			<includes>
				<include>**/*.xml</include>
			</includes>
		</resource>
	</resources>
	<plugins>
		<plugin>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.3.2</version>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>				
			</configuration>
		</plugin>		
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<version>${spring.boot.version}</version>
			<executions>
				<execution>
					<configuration>
						<mainClass>xx.xxx.Application</mainClass>
					</configuration>
					<goals>
						<goal>repackage</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

2、通过参数spring.config.location区分不同环境

  • resources
    • a.xml
    • b.properties
    • env
      • dev
        • application.yml
        • aa.properties
      • test
        • application.yml
        • aa.properties
      • prod
        • application.yml
        • aa.properties
-Dspring.profiles.active=dev -Dspring.config.location=classpath:/env/dev/

此种方式插件配置与第一种模式一样,就是对于代码中的配置文件需要处理一下

@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"${spring.config.location}person.properties"}, encoding = "UTF-8")
public class PersonProperties implements Serializable {
    //
}

3、通过参数profiles区分不同环境

mvn clean compile -T 4 -D maven.test.skip=true -D env=dev
mvn jar:jar -T 4 -D maven.test.skip=true
mvn jar:test-jar -T 4
mvn package
<profiles> 	
	<profile>            
		<id>dev</id>
		<properties>
			<env>dev</env>
		</properties>
	</profile>
	<profile>            
		<id>test</id>
		<properties>
			<env>test</env>
		</properties>
	</profile>      
	<profile>            
		<id>prod</id>
		<properties>
			<env>prod</env>
		</properties>
	</profile>      
</profiles>
<build>
	<resources>
		<!--资源文件复制-->
         <resource>
             <directory>${project.basedir}/src/main/resources</directory>
             <targetPath>${project.build.directory}/classes</targetPath>
             <excludes>
                 <exclude>env/**</exclude>
             </excludes>
         </resource>
		<!--mybatis配置文件与mapper文件放在一起-->
		<resource>
			<directory>${project.basedir}/src/main/java</directory>
			<targetPath>${project.build.directory}/classes</targetPath>
			<includes>
				<include>**/*.xml</include>
			</includes>
		</resource>
		<!--不同环境配置文件-->
        <resource>
            <directory>${project.basedir}/src/main/resources/env/${env}</directory>
            <targetPath>${project.build.directory}/classes</targetPath>
        </resource>
	</resources>
	<plugins>
		<plugin>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.3.2</version>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>                    
			</configuration>
		</plugin>
		<!--https://docs.spring.io/spring-boot/docs/current/maven-plugin/index.html-->
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<version>${spring.boot.version}</version>
			<executions>
				<execution>
					<configuration>
						<mainClass>xxx.xxx.Application</mainClass>
					</configuration>
					<goals>
						<goal>repackage</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值