【问题标题】:Maven resolver from pom.xml, Shrinkwrap来自 pom.xml 的 Maven 解析器,Shrinkwrap
【发布时间】:2015-08-24 14:30:31
【问题描述】:

在测试中,当我设置 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,而是通过 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();

【问题讨论】:

  • 你在 Linux 上运行吗?
  • @AndrewSmiley,是的,我是。
  • @Alexandr 那更好。

标签: java maven integration-testing shrinkwrap


【解决方案1】:

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

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

public static 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-resourcesprocess-test-resources 阶段相应地:see maven-build-lifecycles

总结一下:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多