Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding Files From Jar With build-helper-maven-plugin

Tags:

java

maven

jar

I have some test related classes that I want to exclude from the compiled jar output for a project. This is a legacy project and I don't want to lose existing revision history by moving the classes to src/test. Since I am already using build-helper-maven-plugin I thought I would be able to specify an exclusion pattern there, but so far nothing I have tried seems to work. I run

mvn clean install package

in my project root and I see the log message

[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.

but when I look at the compiled jar it still containts the test directory and its contents. Any idea what I could be doing wrong?

My 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>
like image 494
pbuchheit Avatar asked Dec 19 '25 12:12

pbuchheit


1 Answers

Context

I believe this is a bug in build-helper-maven-plugin or possibly a misprint in the usage documentation. The <excludes> tag is completely ignored by the plugin.

It does not even appear in the code completion in Eclipse code completion

Solution

While you can't exclude files in the builder-helper-maven-plugin, you can exclude files in the maven-compiler-plugin. So if you simply add the following configuration to your maven-compiler-plugin it should exclude the **/test/** directories

<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>

Best Practice

I feel obligated to remind the reader that this is an anti-pattern in maven. Ideally you would create separate pom files for each source directory, then add them as separate modules in your parent pom file. I realize this may be a larger refactoring effort though

like image 105
Chris Maggiulli Avatar answered Dec 22 '25 07:12

Chris Maggiulli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!