在命令行使用构建配置文件时,是-P,好比:mvn -Pinput
注意:这里的构建配置文件并非一个真正的文件,而是经过指定参数来作特定的事。html
如下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_profiles.html:java
当时此教程的例子是在2.0版本,而新的3.0版本只是增长了一点,具体能够参考官网http://maven.apache.org/guides/introduction/introduction-to-profiles.htmlgit
什么是构建配置文件?github
构建配置文件(A Build profile) 是一系列的配置项的值,能够用来设置或者覆盖Maven构建默认值。使用构建配置文件,你能够为不一样的环境,好比说生产环境(Producation)和开发(Development)环境,定制构建方式。数据库
配置文件在pom.xml文件中使用activeProfiles或者profiles元素指定,而且能够经过各类方式触发。配置文件在构建时修改POM,而且用来给参数设定不一样的目标环境(好比说,开发(Development)、测试(Testing)和生产环境(Producation)中数据库服务器的地址)。apache
构建配置文件的类型服务器
构建配置文件大致上有三种类型maven
类型 | 在哪定义 |
---|---|
项目级(Per Project) | 定义在项目的POM文件pom.xml中 |
用户级 (Per User) | 定义在Maven的设置xml文件中 (%USER_HOME%/.m2/settings.xml) |
全局(Global) | 定义在Maven全局的设置xml文件中 (%M2_HOME%/conf/settings.xml) |
配置文件激活ide
Maven的构建配置文件能够经过多种方式激活。测试
官方配置文件激活示例
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
http://maven.apache.org/ref/2.2.1/maven-profile/profiles.html
实践配置文件激活示例
新建的项目结构以下:
其中在src/main/resources文件夹下有三个用于测试文件:
文件名 | 描述 |
---|---|
env.properties | 若是未指定配置文件时默认使用的配置。 |
env.test.properties | 当测试配置文件使用时的测试配置。 |
env.prod.properties | 当生产配置文件使用时的生产配置。 |
注意:这三个配置文件并非表明构建配置文件的功能,而是用于本次测试的目的;好比,我指定了构建配置文件为prod时,项目就使用envprod.properties文件。
注意:下面的例子仍然是使用AntRun插件,由于此插件能绑定Maven生命周期阶段,并经过Ant的标签不用编写一点代码便可输出信息、复制文件等,经此而已。其他的与本次构建配置文件无关。
一、显示配置文件激活
pom.xml配置以下:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>testproject</artifactId> <packaging>jar</packaging> <version>0.1-SNAPSHOT</version> <name>testproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.test.properties</echo> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.properties</echo> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.prod.properties</echo> <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
注意:构建配置文件采用的是<profiles>节点。
说明:上面新建了三个<profiles>,其中<id>区分了不一样的<profiles>执行不一样的AntRun任务;而AntRun的任务能够这么理解,AntRun监听test的Maven生命周期阶段,当Maven执行test时,就除了发AntRun的任务,任务里面为输出文本并复制文件到指定的位置;而至于要执行哪一个AntRun任务,此时构建配置文件起到了传输指定的做用,好比,经过命令行参数输入指定的<id>。
执行命令:
mvn test -Ptest
提示:第一个test为Maven生命周期阶段,第2个test为为构建配置文件指定的<id>参数,这个参数经过-P来传输,固然,它能够是prod或者normal这些由你定义的<id>。
运行的结果以下:
能够看出成功的触发了AntRun的任务。而且是对应构建配置文件下的<id>为test的任务。
再测试其他两个命令,结果以下:
二、经过Maven设置激活配置文件
打开%USER_HOME%/.m2目录下的settings.xml文件,其中%USER_HOME%表明用户主目录。若是setting.xml文件不存在就直接拷贝%M2_HOME%/conf/settings.xml到.m2目录,其中%M2_HOME%表明Maven的安装目录。对于为何能够这样作,参考:http://www.cnblogs.com/EasonJim/p/6827058.html
配置setting.xml文件,增长<activeProfiles>属性:
<settings 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/settings-1.0.0.xsd"> ... <activeProfiles> <activeProfile>test</activeProfile> </activeProfiles> </settings>
执行命令:
mvn test
提示:此时不须要使用-Ptest来输入参数了,上面的setting.xml文件的<activeprofile>已经指定了test参数,代替了。
提示2:一样可使用在%M2_HOME%/conf/settings.xml的文件进行配置,效果一致。
执行结果:
三、经过环境变量激活配置文件
先把上一步测试的setting.xml值所有去掉。
而后在pom.xml里面的<id>为test的<profile>节点,加入<activation>节点:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>testproject</artifactId> <packaging>jar</packaging> <version>0.1-SNAPSHOT</version> <name>testproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <activation> <property> <name>env</name> <value>test</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.test.properties</echo> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.properties</echo> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.prod.properties</echo> <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
执行命令:
mvn test -Denv=test
提示:上面使用-D传递环境变量,其中evn对应刚才设置的<name>值,test对应<value>。
提示2:在Windows 10上测试了系统的环境变量,可是不生效,因此,只能经过-D传递。
执行结果:
四、经过操做系统激活配置文件
五、经过文件的存在或者缺失激活配置文件
六、经过JDK的版本激活配置文件
...
更多激活配置,能够参考官方的例子:http://maven.apache.org/guides/introduction/introduction-to-profiles.html
测试工程:https://github.com/easonjim/5_java_example/tree/master/maventest/test4/test4/testproject