一般公司maven項(xiàng)目都是多模塊,這樣解耦會比較方便,但是經(jīng)常會遇到多個模塊的某個配置文件一樣,如果每個模塊都配置一個,那樣最后打包會很臃腫,以下方法可以解決這個問題。
資源共享
共享資源插件可以用于在多模塊構(gòu)建中的模塊之間共享資源。在以下示例中,我們有一組文件,我們想在項(xiàng)目的幾個模塊中復(fù)用這些資源。
設(shè)置一個用于共享資源的模塊
創(chuàng)建一個名為common的新模塊。將文件放在以下目錄中,確保共享文件位于src / main / resources目錄中:
common
|
+- src
| |
| `- main
| |
| `- resources
| |
| +- kafka_consumer.properties
| |
| `- redis.properties
|
`- pom.xml
共享資源的POM 應(yīng)該如下所示:
<project>
...
<groupId>org.test</groupId>
<artifactId>common</artifactId>
...
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.8.0</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*.properties</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
這將在generate-resources階段將共享資源捆綁到JAR文件中。這意味著其他模塊可以在此后的任何階段使用這些資源。
配置其他模塊以使用共享模塊
要在另一個模塊中使用共享資源,您需要按如下方式配置插件:
<project>
...
<groupId>org.test</groupId>
<artifactId>resource-consumer</artifactId>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.8.0</version>
<configuration>
<resourceBundles>
<resourceBundle>org.test:common:${project.version}</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
這將檢索common模塊的捆綁資源,處理捆綁中的每個資源,并將它們放入resource-consumer模塊的$ {project.build.directory} / maven-shared-archive-resources目錄中。
具體學(xué)習(xí)需要查看官方文檔:http://maven.apache.org/plugins/maven-remote-resources-plugin/examples/sharing-resources.html