maven的pom.xml中配置的<profile>会替换掉原来对应的配置项。(执行maven命令是加 -P id 才会起作用)
比如如下的pom.xml配置文件:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>gbss-trade-parent</artifactId> <groupId>com.infinitus.gbss.trade</groupId> <version>2.0.0-SNAPSHOT</version> <relativePath>../gbss-trade-parent/pom.xml</relativePath> </parent> <artifactId>gbss-trade-webapp</artifactId> <name>gbss-webapp Maven Webapp</name> <packaging>war</packaging> <properties> <maven.build.timestamp.format>yyMMdd-HHmm</maven.build.timestamp.format> <buildNumber>${maven.build.timestamp}</buildNumber> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <m.version>${project.version}-${buildNumber}</m.version> <!-- 执行mvn clean install -Ptest environment属性值会被替换成test --> <environment>local</environment> <warName>gbss-trade-${environment}</warName> </properties> <!-- profile定义 --> <profiles> <profile> <id>test</id> <properties> <environment>test</environment> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <warName>${warName}</warName> <archive> <manifestEntries> <Manifest-Version>${m.version}</Manifest-Version> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> </project>
上面的profile很好理解。profile中定义的environment属性会替换掉pom.xml原定义的environment属性。
(需要注意的是,这里的替换不会是将<properties>节点下的内容都替换掉,这也即是标题说的只会将根节点进行替换)
用<properties>来说可能不会特别直观,下面在<profiles>中定义一个插件来说事,而且在pom.xml的<build>下也定义这个插件,pom.xml的配置如下所示:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>gbss-trade-dist</artifactId> <groupId>com.infinitus.gbss.trade</groupId> <version>2.0.0-SNAPSHOT</version> </parent> <artifactId>gbss-trade-webapp</artifactId> <packaging>war</packaging> <name>gbss-webapp Maven Webapp</name> <url>https://gbss.infinitus.com.cn/gbss-trade</url> <properties> <maven.build.timestamp.format>yyMMdd-HHmm</maven.build.timestamp.format> <buildNumber>${maven.build.timestamp}</buildNumber> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--environment 属性的值会被<profile>中的id=test中定义的属性替换掉--> <environment>local</environment> <finalName>gbss-trade-${local}</finalName> <m.version>${project.version}-${buildNumber}</m.version> </properties> <build> <plugins> <!-- 这里定义了一个war插件,但是只是配置了 warName跟archive信息,在profile中也定义了一个war插件,配置了webResources信息 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <warName>${finalName}</warName> <archive> <manifestEntries> <Manifest-Version>${m.version}</Manifest-Version> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>test</id> <properties> <environment>test</environment> </properties> <build> <plugins> <!-- profile中配置了一个war插件,这里只配置了webResources信息,如果pom.xml原配的war插件也配置了webResources信息,则会替换掉原来的,如果原来的war插件没有配置webResources信息,则会添加到war插件中去,即即相当于原配的war插件多了一个配置项信息,对于这个的理解,其实就跟spring中的<bean parent='parentBean' />的效果差不多 --> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <webResources> <resource> <filtering>true</filtering> <directory>src/main/deploy/test</directory> <includes> <include>WEB-INF/web.xml</include> </includes> </resource> </webResources> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>
具体的解释及要表达的内容都在上面xml代码的注释中了,至于对这段注释的理解,需要敲代码去验证,验证的依据无非就是看看原配的war插件的配置项跟profile中定义的war插件的配置项是否都起作用了,若起作用,说明我的理解是对的。(我已经验证过了,有兴趣的朋友可以尝试一下)。