繁体   English   中英

来自pom.xml,Shrinkwrap的Maven解析器

[英]Maven resolver from pom.xml, Shrinkwrap

在测试中,当我将完整路径设置为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();

您必须首先实现一件事,然后再实现另外两件事。 这是第一件事:

  1. 在谈论jar中的相对路径而不是fullpath时,您(主要是)在谈论classpath文件。 因此,您可能应该看看具有方法的LoadPomTask

公共静态LoadPomTask loadPomFromClassLoaderResource(最终字符串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阶段和process-test-resources阶段相应地, 请参见maven-build-lifecycles

把它们加起来:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM