未添加到类路径的测试资源

时间:2021-06-03 08:26:25

标签: java maven

使用 Maven 构建项目时未将测试资源添加到类路径

项目结构:

project/pom.xml
   /dist/pom.xml
        /src/main/resources/db/test.sql
   
   /test/pom.xml
        /src/test/com/loader/TestLoader.java

这里是dist模块的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>test1</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dist</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.12</version>
                <executions>
                    <execution>
                        <id>add-test-resource</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

这里是测试模块的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>test1</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <artifactId>dist</artifactId>
            <groupId>org.example</groupId>
            <version>1.0-SNAPSHOT</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

以及试图从 dist 模块读取 test.sql 的 JUnit 测试

@RunWith(JUnit4.class)
public class TestLoader {

    @Test
    public void test() throws Exception {
        Path path = Paths.get(ClassLoader.getSystemResource("db/test.sql").toURI());

        System.out.println(path.toFile().exists());
    }

当我从 IDEA 运行此测试时,一切正常,但是当我尝试使用命令 mvn clean install 时,出现找不到资源的错误。

可能是什么错误?

1 个答案:

答案 0 :(得分:1)

文件检测

ClassLoader.getSystemResource() 将根据测试类的执行方式返回不同类型的 URL:

  • 在 IDE 中,它是一个文件(“file:/path/to/db/test.sql”),所以 Path.get(uri) 没问题
  • 来自 Maven,它是一个 JAR (ZIP) 条目(“jar:file:/path/to/dist-1.0-SNAPSHOT.jar!/db/test.sql”),所以 Path.get(uri) 抛出一个 { {1}}

URL 足以知道条目是否存在。
如果是,则可以使用 FileSystemNotFoundException 读取其内容。

url.openStream()

Maven 配置

根据 URL url = ClassLoader.getSystemResource("db/test.sql"); if (url == null) { // File not found } else { try (InputStream is = url.openStream()) { (...) } } 模块的目标,可以有 2 种配置。

1- dist 是将在 dist 中测试的 API 的一部分

那么根本不需要使用 test 执行目标。

  • test-jar :可以删除整个 dist/pom.xml 部分
  • build : 没问题
2- test/pom.xml 仅用于测试(在 dist 模块和其他模块中)

然后,将 test 资源放在 test 目录中更加一致。

  • dist 应移至 db/test.sql
  • dist/src/test/resourcesdist/pom.xml 部分保留,但 test-jar 变得无用,可以删除
  • build-helper-maven-plugin :要使其工作,依赖项 test/pom.xml 是必需的
classifier