我有一些测试相关的类,我想从项目的编译 jar 输出中排除。这是一个遗留项目,我不想通过将类移动到 src/test 来丢失现有的修订历史。因为我已经在使用 build-helper-maven-plugin,所以我想我可以在那里指定一个排除模式,但到目前为止我尝试过的任何东西似乎都不起作用。我跑
mvn clean install package
在我的项目根目录中,我看到了日志消息
[INFO] --- build-helper-maven-plugin:3.0.0:add-source (add-source) @ Person-ejb --- [INFO] Source directory: /media/psf/Home/Documents/workspace/optics/optics/Person/ejb/src added.
但是当我查看编译后的 jar 时,它仍然包含测试目录及其内容。知道我可能做错了什么吗?
我的 pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/</source>
</sources>
<excludes>
<exclude>**/test/**</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>add-reource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>resources/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
最佳答案
上下文
我认为这是 build-helper-maven-plugin 中的错误,或者可能是用法中的打印错误 documentation . <excludes>
标签被插件完全忽略。
解决方案
虽然您不能排除 builder-helper-maven-plugin 中的文件,但您可以排除 maven-compiler-plugin 中的文件。因此,如果您只是将以下配置添加到您的 maven-compiler-plugin 它应该排除 **/test/**
目录
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<excludes>
<exclude>**/test/**</exclude>
</excludes>
</configuration>
</plugin>
最佳实践
我觉得有义务提醒读者这是一个 anti-pattern在 maven 。理想情况下,您将为每个源目录创建单独的 pom 文件,然后将它们作为单独的模块添加到您的父 pom 文件中。我意识到这可能是一项更大的重构工作
关于java - 使用 build-helper-maven-plugin 从 Jar 中排除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64973148/