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

如何在Maven构建中排除嵌套于src目录的test文件夹?

问题

项目采用非标准Maven目录结构,test文件夹嵌套在src目录下,结构如下:

.
├── pom.xml
├── src
│   ├── com
│   │   └── my_project
│   └── test
│       └── java
│           └── com

期望部署构建时,测试文件仅编译到test-classes目录,不混入主classes目录。当前pom.xml配置尝试用maven-compiler-plugin排除测试文件夹但未成功,配置如下:

<parent>
    <groupId>parent-project</groupId>
    <artifactId>parent-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <testOutputDirectory>target/test-classes</testOutputDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <!-- Exclude test folders from compilation -->
                    <excludes>
                        <exclude>test/**</exclude>
                    </excludes>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>

Maven生成的classpath:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
        <classpathentry excluding="test/**" including="**/*.java" kind="src" output="target/classes" path="src">
                <attributes>
                        <attribute name="optional" value="true"/>
                        <attribute name="maven.pomderived" value="true"/>
                </attributes>
        </classpathentry>
        <classpathentry kind="src" output="target/test-classes" path="src/test/java">
                <attributes>
                        <attribute name="test" value="true"/>
                        <attribute name="optional" value="true"/>
                        <attribute name="maven.pomderived" value="true"/>
                </attributes>
        </classpathentry>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
                <attributes>
                        <attribute name="maven.pomderived" value="true"/>
                </attributes>
        </classpathentry>
        <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
                <attributes>
                        <attribute name="maven.pomderived" value="true"/>
                        <attribute name="org.eclipse.jst.component.nondependency" value=""/>
                </attributes>
        </classpathentry>
        <classpathentry kind="output" path="target/classes"/>
</classpath>

Eclipse构建后,测试类同时出现在classes和test-classes目录:

target
├── classes
│   ├── META-INF
│   ├── com
│   │   └── my_project
│   └── test
│       └── java
│           └── com
│               └── my_project
│                   └── test
│                       ├── FirstTest.class
│                       └── SecondTest.class
└── test-classes
    └── com
        └── my_project
            └── test
                ├── FirstTest.class
                └── SecondTest.class

推测是排除模式设置错误,需要修正路径模式解决问题。

解决方案

1. 修正maven-compiler-plugin的排除路径

当前<exclude>test/**</exclude>的匹配规则不完整,因为sourceDirectory设为src,需要从src根目录匹配完整的测试文件路径。修改为:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <!-- 排除src下test目录的所有Java文件 -->
        <excludes>
            <exclude>test/**/*.java</exclude>
        </excludes>
        <release>11</release>
    </configuration>
</plugin>

如果需要排除test目录下所有类型文件,可使用:

<excludes>
    <exclude>test/**/*</exclude>
</excludes>

2. 修正资源配置的排除规则

当前资源配置将src作为资源目录,虽然排除了.java文件,但需确保测试目录的非Java资源也不会被打包到主classes,补充排除规则:

<resources>
    <resource>
        <directory>src</directory>
        <excludes>
            <exclude>**/*.java</exclude>
            <exclude>test/**</exclude>
        </excludes>
    </resource>
</resources>

3. 同步Eclipse构建路径

修改pom.xml后,在Eclipse中执行以下操作:

  • 右键项目 → Maven → Update Project...,勾选Force Update of Snapshots/Releases后确定,让Eclipse重新同步Maven配置。
  • 若问题仍存在,手动检查构建路径:右键项目 → Build Path → Configure Build Path,确认src目录的排除规则包含test/**,且src/test/java仅作为测试源目录输出到target/test-classes。

验证

执行mvn clean compile test-compile,查看target/classes目录,确认不再包含test相关的class文件,测试类仅存在于target/test-classes目录。


内容的提问来源于stack exchange,提问作者SkylerA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:27:05