Maven resolver from pom.xml, Shrinkwrap

1.6k Views Asked by At

In tests when I set a full path to pom.xml, everything works fine:

{code}
File[] files = Maven.resolver().loadPomFromFile("/full/system/path/pom.xml").importDependencies(ScopeType.TEST, ScopeType.PROVIDED).resolve().withTransitivity().asFile();
{code}

In a lot of examples, just pom.xml is used, so I tried:

{code}
File[] files = Maven.resolver().loadPomFromFile("pom.xml").importDependencies(ScopeType.TEST, ScopeType.PROVIDED).resolve().withTransitivity().asFile();
{code}

But in this case I get exception:

Path to the pom.xml file must be defined and accessible

Same result if I try to pass ""../pom.xml". I have to include all dependencies from pom.xml into the war archive, that is deployed during arquillian tests, is there a workaround? Ideally, I'd like to reuse the pom.xml that is used to build project. I don't want to have a separate pom.xml file in the "src/test/resources" folder.

EDIT: I've got the main idea of @baba, but instead of coping pom.xml, I've set a basedir property via maven resource filtering:

I've added the tests.properties file into test/resources, added a property in the file:

basedir=${basedir}

In pom.xml I've used the [http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html]

<build>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>

In tests, I've loaded all dependencies:

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

There are 1 best solutions below

0
On

You have to realize one thing first and then two other things. Here is the first thing:

  1. As soon as you are talking about relative paths in a jar and not fullpaths, you are talking (mostly) about classpath files. So you probably should take a look at LoadPomTask which has a method

public static LoadPomTask loadPomFromClassLoaderResource(final String pathToPomResource)

Now that we realized that there is no way to avoid the classpath, we have to answer two other questions:

  1. Is the pom.xml file bundled in the classpath by default (answer: yes) (where? by default-> META-INF/maven/${groupId}/${artifactId}/pom.xml)
  2. If I am using a test framework, like surefire, does it change the classpath and is the pom.xml available at the same location at the time of the execution of the tests (answer: yes, mostly)

Suggested solution: Since META-iNF/MAVEN/${groupId}/${artifactId} is subject to change, you shouldn't have reference in your code that loads the stuff from there. So I'd suggest you take a look at the maven-resources-plugin. Which'd allow you to copy the pom.xml file during compilation at a location of your choosing (I'd actually recommend you copy it to two locations:

/src/main/resources

and

/src/test/resources

During the process-resources and the process-test-resources phases accordingly: see maven-build-lifecycles

To sum it up:

  1. You need to load the pom.xml from the classpath if you want to be using a location of type (pom.xml) and not the full path

  2. You need to include the pom.xml file during the compilation phase using the maven-resources-plugin to be in your classpath