关于使用maven打包如何聚合资源文件

多数情况下,我们使用maven管理多个子工程,在最后maven打包阶段将多个子工程聚合到一个jar或war包。单个子工程会有自己独立的资源配置文件,在打包的时候我们需要将其聚合在一起(各子工程中的配置文件名称不能重复)。下面提供两种方法:

1.使用maven-resources-plugin插件,下面提供代码示例:

<build>
        <finalName>${projectName}</finalName>
        <plugins>
        <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes/</outputDirectory>
                            <resources>
                                <!-- 将app.framecore中的配置文件复制到该宿主工程中 -->
                                <resource>
                                    <directory>${basedir}/../../app.framecore/src/main/resources</directory>
                                    <includes>
                                        <include>**/*.sql</include>
                                        <include>**/*.xml</include>
                                        <include>**/*.properties</include>
                                    </includes>
                                </resource>
                            <!-- 将app.pms.cust.api中的配置文件复制到该宿主工程中 -->
                                <resource>
                                    <directory>${basedir}/../app.pms.cust.api/src/main/resources</directory>
                                    <includes>
                                        <include>**/*.sql</include>
                                        <include>**/*.xml</include>
                                        <include>**/*.properties</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <compilerArgument>-parameters</compilerArgument>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- true:跳过测试 -->
                    <skip>true</skip>
                    <encoding>${maven.compiler.encoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.直接在build节点中配置resources节点。如下代码所示:

<build>
        <resources>
            <resource>
                <targetPath>${basedir}/target/classes/</targetPath>
                <directory>${basedir}/../../app.framecore/src/main/resources</directory>
                <includes>
                    <include>**/*.sql</include>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <targetPath>${basedir}/target/classes/</targetPath>
                <directory>${basedir}/../app.pms.cust.api/src/main/resources</directory>
                <includes>
                    <include>**/*.sql</include>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
   ...
</build>

不过这里需要注意的是,一旦在build中指定了resources节点,必须自己手动添加所有待整合的资源文件。因为指定了resources,maven在打包时将严格按照用户指定的resources处理资源文件。所以这里推荐方法1.

posted @ 2018-03-19 18:20  Franson  阅读(3480)  评论(0编辑  收藏  举报