Maven 进行多环境配置,使用profile文件进行配置

说明:Spring和Maven都可以进行profile配置多环境,这里只介绍Maven进行管理。我的开发环境:eclipse+maven

文件结构目录:

 

步骤:

一、在pom.xml文件中,添加如下:

<profiles>
<profile>
<id>develop</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
</profiles>

<build>
<filters>
<filter>conf/${env}/jdbc.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

简单说明:profiles里面配置了两个profile文件,即有两个环境dev和test环境,<env>标签是为了切换环境,激活需要的环境需要。<activeByDefault>设置为true表明,该profile是默认激活状态。

<filters>里的<filter>的值表示你所要激活的环境所需的配置文件,比如:dev环境下,我需要激活conf/dev/jdbc.properties;test环境下,我需要激活conf/test/jdbc.properties。

<resources>中的<resource>标签表示,我需要替换的目标文件(替换的是值,不是整个jdbc.properties替换)

src/main/resource中的jdbc.properties内容如下:(所有内容就是下面的,行数自动忽略,那是我的注释造成的,不用管)

二、在eclipse下设置maven的编译环境(即在eclipse如何激活我所需要的环境):

右击该maven项目  -> Properties -> Maven ,在右边的框中填入你想切换的环境。如下:

点击Apply 和OK即可。然后重启服务器即可。

如果你到此,成功切换,那么已经完成,下面不用看了。如果不成功,提示com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException   字节的 UTF-8序列的字节 无效

请按如下操作:

三、出现UTF-8字节无效的错误,首先请确定你所有的代码都是UTF-8编码,然后还是报该错误。那么就需要在pom.xml中添加如下:

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

添加地方如下图:

说明:<project.build.sourceEncoding>标签保证整个工程是UTF-8编码

到此所有工作完成。

NOTE:spring也可以配置profile,但是需要配合web.xml进行,不是很灵活。所以本文选择maven进行环境配置。

本文参考:http://www.petrikainulainen.net/programming/tips-and-tricks/creating-profile-specific-configuration-files-with-maven/

spring配置请参考:找不到了,自己google吧!

猜你喜欢

转载自blog.csdn.net/qq_39658059/article/details/78705213