导航

Maven 包含资源文件

Posted on 2017-10-11 14:17  耍流氓的兔兔  阅读(2794)  评论(0编辑  收藏  举报

 

Maven打包时,如何包含资源文件(src/main/java | src/main/resources):

  参考博客:http://blog.csdn.net/jsflzhong/article/details/52077075

   在Maven项目中,一般将用到的资源文件(xml | properties | xsd等)放在src/main/resources下面,利用Maven打包时,Maven可以将这些资源文件打包到相应的jar或者时war中

  1 可以在pom文件中进行配置:

    **/*这样的写法,是为了保证各级子目录下的资源文件都被打包

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

   2 可以使用build-helper-maven-plugin插件

    <build>  
        <plugins>  
            <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>  

  3 可以使用maven-resources-plugin插件

<build>  <plugins>
    <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>