Maven是一个项目管理和构建自动化工具,使用标准的目录结构(如src/main/java、src/test/java等)。当项目需要使用自定义的源代码目录时,需要特别配置以确保构建过程正确处理这些文件。
首先,如果你需要添加自定义源目录,可以使用build-helper-maven-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/custom/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
要在编译中排除自定义src路径下的特定文件,有几种方法:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<excludes>
<exclude>**/custom/**/*Test.java</exclude>
<exclude>**/generated/**/*.java</exclude>
</excludes>
</configuration>
</plugin>
对于资源文件(非Java源文件),可以使用maven-resources-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<excludes>
<exclude>**/custom/config/*.properties</exclude>
</excludes>
</configuration>
</plugin>
<profiles>
<profile>
<id>exclude-custom</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>remove-source</id>
<phase>initialize</phase>
<goals>
<goal>remove-source</goal>
</goals>
<configuration>
<sources>
<source>src/custom/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
问题1:排除配置不起作用
可能原因:
解决方案:
**/test/**
)问题2:IDE和Maven行为不一致
解决方案:
mvn clean
后重新导入项目问题3:排除后依赖关系出错
解决方案: