Maven根据不同个环境打包, 获取不同的配置文件等等
http://www.cnblogs.com/tartis/p/5391079.html
- <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.</modelVersion>
- <groupId>book</groupId>
- <artifactId>book</artifactId>
- <packaging>war</packaging>
- <version>1.0</version>
- <name>book</name>
- <repositories>
- <repository>
- <id>eap</id>
- <url>http://maven.repository.redhat.com/techpreview/all</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>eap</id>
- <url>http://maven.repository.redhat.com/techpreview/all</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </pluginRepository>
- </pluginRepositories>
- <properties>
- <project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
- <maven.compiler.source>1.6</maven.compiler.source>
- <maven.compiler.target>1.6</maven.compiler.target>
- <package.environment>local</package.environment>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.postgresql</groupId>
- <artifactId>postgresql</artifactId>
- <version>9.2--jdbc4</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>3.2..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>3.2..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>3.2..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>3.2..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>3.2..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>3.2..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>3.2..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>3.2..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.2</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.</version>
- </dependency>
- </dependencies>
- <profiles>
- <profile>
- <id>local</id>
- <properties>
- <package.environment>local</package.environment>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>product</id>
- <properties>
- <package.environment>product</package.environment>
- </properties>
- </profile>
- </profiles>
- <build>
- <finalName>book</finalName>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- <excludes>
- <exclude>local/*</exclude>
- <exclude>product/*</exclude>
- <exclude>public/*</exclude>
- </excludes>
- </resource>
- </resources>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.1.1</version>
- <configuration>
- <archive>
- <addMavenDescriptor>false</addMavenDescriptor>
- </archive>
- <warName>book</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>
- <!--<resource>-->
- <!--<directory>${project.build.directory}/classes</directory>-->
- <!--<includes>-->
- <!--<include>**/*.properties</include>-->
- <!--<include>**/*.xml</include>-->
- <!--</includes>-->
- <!--</resource>-->
- </webResources>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
关键代码:
- <profiles>
- <profile>
- <id>local</id>
- <properties>
- <package.environment>local</package.environment>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>product</id>
- <properties>
- <package.environment>product</package.environment>
- </properties>
- </profile>
- </profiles>
这里就是不同的resources文件夹, 我这里只区分本地 产品; 设置为true的就是默认被激活的. 所以后面打包默认就是本地;
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- <excludes>
- <exclude>local/*</exclude>
- <exclude>product/*</exclude>
- <exclude>public/*</exclude>
- </excludes>
- </resource>
- </resources>
这里就是我的资源文件, public里面存放我的公用的, 比如Spring的配置文件, 就是本地与产品都一样的;local 与 product一看就知道了;
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.1.1</version>
- <configuration>
- <archive>
- <addMavenDescriptor>false</addMavenDescriptor>
- </archive>
- <warName>book</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>
- <!--<resource>-->
- <!--<directory>${project.build.directory}/classes</directory>-->
- <!--<includes>-->
- <!--<include>**/*.properties</include>-->
- <!--<include>**/*.xml</include>-->
- <!--</includes>-->
- <!--</resource>-->
- </webResources>
- </configuration>
- </plugin>
这里就是war的插件了,
- ${package.environment}
就是动态指定文件夹了.
- <filtering>true</filtering>
这里一定需要设置为true才行.
然后就差不多了. 最后执行 mvn clean ; mvn compile; mvn package; 这里是Maven的生命周期, 其他介绍的文章太多了, 我就不再具体讲. 如果说我要打product的war包;
mvn clean ; mvn compile; mvn -Pproduct package;
那就激活了product 的资源文件;
就这样子, 很简单吧~
Maven根据不同个环境打包, 获取不同的配置文件等等的更多相关文章
- maven 使用-P指定环境打包,linux移动配置文件失败,windows成功!
问题描述: windows机器使用-P指定环境打包,最后组装文件组装成功,配置文件成功移动,linux下却只移动了jar包. windows: linux: ...
- Maven_根据不同个环境打包, 获取不同的配置文件等等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 用maven按环境打包SpringBoot的不同配置文件
利用maven按环境打包SpringBoot的不同配置文件 application-dev.properties对应开发环境 application-test.properties对应测试环境 app ...
- 【转】maven profile实现多环境打包
作为一名程序员,在开发的过程中,经常需要面对不同的运行环境(开发环境.测试环境.生产环境.内网环境.外网环境等等),在不同的环境中,相关的配置一般不一样,比如数据源配置.日志文件配置.以及一些软件运行 ...
- maven profile实现多环境打包
快速解决: 项目目录 1.pom文件中添加profile <profiles> <profile> <!-- 本地开发环境 --> <id>dev< ...
- Maven根据不同的环境打包不同的配置
前言: 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置等等. 那么就需要借助maven提 ...
- maven为不同环境打包(hibernate)-超越昨天的自己系列(6)
超越昨天的自己系列(6) 使用ibatis开发中,耗在dao层的开发时间,调试时间,差错时间,以及适应修改需求的时间太长,导致项目看起来就添删改查,却特别费力. 在项目性能要求不高的情况下,开始寻 ...
- Maven插件之portable-config-maven-plugin(不同环境打包)
在大型的项目组中,分不同的开发环境,测试环境,生产环境(说白了就是配置文件不同,或者数据源,或者服务器,或者数据库等);问题来了,如何使用Maven针对不同的环境来打包呢? Maven提供了Profi ...
- Maven适配多种运行环境的打包方案
项目从开发到部署会历经多个运行环境,如开发环境.测试环境和生产环境,不同环境中项目的配置文件通常也会不同,典型的如数据库连接配置.我们当然不希望每次部署打包前都去修改配置文件以适配环境,利用Maven ...
随机推荐
- 【转】关于 Web GIS
以下部分选自2015-03-01出版的<Web GIS从基础到开发实践(基于ArcGIS API for JavaScript)>一书中的前言部分: Web GIS 概念于1994 年首次 ...
- C#窗体 LISTVIEW
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 初学者:JSP登陆界面
学生登陆查询系统 1 程序的主要功能及特点 实现一个登录界面的基本功能,具体要求: 登录界面login.jsp含有表单,用户能够输入用户名和密码,并提交表单给verify.jsp. Verify.js ...
- 利用OVER开窗函数分页
在SQL Server中,利用SQL进行分页的方法也有很多,今天要总结的是SQL Server 2005中引入的OVER开窗口函数,然后利用开窗函数进行分页. 示例代码如下: -- 设置数据库上下文 ...
- Oracle错误:动态执行表不可访问,本会话自动统计被禁止,关闭自动统计之后的问题
使用PL/SQL时, 每次第一次打开表的时候会提示"动态执行表不可访问,本会话的自动统计被禁止"的错误,一消息如下: V$SESSION,V$SESSTAT,V$STATNAME没 ...
- poj 2777
题意:两个操作:c l r x l到r之间的颜色变成x q l r 询问l到r有多少种颜色 思路:记一个整数表示哪种颜色是否取了 这里真的是煞笔了,看到这一题第一直觉是异或,但是A^A= ...
- scp不可用:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! After doing ssh are you seeing this error.No proble ...
- js里function的apply vs. bind vs. call
js里除了直接调用obj.func()之外,还提供了另外3种调用方式:apply.bind.call,都在function的原型里.这3种方法的异同在stackoverflow的这个答案里说的最清楚, ...
- sql中replace函数与like结合达到提换的效果
create table ts ( idd varchar() , co1 varchar() , co2 varchar() , co3 varchar() , gai varchar() ); i ...
- Jmeter—5 关联 响应数据传递-正则表达式提取器
在测试过程中,遇到一个问题:用户登录成功后服务器会返回一个登录凭证,之后所有的操作都需要带上此凭证.我们怎么获取登录凭证并传递给后续的操作? Jmeter提供了正则表达式提取器,用变量提取参数,后续通 ...