You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何运行从依赖项中复制的测试类?

如何运行从依赖项中复制的测试类?

我把一个包含测试代码的模块作为依赖项引入到了另一个项目中,依赖的配置如下:

<dependency>
    <groupId>ru.qwep</groupId>
    <artifactId>vendor-nexus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>ru.qwep</groupId>
    <artifactId>vendor-nexus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <classifier>tests</classifier>
    <scope>test</scope>
</dependency>

我已经确认测试类的复制是成功的——执行mvn clean install后,这些来自依赖项的测试类确实已经出现在target/test-classes目录下。

但现在遇到了问题:只有项目自身的测试会被执行,从依赖项复制过来的测试完全被忽略了。我没有修改过maven-surefire-plugin的默认配置,当前的插件配置如下,看起来这些外部测试就是被直接跳过了:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.3.1</version>
    <configuration>
        <dependenciesToScan>
            <dependency>ru.qwep.vendoradapter</dependency>
        </dependenciesToScan>

        <additionalClasspathElements>
            <additionalClasspathElement>
                ${project.build.directory}/test-classes/ru/qwep/vendoradapter
            </additionalClasspathElement>
        </additionalClasspathElements>

        <testSourceDirectory>
            ${project.build.directory}/test-classes/ru/qwep/vendoradapter
        </testSourceDirectory>

        <includes>
            <include>**/*</include>
        </includes>

    </configuration>
</plugin>

另外附上我自己模块的打包配置,可能对定位问题有帮助:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.4.2</version>
    <executions>
        <execution>
            <id>default-jar</id>

            <goals>
                <goal>jar</goal>
            </goals>
        </execution>

        <execution>
            <id>test-jar</id>
            <goals>
                <goal>test-jar</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <classifier>tests</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.3.1</version>
    <executions>
       
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <classifier>sources</classifier>
            </configuration>
        </execution>
     
        <execution>
            <id>attach-test-sources</id>
            <goals>
                <goal>jar</goal>

<!--                <goal>test-jar</goal>-->
            </goals>
            <phase>package</phase>
            <configuration>
                <classifier>test-sources</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>

备注:内容来源于stack exchange,提问作者Nikita Serzhevskiy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 09:09:32