maven profile实现多环境打包

快速解决:

项目目录

1.pom文件中添加profile

<profiles>    <profile>        <!-- 本地开发环境 -->        <id>dev</id>        <properties>            <profiles.active>dev</profiles.active>        </properties>        <activation>            <activeByDefault>true</activeByDefault>        </activation>    </profile>    <profile>        <!-- 测试环境 -->        <id>test</id>        <properties>            <profiles.active>test</profiles.active>        </properties>

    </profile>    <profile>        <!-- 生产环境 -->        <id>prod</id>        <properties>            <profiles.active>prod</profiles.active>        </properties>    </profile></profiles> 

2. pom中指定 filter文件夹 和 maven-war-plugin指定替换文件夹

 <build>       <resources>           <resource>               <directory>src/main/resources</directory>               <includes>                   <include>spring-content.xml</include>               </includes>               <filtering>true</filtering>           </resource>       </resources>       <plugins>           <plugin>               <groupId>org.apache.maven.plugins</groupId>               <artifactId>maven-war-plugin</artifactId>               <version>2.4</version>               <configuration>                   <archiveClasses>true</archiveClasses>                   <warName>${project.artifactId}</warName>                   <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>                   <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>                   <webappDirectory>${project.build.directory}/${project.artifactId}                   </webappDirectory>                   <webResources>                       <resource>         <!-- 由于我是把配置文件都在/WEB-INF/config/文件夹-->         <!-- 所以把src/main/resources 被filter替换的文件替换dao WEB-INF/config/下-->                           <directory>src/main/resources</directory>                           <targetPath>WEB-INF/config</targetPath>                           <filtering>true</filtering>                       </resource>                   </webResources>               </configuration>           </plugin>           <plugin>               <groupId>org.eclipse.jetty</groupId>               <artifactId>jetty-maven-plugin</artifactId>               <version>9.3.0.M2</version>               <configuration>                   <scanIntervalSeconds>6</scanIntervalSeconds>                   <httpConnector>                       <port>5004</port>                   </httpConnector>                   <webAppConfig>                       <contextPath>/xxxx</contextPath>                       <!--<defaultsDescriptor>${basedir}/src/main/resources/webdefault.xml</defaultsDescriptor>-->                   </webAppConfig>               </configuration>           </plugin>       </plugins></build>

3. 对比 WEB-INF/config下aplicationContent.xml(将被后面替换)和 src/main/resources 下aplicationContent.xml

<bean id="configProperties"   class="org.springframework.beans.factory.config.PropertiesFactoryBean">   <property name="locations">      <list>         <value>/WEB-INF/config/application_dev.properties</value>      </list>   </property></bean>

VS

<bean id="configProperties"   class="org.springframework.beans.factory.config.PropertiesFactoryBean">   <property name="locations">      <list>         <!--${profiles.active} 此处占位符 会被mvn替换从pom中profile.active的环境变量         第三步中 maven-war-plugin配置替换文件  完成多环境切换-->         <value>/WEB-INF/config/application_${profiles.active}.properties</value>      </list>   </property></bean>

4.maven 编译打包  mvn  clean package  -Dmaven.test.skip=true -Ptest

指定-Dmaven.test.skip=true表示跳过测试   -Ptest 激活Profile id=test的环境参数

实现效果

config下 原本application_dev.properties 编译完成 替换为application_test.properties

从而实现了加载多环境配置.

概念简介

构建项目时可能会遇到在测试(如单元测试)、开发、模拟、生产等不同环境下需要不同配置.
如果需要修改的项目很多而且复杂的话,则应该使用 Apache Maven 的 Profile 和 Filtering 功能来解决。

Filtering 功能

Filtering 是 Maven Resources Plugin 的一个功能,它会使用系统属性或者项目属性的值替换资源文件(*.properties,*.xml)当中 ${…} 符号的值。比如你系统属性有一项 “user.name=foobar”,那么资源文件当中的 ${user.name} 符号会在 Maven 编译时自动被替换为 “foobar”。

Profile 功能

Profile 的作用是允许你在项目文件(pom.xml)里定义若干个 profile 段,然后在编译时选择其中的一个用于覆盖项目文件原先的定义。接着上一个例子,如果我们需要为开发环境和生产环境定义不同的 user.name 属性值,则我们在项目目录里创建两个属性文件:

profile-development.properties,内容

user.name=foobar

profile-production.properties,内容

user.name=tom

然后在项目文件(pom.xml)里增加 profile 段,如下:

<build>   <filters>      <filter>src/main/filters/filter-${env}.properties</filter>   </filters>   <resources>      <resource>         <directory>src/main/resources</directory>         <filtering>true</filtering>      </resource>   </resources></build><profiles>   <profile>      <id>develop</id>      <properties>         <env>develop</env>      </properties>   </profile>   <profile>      <id>test</id>      <properties>         <env>test</env>      </properties>   </profile>   <profile>      <id>product</id>      <properties>         <env>product</env>      </properties>   </profile></profiles>

pasting

在编译项目时,可以使用 -P 参

数指定需要使用的 profile 的 id,比如下面命令将会使用 development profile:

$mvn clean compile -Pdevelopment

如果想使用 production profile 则执行如下命令:

$mvn clean compile -Pproduction

假如不指定 -P 参数的话,则会使用 activeByDefault=true 的一项(即 development)。

至此,通过 filtering 和 profile 功能实现了为开发环境和生产环境使用不同配置值的目的。当然 profile 还可以允许你添加更多的定义,比如为某一个 profile 添加不同的资源文件。在一些大中型项目里,不同的环境可能仅仅修改配置值并不足够,可能还需要某个配置文件整个替换,那么就应该在 profiles/profile/build/resources 段里指定了。详细的可以参阅附录链接。

http://archboy.org/2012/05/21/apache-maven-profile-filtering-multiple-build-environments/

http://www.kafeitu.me/solution/2013/06/29/a-successful-cofiguration-file-management-solution.html

时间: 2025-01-10 01:25:52

maven profile实现多环境打包的相关文章

【转】maven profile实现多环境打包

作为一名程序员,在开发的过程中,经常需要面对不同的运行环境(开发环境.测试环境.生产环境.内网环境.外网环境等等),在不同的环境中,相关的配置一般不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置.每次在不同环境部署程序时,都需要修改相应的配置文件,使之完成环境的配置.这么做存在一个比较大的问题:每次修改配置非常麻烦,而且配置错误会产生不可预估的影响,比如,在发布生产环境时用的开发环境的配置还好,但如果在开发环境下用生产环境的数据,将会造成生产数据的污染,导致生产环境崩溃.

maven profile实现多环境配置

每次项目部署上线都需要手动去修改配置文件(比如数据库配置,或者一个自定义的配置)然后才能打包,很麻烦,网上找到 maven profile可以完成这个工作,记录如下: 环境:eclipse + spring mvc + maven 1.直接看图,把数据库的配置单独拿出来放在了resources_env目录下,三个不同环境参数不同, 2,在pom文件中添加配置    这个引用自:http://www.cnblogs.com/raphael5200/p/6677549.html,感谢 <profil

Maven根据不同的环境打包不同的配置

前言: 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置等等. 那么就需要借助maven提供的profile功能,通过不同的环境激活不同的profile来实现"maven根据不同的运行环境,打包不同的配置文件"的目的. 一.原理 利用filter实现对资源文件(resouces)过滤 maven filter可利用指定的xxx.properties中对应的key=value对资

使用maven profile实现多环境可移植构建

在开发过程中,我们的软件会面对不同的执行环境.比方开发环境.測试环境.生产环境,而我们的软件在不同的环境中.有的配置可能会不一样.比方数据源配置.日志文件配置.以及一些软件执行过程中的基本配置,那每次我们将软件部署到不同的环境时,都须要改动对应的配置文件.这样来回改动,是个很麻烦的事情.有没有一种方法可以让我们不用改动配置就能公布到不同的环境中呢?当然有.这就是接下来要做的事. 当然,这里的前提是使用maven做为构建工具. 使用maven来实现多环境的构建可移植性.须要借助maven提供的pr

使用maven profile实现多环境配置相关打包

项目开发需要有多个环境,一般为开发,测试,预发,正式4个环境,通过maven可以实现按不同环境进行打包部署,命令为: mvn package -P dev 在eclipse中可以右击选项run configuration,输入上述命令. PS:eclipse maven install和maven packege的区别在于前者除了打包到target外,还会install到本地仓库,这样其他引用的工程就可直接使用. 其中"dev"为环境的变量id, 可以自己定义, 我定义的名称为:dev

Maven 为不同的环境打包 -开发,测试,生产

每个项目都会有多套运行环境,最基本的就是Dev.QA.Prod,不同的环境对应的配置也不尽相同,比如说数据库连接,文件路径,WebServices address 等等. 不同环境下需要构建不同的包,那么我们可以通过以下两种方式来解决: 一.定义多个 pom,使用 -f <pom_file> 指定 e.g. pom.xml (default) pom_qa.xml pom_prod.xmlBuild war for qa: mvn clean package -f pom_qa.xmlBuil

使用maven profile 构建不同环境引用不同的值

需要做的配置如下 <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <filters> <filter>vars/vars.dev.properties</filter> </filters&

spring-boot分环境打包为tar包

1.pom配置 <!-- 多环境打包 start --> <profiles> <!-- 开发环境配置 --> <profile> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> <activation> <activeByDefault>true<

spring-boot分环境打包为war包

1.启动类修改 @EnableSwagger2 @SpringBootApplication public class CustWebAcApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(CustWebAcApplication.class, args); } @Override protected SpringAppl