maven build resources 的功能

本文介绍了如何使用Maven将特定目录下的资源文件打包进最终的软件包中,特别是针对Mybatis的mapper.xml文件。文中提供了两种方法:一是利用build-helper-maven-plugin插件;二是使用maven-resources-plugin插件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

build resoures的功能

maven 在打包时,默认资源文件都是在 src/main/resources 下面,因此只会打包该目录下的资源文件。
如果需要打包其他目录的资源文件,就需要通过resources指定目录。

这也就体现了 maven 默认大于配置的思想

打包其他目录资源文件的发方法

  1. 利用build-helper-maven-plugin插件
    <build>  
    ...  
    </plugins>  
        ...  
        <!--  
        利用此plugin,把源代码中的xml文件,  
        打包到相应位置,这里主要是为了打包Mybatis的mapper.xml文件   
        -->  
        <plugin>  
            <groupId>org.codehaus.mojo</groupId>  
            <artifactId>build-helper-maven-plugin</artifactId>  
            <version>1.8</version>  
            <executions>  
                <execution>  
                    <id>add-resource</id>  
                    <phase>generate-resources</phase>  
                    <goals>  
                        <goal>add-resource</goal>  
                    </goals>  
                    <configuration>  
                        <resources>  
                            <resource>  
                                <directory>src/main/java</directory>  
                                <includes>  
                                    <include>**/*.xml</include>  
                                </includes>  
                            </resource>  
                        </resources>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>     
        ...  
    </plugins>       
    ...  
</build>  
  1. 利用maven-resources-plugin插件
<build>  
    ...  
    </plugins>  
        ...  
        <!--  
        利用此plugin,把源代码中的xml文件,打包到相应位置,  
        这里主要是为了打包Mybatis的mapper.xml文件   
        -->  
        <plugin>  
            <artifactId>maven-resources-plugin</artifactId>  
            <version>2.5</version>  
            <executions>  
                <execution>  
                    <id>copy-xmls</id>  
                    <phase>process-sources</phase>  
                    <goals>  
                        <goal>copy-resources</goal>  
                    </goals>  
                    <configuration>  
                        <outputDirectory>${basedir}/target/classes</outputDirectory>  
                        <resources>  
                            <resource>  
                                <directory>${basedir}/src/main/java</directory>  
                                <includes>  
                                    <include>**/*.xml</include>  
                                </includes>  
                            </resource>  
                        </resources>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>     
        ...  
    </plugins>       
    ...  
</build>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值