使用 build-helper-maven-plugin 从 Jar 中排除文件
Excluding Files From Jar With build-helper-maven-plugin
我有一些与测试相关的 类 我想从项目的编译 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>
标签被插件完全忽略。
It does not even appear in the code completion in Eclipse
解决方案
虽然您不能排除 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>
最佳实践
我觉得有义务提醒 reader 这是 maven 中的 anti-pattern。理想情况下,您将为每个源目录创建单独的 pom 文件,然后将它们作为单独的模块添加到您的父 pom 文件中。我意识到这可能是一个更大的重构工作
我有一些与测试相关的 类 我想从项目的编译 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>
标签被插件完全忽略。
It does not even appear in the code completion in Eclipse
解决方案
虽然您不能排除 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>
最佳实践
我觉得有义务提醒 reader 这是 maven 中的 anti-pattern。理想情况下,您将为每个源目录创建单独的 pom 文件,然后将它们作为单独的模块添加到您的父 pom 文件中。我意识到这可能是一个更大的重构工作