赞
踩
本地的测试配置文件与内测的配置,甚至线上的配置不一样, 以前比较土的做法就是每次复制粘贴。然后不停的来回改文件。
现在是将本地的开发环境,内测的,生产环境的,各个不同的配置文件放在配置文件夹里。例如:
在项目的资源文件夹 resources下面再新建四个文件夹:
local: 表示本地开发环境的配置文件
internal : 表示 内测环境配置文件
product : 表示生产环境配置环境
除了三这不同的配置文件之外,还有一些是共同的,与环境无关的,就可以放在同一个文件夹里,统一管理,这个文件夹就是:
public : 表示共同的配置文件
结果如一下:
相应在的pom里进行设置:
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- <excludes>
- <exclude>local/*</exclude>
- <exclude>internal/*</exclude>
- <exclude>product/*</exclude>
- <exclude>public/*</exclude>
- </excludes>
- </resource>
- </resources>
完整理的 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>net.saindy</groupId>
- <artifactId>faq</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>faq Maven Webapp</name>
- <url>http://maven.apache.org</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <tomcat.version>2.1</tomcat.version>
- <slf4j.version>1.7.5</slf4j.version>
- <log4j.version>1.2.17</log4j.version>
- <java.version>1.7</java.version>
- <commons-io.version>2.4</commons-io.version>
- <java.encoding>utf8</java.encoding>
- <java.home>D:\Development\Java\jdk1.7.0_76\jre</java.home>
-
- <package.environment>local</package.environment>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
-
- <!-- web setting start -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- <type>jar</type>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.0.1</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.2</version>
- <scope>provided</scope>
- </dependency>
- <!-- web setting end -->
-
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>${commons-io.version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.4</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons.lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.1</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.2.1</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-email</artifactId>
- <version>1.2</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- <version>2.4.2</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.5</version>
- </dependency>
-
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
-
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.38</version>
- </dependency>
-
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache-core</artifactId>
- <version>2.5.2</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.9</version>
- </dependency>
-
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.7</version>
- <scope>provided</scope>
- </dependency>
-
- </dependencies>
-
- <!-- 配置架包从中央仓库下载 -->
- <repositories>
- <!-- 仓库地址 -->
- <repository>
- <id>my-nexus-repositories</id>
- <name>my-nexus-repository</name>
- <url>http://192.168.0.9:8081/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
-
- </repositories>
-
- <pluginRepositories>
- <!-- 插件地址 -->
- <pluginRepository>
- <id>my-nexus-pluginRepositories</id>
- <name>my-nexus-repository</name>
- <url>http://192.168.0.9:8081/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </pluginRepository>
-
- </pluginRepositories>
- <!-- 配置发布到中央仓库 -->
- <distributionManagement>
- <repository>
- <id>my-nexus-releases</id>
- <name>my-nexus-releases</name>
- <url>http://192.168.0.9:8081/nexus/content/repositories/my-nexus-releases/</url>
- </repository>
-
-
- <snapshotRepository>
- <id>my-nexus-snapshot</id>
- <name>my-nexus-snapshots</name>
- <url>http://192.168.0.9:8081/nexus/content/repositories/my-nexus-snapshots/</url>
- </snapshotRepository>
- </distributionManagement>
-
- <profiles>
- <profile>
- <id>local</id><!-- 本地开发环境 -->
- <properties>
- <package.environment>local</package.environment>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>internal</id><!-- 内测环境 -->
- <properties>
- <package.environment>internal</package.environment>
- </properties>
- </profile>
- <profile>
- <id>product</id><!-- 生产环境 -->
- <properties>
- <package.environment>product</package.environment>
- </properties>
- </profile>
- </profiles>
-
- <build>
- <finalName>${project.artifactId}</finalName>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- <excludes>
- <exclude>local/*</exclude>
- <exclude>internal/*</exclude>
- <exclude>product/*</exclude>
- <exclude>public/*</exclude>
- </excludes>
- </resource>
- </resources>
- <plugins>
- <!-- tomcat7插件 -->
- <plugin>
- <groupId>org.apache.tomcat.maven</groupId>
- <artifactId>tomcat7-maven-plugin</artifactId>
- <version>${tomcat.version}</version>
- <configuration>
- <!-- 自动发布到tomcat -->
- <url>http://localhost:80/manager/text</url>
- <update>true</update>
- <server>tomcat</server>
- <username>admin</username>
- <password>admin</password>
- <uriEncoding>utf-8</uriEncoding>
- <path>/${project.artifactId}</path>
- </configuration>
- </plugin>
-
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.1.1</version>
- <configuration>
- <archive>
- <addMavenDescriptor>false</addMavenDescriptor>
- </archive>
- <warName>${project.artifactId}</warName>
- <webResources>
- <!-- 不同的环境,使用不同的配置文件 -->
- <resource>
- <directory>src/main/resources/${package.environment}</directory>
- <targetPath>WEB-INF/classes</targetPath>
- <filtering>true</filtering>
- </resource>
- <!-- 公共的配置文件 -->
- <resource>
- <directory>src/main/resources/public</directory>
- <targetPath>WEB-INF/classes</targetPath>
- <filtering>true</filtering>
- </resource>
- </webResources>
- </configuration>
- </plugin>
- </plugins>
-
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>${java.version}</source>
- <target>${java.version}</target>
- <encoding>${java.encoding}</encoding>
- <compilerArguments>
- <verbose />
- <!-- <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> -->
- </compilerArguments>
- </configuration>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
- </project>

- <profiles>
- <profile>
- <id>local</id><!-- 本地开发环境 -->
- <properties>
- <package.environment>local</package.environment>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>internal</id><!-- 内测环境 -->
- <properties>
- <package.environment>internal</package.environment>
- </properties>
- </profile>
- <profile>
- <id>product</id><!-- 生产环境 -->
- <properties>
- <package.environment>product</package.environment>
- </properties>
- </profile>
- </profiles>

这里就是不同的resources文件夹, 在这里可以区分本地、内测、生产;
<activeByDefault>true</activeByDefault>
设置为true的就是默认被激活的. 所以后面打包默认就是本地;
- <!-- 不同的环境,使用不同的配置文件 -->
- <resource>
- <directory>src/main/resources/${package.environment}</directory>
- <targetPath>WEB-INF/classes</targetPath>
- <filtering>true</filtering>
- </resource>
<directory>src/main/resources/${package.environment}</directory>
这边指的是动态文件夹
最后别忘了
<filtering>true</filtering>
这个一定要设置成true
最后, 打包, 使用Maven的生命周期,比如,打包生产环境的配置文件,package 打包生成 war 文件。
mvn -Pproduct package;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。