Maven profile实现多环境配置
来源:互联网 发布:顽固软件卸载。 编辑:程序博客网 时间:2024/05/18 02:58
实际开发项目是需要配置多套环境配置的,如开发、测试、生产等。 在Maven中,有种多环境配置的方法,可以做到在开发、测试、运营的时候,使用各自对应的环境配置,可以大大的提高开发效率。
下面看下环境的配置文件:
用个例子说明下。为了对比方便,这里创建了一个聚合项目,然后其中一个Project根据构建参数不同,加载不同配置。另一个Project不加载配置。目录如下:
这里有三个project,各自的pom如下:
mavenaggregator的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>com</groupId><artifactId>mavenaggregator</artifactId><!-- require pom if aggregator --><packaging>pom</packaging><version>1.0.0.1</version><name>my Maven Webapp</name><url>http://maven.apache.org</url><modules><!-- relative paths to the directories of pom --> <module>my-project</module> <module>another-project</module></modules></project>my-project的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>com</groupId><artifactId>my-project</artifactId><packaging>jar</packaging><version>1.0.0.1</version><name>my-project</name><url>http://maven.apache.org</url><parent> <groupId>com</groupId> <artifactId>mavenaggregator</artifactId> <version>1.0.0.1</version> </parent></project>another-project的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>com</groupId><artifactId>another-project</artifactId><packaging>jar</packaging><version>1.0.0.1</version><name>another-project</name><url>http://maven.apache.org</url><parent> <groupId>com</groupId> <artifactId>mavenaggregator</artifactId> <version>1.0.0.1</version> </parent></project>
mavenaggregator的pom.xml中添加如下:
<!-- 其他代码 --> <profiles><profile> <!-- 本地开发打包 --> <id>development</id> <properties> <!-- 为该环境下的build指定参数赋值 --> <env>development</env> <deploy.url>http://host:port/manager/text</deploy.url> </properties> <activation> <!-- 默认激活的(activeByDefault为true) --> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>production</id> <properties> <!-- 为该环境下的build指定参数赋值 --> <env>production</env> <deploy.url>http://host:port/manager/text</deploy.url> </properties> <activation> <!-- 默认激活的(activeByDefault为true) --> <activeByDefault>true</activeByDefault> </activation> </profile></profiles><!-- 其他代码 -->通常为了开发方便,我们都是将本地开发激活 <activeByDefault>true</activeByDefault>,即当构建时没有参数时,默认使用本地开发配置。
下面看下环境的配置文件:
带有参数的 common的文件,这里是 config.properties
username=${db_username}password=${db_password}而各个环境的参数配置,都在各自文件夹下,比如这里的 resource/development/build.properties 和 resource/production/build.properties 。
db_username=localdb_password=password1
db_username=productiondb_password=password1因为我们这里只需要让这个子Project加载资源文件,因此只需要在my-project的pom.xml中添加如下配置:
<!-- 其他代码 --> <build> <plugins><plugin><!-- 处理资源文件。默认的主资源文件目录是src/main/resources,可以在此指定额外的资源文件目录 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.5</version><configuration><encoding>UTF-8</encoding></configuration></plugin></plugins><resources> <resource> <!-- 资源文件位置src/main/resources/,这下面的资源文件的${}会全部被替换成filter中的标签内容。 directory指定的value会作为classes的资源跟目录, 比如指定:src/main/resources/,则classes下会出现jdbc等包, 若指定:src/main/resources/jdbc/,则classes下直接出现jdbc包下的文件,不会额外出现jdbc等其他包结构。因为他把jdbc作为了根目录 --> <directory>src/main/resources</directory> <!-- 在某个resource中如果设置filtering为true,将会根据输入参数动态修改相关内容。 --> <filtering>true</filtering> <!-- 排除环境的配置资源根目录使用单独的资源目录来指定 --> <excludes> <exclude>production/*</exclude> <exclude>development/*</exclude> </excludes> </resource> <!-- <resource> --> <!-- 然后再加载使用单独的资源目录来指定 --> <!-- <directory>src/main/resources/${env}</directory> --> <!-- </resource> --> </resources> <filters> <filter>src/main/resources/${env}/build.properties</filter> </filters></build><!-- 其他代码 -->最后,回到项目根目录下,执行maven命令即可:
mvn clean package -Pproduction或
mvn clean package -Pdevelopment此时再回到my-project的target 目录就会发现 config.properties 文件里面的参数都已经被指定环境的配置替换好了。
username=localpassword=password1
阅读全文
0 0
- Maven profile实现多环境配置
- 使用maven profile实现多环境配置
- maven profile多环境配置
- Maven中实现多环境的配置profile和filter
- maven实现多环境大包的配置--profile
- 通过maven profile实现多套环境配置
- maven profile实现多环境打包
- maven profile实现多环境打包
- maven 使用maven profile实现多环境可移植构建
- maven profile实现多环境构建 (单项目多套配置)
- maven profile实现多环境构建 (单项目多套配置)
- Maven 进行多环境配置,使用profile文件进行配置
- Profile-多环境配置
- java学习笔记8 - maven profile实现多环境打包
- 使用maven profile实现多环境可移植构建
- 使用maven profile实现多环境可移植构建
- 使用maven profile实现多环境可移植构建
- java学习笔记8 - maven profile实现多环境打包
- post 与get 区别,为什么现在的HTTP通信中大多数请求还是使用get?
- Android开发之PendingIntent的使用
- 设计模式-原型模式
- Linux安装NodeJS
- 一张图说明各个开源协议的不同
- Maven profile实现多环境配置
- [干货]作为大数据入门者_你不得不知道的2017杭州云栖大会
- .NET与JAVA的区别【完整版】
- pip install opencv-python opencv-contrib-python 不可用时,可使用whl的方式安装
- Existing Linux Wireless drivers
- 鼠标悬停显示浮框
- linux环境c++程序调用shell返回值问题
- Android App 启动页(Splash)黑/白闪屏现象产生原因与解决办法
- 程序员练级攻略