現實中一個項目每每須要部署到多個環境,如生產環境,測試環境等,可是各個環境的配置會有所不一樣.若是項目用到了maven可使用properties-maven-plugin插件解決這個問題.apache
在pom.xml裏maven
添加profiles測試
<profiles> <!-- 默認使用dev配置打包 命令 mvn package --> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <environment>dev</environment> </properties> </profile> <!--生產環境 mvn package -Pproduction--> <profile> <id>production</id> <properties> <environment>production</environment> </properties> </profile> </profiles>
添加pluginui
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-2</version> <executions> <execution> <id>default-cli</id> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>${basedir}/profiles/${environment}.properties</file> </files> </configuration> </execution> </executions> </plugin>
pom.xml完整的配置url
<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.test</groupId> <artifactId>test</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>com.test.properties</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> <profiles> <!-- 默認使用dev配置打包 命令 mvn package --> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <environment>dev</environment> </properties> </profile> <!--生產環境 mvn package -Pproduction--> <profile> <id>production</id> <properties> <environment>production</environment> </properties> </profile> </profiles> <build> <finalName>${project.artifactId}-${project.version}</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-2</version> <executions> <execution> <id>default-cli</id> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>${basedir}/profiles/${environment}.properties</file> </files> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
dev.propertiesspa
log.level=DEBUG
production.properties插件
log.level=ERROR
log.propertiescode
level=${log.level}
使用如下命令:xml
mvn packageci
生成的war文件log.properties的內容爲level=DEBUG
mvn package -Pproduction
生成的war文件log.properties的內容爲level=ERROR