如何从maven中的外部jar复制资源?
How to copy resources from external jar in maven?
我有一个基于多模块 Maven 的项目,具有以下模块结构:
--
--A
pom.xml
--B
pom.xml
-pom.xml
我在 parent.pom 文件中添加了一个依赖项。现在我想使用 A 模块中添加的依赖项中的资源。
有没有办法使用 maven 只将外部资源复制到 A 模块?
我尝试为此使用 maven-remote-resources-plugin,但它看不到外部资源。
您必须向应该使用它的模块添加依赖项。然后您可以使用 maven dependency plugin 从该依赖项中获取资源。
您可以在section下的父POM中定义模块A和B的依赖关系。然后在部分的子 POM(A 或 B)中引用它们。通过这种方式,您可以确保在所有子模块中只使用给定依赖项的单一版本。
所有关于maven依赖管理的信息都记录在https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
所以我找到了下一个解决方案。它不是最佳的,但它可以工作并且可以做我想要的。
它从外部jar(maven依赖)中提取资源文件并将其复制到class路径resources.
这不是最佳选择,因为我必须在文件移动到所需位置后删除空目录。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.group.id</groupId>
<artifactId>artifact-id</artifactId>
<version>${version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<includes>**/frontend/*.json</includes>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/classes/i18n</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-and-rename-file</id>
<phase>generate-sources</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/classes/i18n/META-INF/resources/i18n/frontend
</sourceFile>
<destinationFile>${project.build.directory}/classes/i18n/frontend/
</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete dir="${project.build.outputDirectory}/i18n/META-INF" includeemptydirs="true"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
我有一个基于多模块 Maven 的项目,具有以下模块结构:
--
--A
pom.xml
--B
pom.xml
-pom.xml
我在 parent.pom 文件中添加了一个依赖项。现在我想使用 A 模块中添加的依赖项中的资源。
有没有办法使用 maven 只将外部资源复制到 A 模块?
我尝试为此使用 maven-remote-resources-plugin,但它看不到外部资源。
您必须向应该使用它的模块添加依赖项。然后您可以使用 maven dependency plugin 从该依赖项中获取资源。
您可以在section下的父POM中定义模块A和B的依赖关系。然后在部分的子 POM(A 或 B)中引用它们。通过这种方式,您可以确保在所有子模块中只使用给定依赖项的单一版本。
所有关于maven依赖管理的信息都记录在https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
所以我找到了下一个解决方案。它不是最佳的,但它可以工作并且可以做我想要的。 它从外部jar(maven依赖)中提取资源文件并将其复制到class路径resources.
这不是最佳选择,因为我必须在文件移动到所需位置后删除空目录。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.group.id</groupId>
<artifactId>artifact-id</artifactId>
<version>${version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<includes>**/frontend/*.json</includes>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/classes/i18n</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-and-rename-file</id>
<phase>generate-sources</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/classes/i18n/META-INF/resources/i18n/frontend
</sourceFile>
<destinationFile>${project.build.directory}/classes/i18n/frontend/
</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete dir="${project.build.outputDirectory}/i18n/META-INF" includeemptydirs="true"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>