maven pom下filter和resources

在maven中,可以利用filter实现对资源文件(resouces)过滤

maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的${key}进行替换,最终把你的资源文件中的username=${key}替换成username=value【需要配合resources一起使用】

filter是在maven的compile阶段执行过虑替换的,所以只要触发了编译动作即可【笔者使用idea,发现重新编译或者启动调试时,会在target下生成 工程名.war 的文件夹,应该是集成了maven的缘故,正常idea是不会懂得替换的】

工程目录:
在根目录下 新增 filter.properties
在pom.xml配置
<project>
...

<build>
<filters>
<filter>filter.properties</filter>
</filters>
<resources>
<!-- 先指定src/main/resources下所有文件和文件夹为资源文件 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<!-- 设置对*.properties,logback.xml进行过虑,即这些文件中的${key}会被替换 -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.properties</include>
<include>logback.xml</include>
</includes>
</resource>
</resources>
...
</build>

</project>
### 如何使用 Maven 将依赖模块的资源文件打包到项目中 在 Maven 中,默认情况下,只有当前项目的 `src/main/resources` `src/test/resources` 被包含在最终构建的包中。如果希望将其他依赖模块中的资源文件也一并打包到目标项目中,则可以通过配置插件实现这一需求。 #### 使用 `maven-resources-plugin` 通过配置 `maven-resources-plugin` 插件,可以显式地复制依赖模块中的资源文件到目标目录下。以下是具体方法: 1. **定义依赖关系** 首先,在父级 POM 或者子模块中明确定义好各个模块之间的依赖关系[^3]。 2. **配置插件** 在需要打包资源的目标模块中添加如下配置: ```xml <build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-resources</id> <phase>generate-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/classes</outputDirectory> <resources> <resource> <directory>../dependent-module/src/main/resources</directory> <includes> <include>*.properties</include> <include>*.xml</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 上述配置会从名为 `dependent-module` 的模块中提取指定类型的资源文件(如 `.properties`, `.xml`),并将它们复制到当前模块的类路径下[^1]。 3. **运行命令** 执行以下命令触发资源复制过程: ```bash mvn process-resources ``` #### 使用 `shade` 插件 另一种方式是利用 `maven-shade-plugin` 来重新打包整个应用程序及其依赖项,包括资源文件。这种方式适用于更复杂的场景,比如需要将多个模块的资源合并成单一 JAR 文件的情况。 示例配置如下: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <!-- Include specific resource files --> <artifact>*:*</artifact> <includes> <include>META-INF/*.sf</include> <include>META-INF/*.DSA</include> <include>META-INF/*.RSA</include> </includes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 此配置允许将所有依赖模块中的特定资源文件纳入最终生成的应用程序 JAR 包中[^1]。 --- ### 注意事项 - 如果依赖模块未被正确解析,请确认其坐标已在本地仓库或远程仓库可用,并且网络连接正常。 - 对于大型项目,建议合理规划模块间的职责划分,避免不必要的重复引入资源文件造成冲突或膨胀问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值