1

在测试中,当我设置 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 测试期间部署的,有解决方法吗?理想情况下,我想重用pom.xml 用于构建项目。我不想在“src/test/resources”文件夹中有单独的 pom.xml 文件。

编辑:我有@baba 的主要思想,但是我没有处理 pom.xml,而是basedir通过 maven 资源过滤设置了一个属性:

我已将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();
4

1 回答 1

2

你必须先了解一件事,然后再了解另外两件事。这是第一件事:

  1. 一旦您谈论 jar 中的相对路径而不是完整路径,您就(主要)谈论类路径文件。所以你可能应该看看LoadPomTask它有一个方法

公共静态 LoadPomTask loadPomFromClassLoaderResource(final String pathToPomResource)

现在我们意识到没有办法避免类路径,我们必须回答另外两个问题:

  1. pom.xml 文件是否默认捆绑在类路径中(答案:是)(在哪里?默认情况下-> META-INF/maven/${groupId}/${artifactId}/pom.xml
  2. 如果我使用的是像surefire这样的测试框架,它是否会更改类路径,并且在执行测试时 pom.xml 是否在同一位置可用(答案:是的,大多数情况下)

建议的解决方案:由于META-iNF/MAVEN/${groupId}/${artifactId}可能会发生变化,因此您不应在代码中引用从那里加载内容的引用。所以我建议你看看 maven-resources-plugin。这将允许您在编译期间在您选择的位置复制 pom.xml 文件(我实际上建议您将其复制到两个位置:

/src/main/resources

/src/test/resources

在相应process-resources的阶段和process-test-resources阶段:请参阅 maven-build-lifecycles

把它们加起来:

  1. 如果要使用类型 (pom.xml) 的位置而不是完整路径,则需要从类路径加载 pom.xml

  2. 您需要在编译阶段使用 maven-resources-plugin 将 pom.xml 文件包含在您的类路径中

于 2015-08-24T15:28:12.967 回答