在构建期间执行测试之前,我需要将资源文件 (menu.xml) 从依赖 jar 文件的根目录复制到当前项目的输出目录的根目录。该文件必须可用于测试,但稍后也可用于使用
...getClassLoader().getResourceAsStream("menu.xml")
的主程序。
我正在尝试这个问题中建议的解决方案,但它不起作用。
这就是 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>other-jar</includeArtifactIds>
<includes>menu.xml</includes>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
当我执行
mvn clean process-test-resources
时,我看到以下输出:
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ something ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-dependency-plugin:2.8:unpack-dependencies (resource-dependencies) @ something ---
[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] ------------------------------------------------------------------------
文件 menu.xml 未复制到目标文件夹。我怎样才能看出可能出现什么问题?我尝试在调试日志级别运行 Maven,并且可以看到配置已正确解析,但该插件不会记录所发生情况的任何附加信息。
...
[DEBUG] (f) includeArtifactIds = other-jar
[DEBUG] (s) includes = menu.xml
...
[DEBUG] (f) outputAbsoluteArtifactFilename = false
[DEBUG] (s) outputDirectory = c:\Dev\workspaces\config\something\target\classes
...
[DEBUG] (f) project = MavenProject: com.something:something:3-SNAPSHOT @ c:\Dev\workspaces\config\something\
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] ------------------------------------------------------------------------
经过评论的一些帮助,我可以解决这个问题。 保存 menu.xml 文件的 jar 不在我的项目的依赖项列表中。
这是我试图实现的更好的解释,以便更好地理解:
我的项目位于maven多模块内,我需要这个menu.xml的信息,该信息是在同一多模块的另一个项目上维护的。除了这个文件之外,其他项目没有其他依赖项,因此,该 jar 也没有 Maven 依赖项。
为了避免项目中的文件重复,到目前为止,我使用了 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>
然后我发现了maven-depency-plugin和unpack目标,它可以从存储库下载的jar中提取文件。这看起来是一个更干净的解决方案。我尝试了一下,效果确实很好。但这个解决方案的问题是解包插件仅在测试后的稍后 Maven 阶段起作用,并且我也使用该文件进行了一些测试。
经过更多研究,我发现资源依赖插件具有 unpack-dependency 目标,这看起来像是完美的解决方案,但在这个阶段我完全忘记了我实际上没有对 jar 的依赖。
从依赖项复制文件的一个好方法是使用 Goal unpack :
您可以在以下位置提及您的文件/目录 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>
我将目标解包附加到阶段编译中。根据您的需要定制。
为了在测试前使用 maven-depency-plugin 解压 jar,解决方法是使用failsafe 而不是 Surefire 在解压后运行测试。
西蒙