多數情況下,我們使用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.