shared-resources
——用于主要和测试资源。parent
|_ shared-resources
|_ child
目前,一个模块使用 maven-resource-plugin
来包含 shared-resources
。
当前的 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>
...
我希望重新组织依赖关系,使它们通过“shared-resources”模块的jar和test-jar打包方式进行包含,因为一些相关问题(例如“在多模块maven项目中指定共享资源”和“在maven项目之间共享测试资源”)建议这样做。
新的“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>
...
新建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>
...
然而,当我运行子项目的测试时,缺少资源的测试。
我以多种方式运行了我的构建:
maven clean test --projects child --also-make
maven clean install -DskipTests && mvn test --projects child
maven clean verify --projects child --also-make
我的权威测试是mvn clean install -DskipTests -pl :child -am && mvn test -pl :child -Dtest='PropertiesAccessTest'
其中属性访问测试为:
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)));
}
}
在shared-resources/src/test/resources/
中有一个对应的test.properties
文件(根据我理解,应该已经包含在上述配置中),但是测试总是失败并显示“文件不存在”。
我可以验证我的本地.m2
存储库按预期包含测试资源的测试jar。
我在这里做错了什么?
shared-resources
构件已经正确安装了吗?提供两个项目的完整 POM 可能会有所帮助,从给定的元素来看。 - Pierre B.share-resources
jar 和 test jar 的内容符合预期。 - jwilnershared-resources
的实际内容似乎不是主题。毫无疑问,这不是一个完美的模块结构,一些东西可以移动,但这超出了这个问题的范围 - 特别是当看起来这是支持的Maven行为时。 - jwilnerinstall
时,是否在构建会话中包含了test-resources
?你的本地m2
仓库中是否存在test-jar
?你使用什么代码来加载资源,显示了什么错误?(问了很多问题来追踪你的问题源头 ;)) - Pierre B.