maven test resources in a multi module project

Viewed 2812

I am refactoring a multimodule project that depends on one module -- shared-resources -- for both main and test resources.

parent
|_ shared-resources
|_ child

Currently, a module includes the shared-resources by using the maven-resource-plugin.

current child/pom.xml:

...
<resources>
  <resource>
    <directory>../shared-resources/src/main/resources</directory>
  </resource>
</resources>
<testResources>
  <testResource>
    <directory>../shared-resources/src/test/resources</directory>
  </testResource>
</testResources>
...

I would like to restructure the dependencies so that they are included via a jar and test-jar packaging of the shared-resources module, as some related questions (e.g. "Specify common resources in a multi-module maven project" and "Share test resources between maven projects") suggest.

new shared-resources/pom.xml:

...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
...

new child/pom.xml:

...
<dependency>
  <groupId>com.example</groupId>
  <artifactId>shared-resources</artifactId>
  <version>0.0.1</version>
</dependency>
<dependency>
  <groupId>com.example</groupId>
  <artifactId>shared-resources</artifactId>
  <version>0.0.1</version>
  <scope>test</scope>
  <type>test-jar</type>
</dependency>
...

When I run tests for child however, tests for lack of the resources.

I've run my build in a variety of ways:

maven clean test --projects child --also-make

maven clean install -DskipTests && mvn test --projects child

maven clean verify --projects child --also-make

My authoritative test has been mvn clean install -DskipTests -pl :child -am && mvn test -pl :child -Dtest='PropertiesAccessTest' where properties access test does:

  public class PropertiesAccessTest {

    @Test
    public void resourceAccessible() throws URISyntaxException, IOException {
        propertyExists(
                "abcdef=12345",
                "test.properties"
        );
    }

    private void propertyExists(String string, String fileName) throws IOException, URISyntaxException {
        URL url = getClass().getClassLoader().getResource(fileName);
        assertNotNull(url);
        assertTrue("file should exist", new File(url.getFile()).exists());

        assertTrue("file should contain property", Files.readAllLines(Paths.get(url.toURI()))
                .stream().anyMatch(l -> l.contains(string)));
    }
}

There is a corresponding test.properties in shared-resources/src/test/resources/ (which should be included as I understand the above config), but instead the test always fails with "file should exist."

I can verify that my local .m2 repository contains the test jar containing test resources as expected.

What am I doing wrong here?

1 Answers
Related