好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Maven项目中resources配置总结

背景

通常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

# Maven项目的标准目录结构

src

  main

  java   #源文件

  resources #资源文件

  filters #资源过滤文件

  config #配置文件

  scripts #脚本文件

  webapp #web应用文件

  test

  java #测试源文件

  resources #测试资源文件

  filters #测试资源过滤文件

  it  #集成测试

  assembly #assembly descriptors

  site #Site

target

  generated-sources

  classes

  generated-test-sources

  test-classes

  xxx.jar

pom.xml

LICENSE.txt

NOTICE.txt

README.txt

其中 src/main/resources 和 src/test/resources 是资源文件目录。本文将详细介绍资源文件相关的配置。

第一部分 基本配置介绍

我们在使用Maven组件来构建项目的时候,通常将配置文件放在资源文件目录下。针对这个目录,在 pom.xml 文件进行了定义,我们首先看一个案例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<build>

<resources>

  <resource>

  <directory>src/main/resources</directory>

  <filtering> true </filtering>

  <includes>

   <include>application.properties</include>

  </includes>

  </resource>

  <resource>

   <directory>src/main/resources</directory>

   <filtering> false </filtering>

   <excludes>

    <exclude>application.properties</exclude>

   </excludes>

  </resource>

</resources>

</build>

标签 <directory> 指定资源文件目录; 标签 <include> 指定资源文件目录中,哪些文件被打包。 标签 <excludes> 指定资源文件目录中,哪些文件不被打包。

特别的,标签 <filtering> 是一个 bool 值,默认值为 false 。在maven资源文件中,支持使用变量 placeholder ,例如资源文件:

?

1

2

3

# application.properties

application.user=${username}

application.password=${password}

文件中使用 ${keyword} 占位符来标识变量。这时候可以在 pom.xml 文件中定义变量的取值:

?

1

2

3

4

<properties>

  <username>mysql</username>

  <password>password123</password>

</properties>

如果需要对配置文件中变量进行替换实际值,就需要开启 <filtering> ,该值设置为 true 。

第二部分 具体配置和注意事项

2.1 案例说明

根据上面的介绍,最开始例子中有两段 resource 的配置描述,分别的含义为:

第一个配置的含义是:在配置文件目录 src/main/resources 过滤掉其他文件,只保留 application.properties 文件。并且开启 filtering 变量替换属性。 第二个配置的含义是:在配置文件目录 src/main/resources 过滤掉 application.properties 文件,其他文件均保留。并且关闭 filtering 变量替换属性。

需要特别注意的是,这里两个 <resources> 都是对资源目录 <src/main/resources> 的配置定义,一个是保留 application.properties ,一个是去除 application.properties 。这样两个配置会不会冲突?实际上两个配置是兼容。最后是取两个配置分别过滤的文件集合的并集。

可以看一下例子,资源目录 src/main/resources 里面有三个文件:

?

1

2

3

application.yml

application.properties

application.xml

编译后, target/classes 路径中三个配置文件都是有的。第一配置文件过滤后文件集合为 {application.properties} ,第二个配置过滤后的集合为 {application.yml,application.xml} ,最后取并集就得到了最后编译结果。

2.2 正则过滤

在对资源目录中文件进行过滤时,还支持正则表达式。例如:

?

1

<include>**/*.xml</include>

这个表达式表示包含了资源目录下面所有 xml 文件(以及子目录下面)。

2.3 变量占位符

这里主要指的是 <filtering> 的功能。例如下面的 xml 文件定义了一个研发 <profile> 。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

<profiles>

   <profile>

    <id>dev</id>

    <properties>

     <resource.delimiter>${}</resource.delimiter>

     <username>mysql</username>

     <password>password123</password>

    </properties>

    <activation>

     <activeByDefault> true </activeByDefault>

    </activation>

   </profile>

  </profiles>

配置中定义的 username 和 password 两个变量的值。使用 package -P dev 编译后,配置文件中占位符变量被替换:

?

1

2

application.user=mysql

application.password=password123

需要注意的是这里增加了 <resource.delimiter> 标签配置,定义了占位符的格式。有些时候其他依赖包的 pom 文件也会指定占位符的格式,就会造成格式不统一。例如:spring boot把默认的占位符号${}改成了@var@。所以建议进行配置,否则容易环境"污染"。

2.4 关于一个错误观点的说明

有很多关于这个主题的文章(例如 CSND )中,认为同一个 <resource> 中,若是 <include> 和 <exclude> 都存在的话,那就发生冲突了,这时会以 <exclude> 为准。

关于这个论点,笔者实际做了实验,同一个 <resource> 中,同时配置了 <include> 和 <exclude> 。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<build>

<resources>

  <resource>

  <directory>src/main/resources</directory>

  <filtering> true </filtering>

  <includes>

   <include>application.properties</include>

  </includes>

  <excludes>

   <exclude>application.properties</exclude>

  </excludes>

  </resource>

</resources>

</build>

编译结果,配置文件没有打包进入 target/classes 。说明这个论点是有问题的。说明在同一个 resource 中两种配置是取交集的。

2.5 子目录

资源目录也是支持子目录的。即可以在资源目录下面创建子目录,在打包过程中会保留子目录结构。例如:

?

1

2

3

4

5

6

resources

  -test

  --app.xml

  -application.yml

  -application.properties

  -application.xml

在项目编译后,如果子目录中资源文件被保留,那么子目录的结构也是保留的。例如:

?

1

2

3

4

5

6

7

target

  -classes

  --test

  ---app.xml

  -application.yml

  -application.properties

  -application.xml

第二部分 读取resources资源

例如我们的配置文件 properties 类型的配置文件,可以使用下面的语句进行读取:

方法1,从编译后的整个classes目录下去找;

?

1

InputStream is = this .getClass().getResourceAsStream( "/" +application.properties);

方法2, ClassLoader 从整个classes目录找;

?

1

InputStream is = this .getClass().getClassLoader().getResourceAsStream(application.properties);

读取使用 Java 的工具包 java.util.Properties :

?

1

2

3

4

5

6

7

8

import java.util.Properties;

 

Properties properties = new Properties();

InputStream is = this .getClass().getClassLoader().getResourceAsStream(application.properties);

properties.load(is)

 

//获取配置文件中name的配置值

System.out.println(properties.get(getProperty( "name" )))

其他类型的配置文件读取读者可以执行查找资料。

参考文献及资料

1、Maven Resources Plugin,链接:https://maven.apache.org/components/plugins-archives/maven-resources-plugin-2.6/

2、Maven资源过滤的配置,链接:http://c.biancheng.net/view/5285.html

到此这篇关于Maven项目中resources配置总结的文章就介绍到这了,更多相关Maven resources配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://www.cnblogs.com/rongxiang1986/archive/2021/03/30/14599064.html

查看更多关于Maven项目中resources配置总结的详细内容...

  阅读:34次