java maven:将资源从依赖的 jar 复制到目标文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37057487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
maven: copy resource from dependent jar to target folder
提问by Alex
I need to copy a resource file (menu.xml) from the root of a dependent jar file to the root of the output directory of my current project, before the tests are executed during the build. The file must be available for the tests but also later for the main program using ...getClassLoader().getResourceAsStream("menu.xml")
.
在构建期间执行测试之前,我需要将一个资源文件 (menu.xml) 从依赖的 jar 文件的根目录复制到我当前项目的输出目录的根目录。该文件必须可用于测试,但稍后也可用于使用...getClassLoader().getResourceAsStream("menu.xml")
.
I'm trying the solution suggested in this questionbut it's not working.
我正在尝试这个问题中建议的解决方案,但它不起作用。
This is how the pom file looks like:
这是 pom 文件的样子:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>process-test-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeArtifactIds>pm1j-jar</includeArtifactIds>
<includes>menu.xml</includes>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
When I execute mvn clean process-test-resources
I see the following output:
当我执行时,mvn clean process-test-resources
我看到以下输出:
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ pm1-config ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-dependency-plugin:2.8:unpack-dependencies (resource-dependencies) @ pm1-config ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.709 s
[INFO] Finished at: 2016-05-05T15:01:06-03:00
[INFO] Final Memory: 30M/458M
[INFO] ------------------------------------------------------------------------
The file menu.xml is not copied to the target folder. How can I see what could be possibly wrong? I tried to run maven on debug log level and could see that the configuration was parsed correctly, but the plugin doesn't log any additional information of what is happening.
文件 menu.xml 不会复制到目标文件夹。我怎样才能看到可能有什么问题?我尝试在调试日志级别运行 maven,可以看到配置被正确解析,但插件没有记录正在发生的任何其他信息。
...
[DEBUG] (f) includeArtifactIds = pm1j-jar
[DEBUG] (s) includes = menu.xml
...
[DEBUG] (f) outputAbsoluteArtifactFilename = false
[DEBUG] (s) outputDirectory = c:\Dev\workspaces\config\pm1j\pm1-config\target\classes
...
[DEBUG] (f) project = MavenProject: com.expersoft.pm1j:pm1-config:3-SNAPSHOT @ c:\Dev\workspaces\config\pm1j\pm1-config\
pom.xml
[DEBUG] -- end configuration --
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.014 s
[INFO] Finished at: 2016-05-05T15:09:29-03:00
[INFO] Final Memory: 27M/328M
[INFO] ------------------------------------------------------------------------
回答by Alex
After some help from the comments, I could figure out the issue. The jar holding the menu.xml file is not in the dependencies list of my project.
经过评论的一些帮助,我可以找出问题所在。保存 menu.xml 文件的 jar 不在我的项目的依赖项列表中。
Here is a better explanation of what I was trying to achieve for better understanding:
这是对我试图实现的更好理解的更好解释:
My project is inside a maven multi module, and I need the information of this menu.xml which is maintained on another project of the same multi module. Apart from this file there's no other dependency to this other project, therefore, there's also no maven dependency to the jar.
我的项目在一个 maven 多模块内,我需要这个 menu.xml 的信息,它在同一个多模块的另一个项目上维护。除了这个文件之外,这个其他项目没有其他依赖项,因此,jar 也没有 maven 依赖项。
In order to avoid duplication of the file in my project, I helped myself so far with the maven-resources-plugin, which copies the file from the source directory of the other project. I never really liked this solution though, because it's dependent on having the source of this other project on the file system.
为了避免在我的项目中重复文件,我到目前为止使用了 maven-resources-plugin,它从另一个项目的源目录中复制文件。不过,我从来没有真正喜欢过这个解决方案,因为它依赖于在文件系统上拥有另一个项目的源代码。
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/../pm1-jJar/src/main/resources</directory>
<includes>
<include>menu.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Then I discovered the maven-depency-pluginand the unpackgoal which can extract the file out of the downloaded jar from the repository. This looked like a much cleaner solution. I tried it out and it worked actually well. The problem with this solution though was that the unpack plugin acts only in a later maven phase after testing and I do have some tests using the file as well.
然后我发现了 maven-depency-plugin和unpack目标,它可以从存储库中下载的 jar 文件中提取文件。这看起来像是一个更清洁的解决方案。我试了一下,效果很好。但是,此解决方案的问题在于解包插件仅在测试后的稍后 maven 阶段起作用,而且我也使用该文件进行了一些测试。
After some more research, I found the resource-dependencies plugin with the unpack-dependenciesgoal which looked like the perfect solution but at this stage I had completely forgotten that I actually don't have a dependency to the jar.
经过一些更多的研究,我发现了具有解包依赖关系目标的资源依赖关系插件,它看起来是一个完美的解决方案,但在这个阶段我完全忘记了我实际上对 jar 没有依赖关系。
回答by raneesh choudhary
A nice way to copy files from your dependency is to use Goal unpack :
从依赖项复制文件的一个好方法是使用 Goal unpack :
you can mention your files /directories in dir/** --> include everything from a directory
您可以在 dir/** 中提及您的文件/目录 --> 包含目录中的所有内容
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>group_id</groupId>
<artifactId>artifact_id</artifactId>
<version>1.0.0</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
<includes>directory_to_include/**</includes>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I attached goal unpack to the phase compile. Customise as per your need.
我将目标解包附加到阶段编译。根据您的需要定制。
回答by Simon
In order to use the maven-depency-plugin to unpack a jar before testing, the workaround is to use failsafe instead of surefire to have tests run after the unpacking.
为了在测试之前使用 maven-depency-plugin 解压 jar,解决方法是使用故障安全而不是surefire 来在解压后运行测试。
Simon
西蒙