在测试中,当我设置 pom.xml 的完整路径时,一切正常:
{code}
File[] files = Maven.resolver().loadPomFromFile("/full/system/path/pom.xml").importDependencies(ScopeType.TEST, ScopeType.PROVIDED).resolve().withTransitivity().asFile();
{code}
在很多例子中,只使用了pom.xml,所以我尝试了:
{code}
File[] files = Maven.resolver().loadPomFromFile("pom.xml").importDependencies(ScopeType.TEST, ScopeType.PROVIDED).resolve().withTransitivity().asFile();
{code}
但在这种情况下我遇到了异常:
Path to the pom.xml file must be defined and accessible
如果我尝试传递“”../pom.xml”,结果相同。我必须将 pom.xml
中的所有依赖项包含到在 arquillian 测试期间部署的 war 存档中,即有解决方法吗?
理想情况下,我想重用用于构建项目的 pom.xml 。我不想在“src/test/resources”文件夹中有一个单独的 pom.xml 文件。
编辑:我已经了解了 @baba 的主要思想,但我没有通过处理 pom.xml,而是通过 Maven 资源过滤设置了 basedir
属性:
我已将 tests.properties
文件添加到 test/resources
中,并在文件中添加了一个属性:
basedir=${basedir}
在 pom.xml 中我使用了 [ http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html]
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
在测试中,我已加载所有依赖项:
ResourceBundle resourceBundle = ResourceBundle.getBundle ("tests");
String baseDir = resourceBundle.getString("basedir");
File[] files = Maven.resolver().loadPomFromFile(baseDir + File.separator + "pom.xml").importDependencies(ScopeType.TEST, ScopeType.PROVIDED).resolve().withTransitivity().asFile();
最佳答案
你必须首先认识到一件事,然后再认识到另外两件事。 这是第一件事:
- 一旦您谈论 jar 中的相对路径而不是完整路径,您就(主要)谈论类路径文件。所以你可能应该看看 LoadPomTask其中有一个方法
public static LoadPomTask loadPomFromClassLoaderResource(final String pathToPomResource)
现在我们意识到没有办法避免类路径,我们必须回答另外两个问题:
- pom.xml 文件是否默认捆绑在类路径中(答案:是)(在哪里?默认->
META-INF/maven/${groupId}/${artifactId}/pom.xml
) - 如果我使用像 Surefire 这样的测试框架,它是否会更改类路径,并且 pom.xml 在执行测试时是否在同一位置可用(答案:是的,大部分情况)
建议的解决方案:
由于 META-iNF/MAVEN/${groupId}/${artifactId}
可能会发生变化,因此您不应在代码中引用从那里加载内容的内容 。所以我建议你看看maven-resources-plugin 。这将允许您在编译期间将 pom.xml 文件复制到您选择的位置(我实际上建议您将其复制到两个位置:
/src/main/resources
和
/src/test/resources
在 process-resources
和 process-test-resources
阶段相应地: see maven-build-lifecycles
总结一下:
如果您想使用类型 (pom.xml) 的位置而不是完整路径,则需要从类路径加载 pom.xml
您需要在编译阶段使用 maven-resources-plugin 将 pom.xml 文件包含在类路径中
关于java - 来自 pom.xml 的 Maven 解析器、Shrinkwrap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32184869/