赞
踩
1、创建指定目录
src/main/resources/uat 目录下properties是验证环境的配置文件
src/main/resources/prd 目录下properties是生产环境的配置文件
2、pom.xml内配置profiles
- <profiles>
- <!-- <profile>
- <id>dev</id>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- <properties>
- <package.environment>dev</package.environment>
- </properties>
- </profile> -->
- <profile>
- <id>uat</id>
- <properties>
- <package.environment>uat</package.environment>
- </properties>
- </profile>
- <profile>
- <id>prd</id>
- <properties>
- <package.environment>prd</package.environment>
- </properties>
- </profile>
- </profiles>
注:可以在工程下xml、properties文件中使用${package.environment}获取数据。
3、pom.xml内build标签下配置plugins
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>${maven.compiler.source}</source>
- <target>${maven.compiler.target}</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <!-- 不同环境的加载指定配置 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <executions>
- <execution>
- <id>copy-resources</id>
- <phase>compile</phase>
- <goals>
- <goal>copy-resources</goal>
- </goals>
- <configuration>
- <!-- 覆盖原有文件 -->
- <overwrite>true</overwrite>
- <outputDirectory>${project.build.outputDirectory}</outputDirectory>
- <!-- 也可以用下面这样的方式(指定相对url的方式指定outputDirectory) -->
- <!-- <outputDirectory>target/classes</outputDirectory> -->
- <!-- 待处理的资源定义 -->
- <resources>
- <resource>
- <!-- 指定resources插件处理指定目录下的资源文件 -->
- <directory>src/main/resources/${package.environment}</directory>
- <filtering>false</filtering>
- </resource>
- </resources>
- </configuration>
- <inherited></inherited>
- </execution>
- </executions>
- </plugin>
- </plugins>
4、pom.xml内build标签下配置resources
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>true</filtering>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <!-- 排除uat、prd目录下的文件 -->
-
- <excludes>
- <exclude>uat/**</exclude>
- <exclude>prd/**</exclude>
- </excludes>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>true</filtering>
- </resource>
- </resources>
5、自动化编译打包命令,由-P参数区分
mvn clean package -Dmaven.test.skip=true -Puat
mvn clean package -Dmaven.test.skip=true -Pprd
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。