在本文中,我们将向您展示一些Maven配置文件示例,这些示例可为不同的环境(开发,测试或生产)传递不同的参数(服务器或数据库参数)。

PS已通过Maven 3.5.3测试

1.基本的Maven配置文件

1.1跳过单元测试的简单配置文件。

pom.xml
<!-- skip unit test --><profile><id>xtest</id><properties><maven.test.skip>true</maven.test.skip></properties></profile>

1.2要激活配置文件,请添加-P选项。

Terminal
# Activate xtest profile to skip unit test and package the project$ mvn package -Pxtest

1.3要激活多个配置文件:

Terminal
$ mvn package -P xtest, another-profile-id# multi modules, same syntax
$ mvn -pl module-name package -P xtest, another-profile-id

1.4在编译或打包阶段始终添加maven-help-plugin以显示活动配置文件,它将节省大量调试时间。

pom.xml
<build><plugins><!-- display active profile in compile phase --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-help-plugin</artifactId><version>3.1.0</version><executions><execution><id>show-profiles</id><phase>compile</phase><goals><goal>active-profiles</goal></goals></execution></executions></plugin></plugins></build>

下次,当前活动配置文件将在编译阶段显示。

Terminal
$ mvn compile -P xtest[INFO] --- maven-help-plugin:3.1.0:active-profiles (show-profiles) @ example1 ---
[INFO]
Active Profiles for Project 'com.mkyong:example1:jar:1.0':The following profiles are active:- xtest (source: com.mkyong:example1:1.0)

2. Maven配置文件–示例1

Maven配置文件示例,可将不同的属性值传递给开发和生产环境。

2.1属性文件。

resources/db.properties
db.driverClassName=${db.driverClassName}
db.url=${db.url}
db.username=${db.username}
db.password=${db.password}

2.2启用过滤。 Maven将使用活动的Maven概要文件属性将${}映射到resources/db.properties

注意
阅读此Maven过滤

pom.xml
<!-- map ${} variable --><resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources>

2.3创建两个具有不同属性值的配置文件ID(dev和prod)。

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><parent><artifactId>maven-profiles</artifactId><groupId>com.mkyong</groupId><version>1.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>example1</artifactId><profiles><profile><id>dev</id><activation><!-- this profile is active by default --><activeByDefault>true</activeByDefault><!-- activate if system properties 'env=dev' --><property><name>env</name><value>dev</value></property></activation><properties><db.driverClassName>com.mysql.jdbc.Driver</db.driverClassName><db.url>jdbc:mysql://localhost:3306/dev</db.url><db.username>mkyong</db.username><db.password>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</db.password></properties></profile><profile><id>prod</id><activation><!-- activate if system properties 'env=prod' --><property><name>env</name><value>prod</value></property></activation><properties><db.driverClassName>com.mysql.jdbc.Driver</db.driverClassName><db.url>jdbc:mysql://live01:3306/prod</db.url><db.username>mkyong</db.username><db.password>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</db.password></properties></profile></profiles><build><!-- map ${} variable --><resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.2.0</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>com.mkyong.example1.App1</mainClass></transformer></transformers></configuration></execution></executions></plugin></plugins></build></project>

2.4加载属性文件并打印出来。

App1.java
package com.mkyong.example1;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class App1 {public static void main(String[] args) {App1 app = new App1();Properties prop = app.loadPropertiesFile("db.properties");prop.forEach((k, v) -> System.out.println(k + ":" + v));}public Properties loadPropertiesFile(String filePath) {Properties prop = new Properties();try (InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(filePath)) {prop.load(resourceAsStream);} catch (IOException e) {System.err.println("Unable to load properties file : " + filePath);}return prop;}
}

2.5测试。

Terminal
# default profile id is 'dev'
$ mvn package$ java -jar target/example1-1.0.jar
db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
db.driverClassName:com.mysql.jdbc.Driver
db.username:mkyong
db.url:jdbc:mysql://localhost:3306/dev# enable profile id 'prod' with -P prod or -D env=prod
$ mvn package -P prod
$ mvn package -D env=prod$ java -jar target/example1-1.0.jar
db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
db.driverClassName:com.mysql.jdbc.Driver
db.username:mkyong
db.url:jdbc:mysql://live01:3306/prod

3. Maven配置文件–示例2

这个Maven配置文件示例会将所有内容放入属性文件中。

3.1一个属性文件,稍后Maven将根据配置文件ID映射值。

resources/config.properties
# Database Config
db.driverClassName=${db.driverClassName}
db.url=${db.url}
db.username=${db.username}
db.password=${db.password}# Email Server
email.server=${email.server}# Log Files
log.file.location=${log.file.location}

3.2为开发,测试和生产环境创建不同的属性文件。

resources/env/config.dev.properties
# Database Config
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/dev
db.username=mkyong
db.password=8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92# Email Server
email.server=email-dev:8888# Log Files
log.file.location=dev/file.log
resources/env/config.test.properties
# Database Config
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://test01:3306/test
db.username=mkyong
db.password=8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92# Email Server
email.server=email-test:8888# Log Files
log.file.location=test/file.log
resources/env/config.prod.properties
# Database Config
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://live01:3306/prod
db.username=mkyong
db.password=8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92# Email Server
email.server=email-prod:25# Log Files
log.file.location=prod/file.log

3.3启用过滤。 这是关键!

注意
阅读此Maven过滤

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><parent><artifactId>maven-profiles</artifactId><groupId>com.mkyong</groupId><version>1.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>example2</artifactId><profiles><profile><id>dev</id><activation><activeByDefault>true</activeByDefault></activation><properties><env>dev</env></properties></profile><profile><id>prod</id><properties><env>prod</env></properties></profile><profile><id>test</id><properties><env>test</env></properties></profile></profiles><build><!-- Loading all ${} --><filters><filter>src/main/resources/env/config.${env}.properties</filter></filters><!-- Map ${} into resources --><resources><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>*.properties</include></includes></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.2.0</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>com.mkyong.example2.App2</mainClass></transformer></transformers></configuration></execution></executions></plugin></plugins></build></project>

3.4加载属性文件并打印出来。

App1.java
package com.mkyong.example2;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class App2 {public static void main(String[] args) {App2 app = new App2();Properties prop = app.loadPropertiesFile("config.properties");prop.forEach((k, v) -> System.out.println(k + ":" + v));}public Properties loadPropertiesFile(String filePath) {Properties prop = new Properties();try (InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(filePath)) {prop.load(resourceAsStream);} catch (IOException e) {System.err.println("Unable to load properties file : " + filePath);}return prop;}}

3.5测试。

Terminal
# profile id dev (default)
$ mvn package$ java -jar target/example2-1.0.jar
log.file.location:dev/file.log
db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
db.driverClassName:com.mysql.jdbc.Driver
db.username:mkyong
email.server:email-dev:8888
db.url:jdbc:mysql://localhost:3306/dev# profile id prod
$ mvn package -P prod$ java -jar target/example2-1.0.jar
log.file.location:prod/file.log
db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
db.driverClassName:com.mysql.jdbc.Driver
db.username:mkyong
email.server:email-prod:25
db.url:jdbc:mysql://live01:3306/prod# profile id test
$ mvn package -P test$ java -jar target/example2-1.0.jar
log.file.location:test/file.log
db.password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
db.driverClassName:com.mysql.jdbc.Driver
db.username:mkyong
email.server:email-test:8888
db.url:jdbc:mysql://test01:3306/test

最后,让我知道您的用例

Maven配置文件示例相关推荐

  1. Maven 配置文件 POM 的常用依赖配置代码

    Maven 配置文件 POM 的常用依赖配置代码 Lombok 测试 Junit 5 日志 Log4j2 & SLF4J & Lombok Spring Bean Spring Boo ...

  2. log4j 配置文件示例_Log4j2示例教程–配置,级别,附加程序

    log4j 配置文件示例 Welcome to the Apache Log4j2 Example Tutorial. If you ask an expert developer about the ...

  3. springboot maven配置文件外置

    springboot maven配置文件外置 ******************* 配置文件默认加载方式 springboot 应用启动时,会自动从以下位置加载配置文件 类路径:类路径根目录.类路径 ...

  4. redis 配置文件示例

    # redis 配置文件示例   # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1 ...

  5. linux c语言 readline,Linux C代码实现读取配置文件示例

    /** 读配置文件示例 注:配置文件必须为unix格式,即\n结尾 */ #include #include #include #include #include // 结构体 struct host ...

  6. Maven 配置文件 POM 的常用插件配置代码

    Maven 配置文件 POM 的常用插件配置代码 普通 将 Maven 多模块依赖集成打进一个 JAR 包(方法 1) 将 Maven 多模块依赖集成打进一个 JAR 包(方法 2) 生成单入口类 J ...

  7. Nginx配置文件示例

    Nginx配置文件示例 nginx.conf load_module modules/ngx_http_image_filter_module.so;user root; worker_process ...

  8. Nginx反向代理与负载均衡等配置文件示例

    Nginx反向代理于负载均衡等配置文件示例 Nginx.conf配置文件 worker_processes 8;events {worker_connections 1024; }http {incl ...

  9. Linux C代码实现读取配置文件示例

    最近在看hostapd,该程序使用了conf配置文件,本文参考hostapd代码抽取出读取配置文件的示例,由于配置选项和业务密切相关,实际使用中仍需要做修改. 下面是代码示例: /** 读配置文件示例 ...

  10. Maven配置文件无法被导出或者生效的问题【已解决】

    Maven配置文件无法被导出或者生效的问题 解决方案: <!--在build中配置resources,来防止我们资源导出失败的问题--><build><resources ...

最新文章

  1. 如何在Rancher 2.2 Preview2上部署和管理多K8s集群应用
  2. python解非线性规划问题讲析_python中线性规划中的单纯形法、scipy库与非线性规划求解问题...
  3. [NCTF2019]Reverse
  4. CommandBehavior.CloseConnection的使用
  5. 手机定位和什么有关?关机后的手机还能被定位吗?
  6. 华为的鸿蒙系统是海思_死心了!华为鸿蒙系统首款终端确认,不是手机
  7. excel和mysql php_php将mysql数据库和Excel相互导入和导出的方法
  8. 算法导论第三版第六章 答案
  9. 数字水印--给我的文件充当保护神
  10. 中国移动MM的免流量费策略太不靠谱
  11. navicat建mysql数据库密码_Navicat修改MySQL数据库密码的多种方法
  12. opencv android模版匹配,基于opencv模板匹配的目标检测方法
  13. 大数据行业包含的岗位有哪些
  14. 上传照片(身份证照片正反面)
  15. 记录学习历程-----游戏编程
  16. 一文快速了解MaxCompute
  17. 大功率LED的热功率计算_51CAE_新浪博客
  18. 【VUE2开发20221004】-day1.1
  19. 杭州拒绝车辆“带病”上路 OBD在线接入实现排放动态监管
  20. Unity 滚球游戏

热门文章

  1. 小韦系统装工行网银U盾驱动的方法
  2. Rxjava:interval的使用
  3. AI巨头宝座易主,百度击败谷歌亚马逊,势不可挡!
  4. [论文笔记]Rob-GAN: Generator, Discriminator, and Adversarial Attacker
  5. 微信开通检测 检测号码是否开通微信
  6. 区块链学习——HyperLedger-Fabric v0.6环境搭建详细过程
  7. PIXEL-LEVEL SELF-PACED LEARNING FOR SUPER-RESOLUTION
  8. 开氏温度与摄氏度换算_为什么体温表要甩?探秘温度计、湿度计的玄机!
  9. 计算机芯片维修论文,中职院校计算机硬件芯片检测及维修的论文
  10. Codevs3315时空跳跃者的魔法