一.分类
在Maven的pom.xml文件中,存在如下两种<build>:
(1)全局配置(project build)
针对整个项目的所有情况都有效
(2)配置(profile build)
针对不同的profile配置
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<!-- "Project Build" contains elements of the BaseBuild set and the Build set-->
<build>...</build>
<profiles>
<profile>
<!-- "Profile Build" contains elements of the BaseBuild set only -->
<build>...</build>
</profile>
</profiles>
</project>
说明:
一种<build>被称为Project Build,即是<project>的直接子元素。
另一种<build>被称为Profile Build,即是<profile>的直接子元素。
Profile Build包含了基本的build元素,而Project Build还包含两个特殊的元素,即各种<...Directory>和<extensions>。
二. 配置说明
1.基本元素
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
defaultGoal | 执行build任务时,如果没有指定目标,将使用的默认值。 如上配置:在命令行中执行mvn,则相当于执行mvn install |
directory | build目标文件的存放目录,默认在${basedir}/target目录 |
finalName | build目标文件的名称,默认情况为${artifactId}-${version} |
filter | 定义*.properties文件,包含一个properties列表,该列表会应用到支持filter的resources中。 也就是说,定义在filter的文件中的name=value键值对,会在build时代替${name}值应用到resources中。 maven的默认filter文件夹为${basedir}/src/main/filters |
Maven多环境配置 filter
在开发过程中我们不可避免的会遇到需要在多种环境下进行部署,比如开发,测试,生产每个阶段都需要对应使用不同的配置(数据库,日志等等信息)。
而有了filter我们就可以根据不同环境匹配传入相应的参数,更灵活直观的使用参数,可以大大提升我们的效率。
首先需要在pom文件中确定filter和要filter的资源,这是通过在build节点中添加filter和resource来实现的,示例如下:
<filters>
<filter>src/main/filters/filter-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
上述配置表示要对src/main/resources下的资源进行过滤,因为该目录下没有二进制文件,所以没有excluding。过滤时采用的过滤文件为src/main/filters/filter-${env}.properties文件,其中${env}是一个变量,表示当前使用的环境。
我们这里有两种方式为${env}这个变量传入值
1)通过maven命令直接传入
-D代表(Properties属性)
mvn package -D env=dev
这里我们是使用-D 这个命令告诉env这个变量传入的参数是dev
2)通过profile定义传入
这是通过在pom文件中通过profile定义的,如下所示
<properties>
<env>dev</env>
</properties>
<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>
使用下面的命令P代表(Profiles配置文件)
mvn package -P test
在<profiles>指定的<id>中,可以通过-P进行传递或者赋值。
其中properties里面表示默认的变量值,使用maven命令不设定-P时默认使用这个dev。
在测试和部署上线时分别通过maven命令-P传入当前的profile id,这样maven就会将env变量设置为对应的值,从而导致使用不同的filter文件来对resources下的文件进行过滤替换。
例如:当调用maven package时传入-Pdev(因为我们将dev设置为默认,所以也可以不传)参数,则会使用filter-dev.properties中的内容来替换resources目录中的配置文件,具体到我们的项目就是db.properties,内容如下:
......
jdbc.connection.url=${demo.jdbc.url}
jdbc.connection.username=${demo.jdbc.username}
jdbc.connection.password=${demo.jdbc.password}
......
filter-dev.properties文件内容如下:
......
demo.jdbc.url=jdbc:mysql://localhost:3306/localhost?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
demo.jdbc.username=root
demo.jdbc.password=111111
......
这样在编译结束后
db.properties的内容就会变为:
jdbc.connection.url=jdbc:mysql://localhost:3306/xiangmu?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
jdbc.connection.username=root
jdbc.connection.password=111111
2. Resources配置
用于包含或者排除某些资源文件
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
...
</testResources>
...
</build>
resources | 一个resources元素的列表。每一个都描述与项目关联的文件是什么和在哪里 |
targetPath | 指定build后的resource存放的文件夹,默认是basedir。 通常被打包在jar中的resources的目标路径是META-INF |
filtering | true/false,表示为这个resource,filter是否激活 |
directory | 定义resource文件所在的文件夹,默认为${basedir}/src/main/resources |
includes | 指定哪些文件将被匹配,以*作为通配符 |
excludes | 指定哪些文件将被忽略 |
testResources | 定义和resource类似,只不过在test时使用 |
3. plugins配置
用于指定使用的插件
(不同插件内的写法也有所区别,之后会总结一些常用插件的文章)
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.0</version>
<extensions>false</extensions>
<inherited>true</inherited>
<configuration>
<classifier>test</classifier>
</configuration>
<dependencies>...</dependencies>
<executions>...</executions>
</plugin>
</plugins>
</build>
4. pluginManagement配置
pluginManagement的配置和plugins的配置是一样的,只是用于继承,使得可以在孩子pom中使用。
父pom:
<build>
...
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>pre-process-classes</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>pre-process</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
...
</build>
则在子pom中,我们只需要配置:
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
...
</build>