详解pom.xml中build和profile

整理不易,不喜勿喷。谢谢

SpringBoot — 整合Ldap.
SpringBoot — 整合Spring Data JPA.
SpringBoot — 整合Elasticsearch.
SpringBoot — 整合spring-data-jpa和spring-data-elasticsearch.
SpringBoot — 整合thymeleaf.
SpringBoot — 注入第三方jar包.
SpringBoot — 整合Redis.
Springboot — 整合slf4j打印日志.
Springboot — 整合定时任务,自动执行方法.
Springboot — 配置多数据源,使用JdbcTemplate以及NamedParameterJdbcTemplate.
Sprignboot — 详解pom.xml中build和profile.
SpringBoot — 监控.
SpringBoot — 缓存Cache/Redis.
SpringBoot与Zookeeper.
Git的使用.

1.pom.xml配置

1.基础配置

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  
    <!-- 模型版本。maven2.0必须是这样写,现在是maven2唯一支持的版本 -->  
    <modelVersion>4.0.0</modelVersion>  
  
    <!-- 公司或者组织的唯一标志,并且配置时生成的路径也是由此生成, 如com.winner.trade,maven会将该项目打成的jar包放本地路径:/com/winner/trade -->  
    <groupId>com.winner.trade</groupId>  
  
    <!-- 本项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->  
    <artifactId>trade-core</artifactId>  
  
    <!-- 本项目目前所处的版本号 -->  
    <version>1.0.0-SNAPSHOT</version>  
  
    <!-- 打包的机制,如pom,jar, maven-plugin, ejb, war, ear, rar, par,默认为jar -->  
    <packaging>jar</packaging>  
  
    <!-- 帮助定义构件输出的一些附属构件,附属构件与主构件对应,有时候需要加上classifier才能唯一的确定该构件 不能直接定义项目的classifer,因为附属构件不是项目直接默认生成的,而是由附加的插件帮助生成的 -->  
    <classifier>...</classifier>  
  
    <!-- 定义本项目的依赖关系 -->  
    <dependencies>  
  
        <!-- 每个dependency都对应这一个jar包 -->  
        <dependency>  
  
            <!-- 一般情况下,maven是通过groupId、artifactId、version这三个元素值(俗称坐标)来检索该构件, 然后引入你的工程 -->    
            <groupId>com.winner.trade</groupId>  
            <artifactId>trade-test</artifactId>  
            <version>1.0.0-SNAPSHOT</version>  
  
            <!-- maven认为,程序对外部的依赖会随着程序的所处阶段和应用场景而变化,所以maven中的依赖关系有作用域(scope)的限制。 -->  
            <!--scope包含如下的取值:compile(编译范围)、provided(已提供范围)、runtime(运行时范围)、test(测试范围)、system(系统范围) -->  
            <scope>test</scope>  
  
            <!-- 设置指依赖是否可选,默认为false,即子项目默认都继承:true,则子项目必需显示的引入,与dependencyManagement里定义的依赖类似  -->  
            <optional>false</optional>  
  
            <!-- 屏蔽依赖关系。 比如项目中使用的libA依赖某个库的1.0版,libB依赖某个库的2.0版,现在想统一使用2.0版,就应该屏蔽掉对1.0版的依赖 -->  
            <exclusions>  
                <exclusion>  
                    <groupId>org.slf4j</groupId>  
                    <artifactId>slf4j-api</artifactId>  
                </exclusion>  
            </exclusions>  
  
        </dependency>  
  
    </dependencies>  
  
    <!-- 为pom定义一些常量,在pom中的其它地方可以直接引用 使用方式 如下 :${file.encoding} -->  
    <properties>  
        <file.encoding>UTF-8</file.encoding>  
        <java.source.version>1.5</java.source.version>  
        <java.target.version>1.5</java.target.version>  
    </properties>  
    ...  
</project>  

2.构建配置

<build>  
  
    <!-- 产生的构件的文件名,默认值是${artifactId}-${version}-->  
    <finalName>myPorjectName</finalName>  
  
    <!-- 构建产生的所有文件存放的目录,默认为${basedir}/target,即项目根目录下的target -->  
    <directory>${basedir}/target</directory>  
  
    <!--当项目没有规定目标(Maven2叫做阶段(phase))时的默认值, -->  
    <!--必须跟命令行上的参数相同例如jar:jar,或者与某个阶段(phase)相同例如install、compile等 -->  
    <defaultGoal>install</defaultGoal>  
  
    <!--当filtering开关打开时,使用到的过滤器属性文件列表。 -->  
    <!--项目配置信息中诸如${spring.version}之类的占位符会被属性文件中的实际值替换掉 -->  
    <filters>  
        <filter>../filter.properties</filter>  
    </filters>  
  
    <!--项目相关的所有资源路径列表,例如和项目相关的配置文件、属性文件,这些资源被包含在最终的打包文件里。 -->  
    <resources>  
        <resource>  
  
            <!--描述了资源的目标路径。该路径相对target/classes目录(例如${project.build.outputDirectory})。 -->  
            <!--举个例子,如果你想资源在特定的包里(org.apache.maven.messages),你就必须该元素设置为org/apache/maven/messages。 -->  
            <!--然而,如果你只是想把资源放到源码目录结构里,就不需要该配置。 -->  
            <targetPath>resources</targetPath>  
  
            <!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。 -->  
            <filtering>true</filtering>  
  
            <!--描述存放资源的目录,该路径相对POM路径 -->  
            <directory>src/main/resources</directory>  
  
            <!--包含的模式列表 -->  
            <includes>  
                <include>**/*.properties</include>  
                <include>**/*.xml</include>  
            </includes>  
  
            <!--排除的模式列表 如果<include><exclude>划定的范围存在冲突,以<exclude>为准 -->  
            <excludes>  
                <exclude>jdbc.properties</exclude>  
            </excludes>  
  
        </resource>  
    </resources>  
  
    <!--单元测试相关的所有资源路径,配制方法与resources类似 -->  
    <testResources>  
        <testResource>  
            <targetPath />  
            <filtering />  
            <directory />  
            <includes />  
            <excludes />  
        </testResource>  
    </testResources>  
  
    <!--项目源码目录,当构建项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->  
    <sourceDirectory>${basedir}\src\main\java</sourceDirectory>  
  
    <!--项目脚本源码目录,该目录和源码目录不同, <!-- 绝大多数情况下,该目录下的内容会被拷贝到输出目录(因为脚本是被解释的,而不是被编译的)-->  
    <scriptSourceDirectory>${basedir}\src\main\scripts  
    </scriptSourceDirectory>  
  
    <!--项目单元测试使用的源码目录,当测试项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->  
    <testSourceDirectory>${basedir}\src\test\java</testSourceDirectory>  
  
    <!--被编译过的应用程序class文件存放的目录。 -->  
    <outputDirectory>${basedir}\target\classes</outputDirectory>  
  
    <!--被编译过的测试class文件存放的目录。 -->  
    <testOutputDirectory>${basedir}\target\test-classes  
    </testOutputDirectory>  
  
    <!--项目的一系列构建扩展,它们是一系列build过程中要使用的产品,会包含在running bulid‘s classpath里面。 -->  
    <!--他们可以开启extensions,也可以通过提供条件来激活plugins。 -->  
    <!--简单来讲,extensions是在build过程被激活的产品 -->  
    <extensions>  
  
        <!--例如,通常情况下,程序开发完成后部署到线上Linux服务器,可能需要经历打包、 -->  
        <!--将包文件传到服务器、SSH连上服务器、敲命令启动程序等一系列繁琐的步骤。 -->  
        <!--实际上这些步骤都可以通过Maven的一个插件 wagon-maven-plugin 来自动完成 -->  
        <!--下面的扩展插件wagon-ssh用于通过SSH的方式连接远程服务器, -->  
        <!--类似的还有支持ftp方式的wagon-ftp插件 -->  
        <extension>  
            <groupId>org.apache.maven.wagon</groupId>  
            <artifactId>wagon-ssh</artifactId>  
            <version>2.8</version>  
        </extension>  
  
    </extensions>  
  
    <!--使用的插件列表 。 -->  
    <plugins>  
        <plugin>  
            <groupId></groupId>  
            <artifactId>maven-assembly-plugin</artifactId>  
            <version>2.5.5</version>  
  
            <!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。 -->  
            <executions>  
                <execution>  
  
                    <!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标 -->  
                    <id>assembly</id>  
  
                    <!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段 -->  
                    <phase>package</phase>  
  
                    <!--配置的执行目标 -->  
                    <goals>  
                        <goal>single</goal>  
                    </goals>  
  
                    <!--配置是否被传播到子POM -->  
                    <inherited>false</inherited>  
  
                </execution>  
            </executions>  
  
            <!--作为DOM对象的配置,配置项因插件而异 -->  
            <configuration>  
                <finalName>${finalName}</finalName>  
                <appendAssemblyId>false</appendAssemblyId>  
                <descriptor>assembly.xml</descriptor>  
            </configuration>  
  
            <!--是否从该插件下载Maven扩展(例如打包和类型处理器), -->  
            <!--由于性能原因,只有在真需要下载时,该元素才被设置成true-->  
            <extensions>false</extensions>  
  
            <!--项目引入插件所需要的额外依赖 -->  
            <dependencies>  
                <dependency>...</dependency>  
            </dependencies>  
  
            <!--任何配置是否被传播到子项目 -->  
            <inherited>true</inherited>  
  
        </plugin>  
    </plugins>  
  
    <!--主要定义插件的共同元素、扩展元素集合,类似于dependencyManagement, -->  
    <!--所有继承于此项目的子项目都能使用。该插件配置项直到被引用时才会被解析或绑定到生命周期。 -->  
    <!--给定插件的任何本地配置都会覆盖这里的配置 -->  
    <pluginManagement>  
        <plugins>...</plugins>  
    </pluginManagement>  
  
</build>  

3.profile配置

<profiles>  
  
    <!--根据环境参数或命令行参数激活某个构建处理 -->  
    <profile>  
        <!--自动触发profile的条件逻辑。Activation是profile的开启钥匙。 -->  
        <activation>  
  
            <!--profile默认是否激活的标识 -->  
            <activeByDefault>false</activeByDefault>  
  
            <!--activation有一个内建的java版本检测,如果检测到jdk版本与期待的一样,profile被激活。 -->  
            <jdk>1.7</jdk>  
  
            <!--当匹配的操作系统属性被检测到,profile被激活。os元素可以定义一些操作系统相关的属性。 -->  
            <os>  
  
                <!--激活profile的操作系统的名字 -->  
                <name>Windows XP</name>  
  
                <!--激活profile的操作系统所属家族('windows') -->  
                <family>Windows</family>  
  
                <!--激活profile的操作系统体系结构 -->  
                <arch>x86</arch>  
  
                <!--激活profile的操作系统版本 -->  
                <version>5.1.2600</version>  
  
            </os>  
  
            <!--如果Maven检测到某一个属性(其值可以在POM中通过${名称}引用),其拥有对应的名称和值,Profile就会被激活。 -->  
            <!-- 如果值字段是空的,那么存在属性名称字段就会激活profile,否则按区分大小写方式匹配属性值字段 -->  
            <property>  
  
                <!--激活profile的属性的名称 -->  
                <name>mavenVersion</name>  
  
                <!--激活profile的属性的值 -->  
                <value>2.0.3</value>  
  
            </property>  
  
            <!--提供一个文件名,通过检测该文件的存在或不存在来激活profile。missing检查文件是否存在,如果不存在则激活profile。 -->  
            <!--另一方面,exists则会检查文件是否存在,如果存在则激活profile。 -->  
            <file>  
  
                <!--如果指定的文件存在,则激活profile。 -->  
                <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</exists>  
  
                <!--如果指定的文件不存在,则激活profile。 -->  
                <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</missing>  
  
            </file>  
  
        </activation>  
        <id />  
        <build />  
        <modules />  
        <repositories />  
        <pluginRepositories />  
        <dependencies />  
        <reporting />  
        <dependencyManagement />  
        <distributionManagement />  
        <properties />  
    </profile>  

2.打包跳过测试

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.5.4</version>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.18.1</version>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
		</plugins>
	</build>

3.多环境打包

  • Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Maven操作的可能。
// An highlighted block
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        
        <!--打包的时候,将静态资源也都打进去 --> 
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.yml</include>
                    <include>bootstrap.yml</include>
                    <include>application-${spring.profiles.active}.yml</include>
                    <include>bootstrap-${spring.profiles.active}.yml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources/conf/${profile.env}</directory>
            </resource>
        </resources>
    </build>

    <profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <!-- 默认环境-->
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
                <profile.env>dev</profile.env>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
                <profile.env>prod</profile.env>
            </properties>
        </profile>
         <!-- test环境 -->
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
                <profile.env>test</profile.env>
            </properties>
        </profile>
    </profiles>

三个application.properties

前面resources中配置了路径:<directory>src/main/resources/conf/${profile.env}</directory>
项目结构目录:
|--src
| |--main
| | |--java
| | | |--com.xxxx
| | |--resources
| | | |--conf
| | | | |--dev
| | | | | |--application.properties
| | | | |--pro
| | | | | |--application.properties
| | | | |--test
| | | | | |--application.properties
| | | |--static
| | | | |--css,js,img,html
| |--test
|--target

application.properties -dev
env=dev
db.url=xxx.xxx.xxx.xxx
db.username=IS_DT
db.password=IS_DT

application.properties -pro
env=pro
db.url=xxx.xxx.xxx.xxx
db.username=IS_DT
db.password=IS_DT

application.properties -test
env=test
db.url=xxx.xxx.xxx.xxx
db.username=IS_DT
db.password=IS_DT

//打包
mvn clean package -P pro
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世经纶『一页書』

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值