SoFunction
Updated on 2025-03-04

Exclude specified directories or specified classes when packaging using Maven

Exclude specified directories or specified classes when packaged

1. Introduction

  • Through maven, I want to remove some test classes when I am using production package.
  • This will completely avoid the existence of test code in the production package.

2. Modify

  • In the maven project
  • Many configurations can be modified in the pom file
  • Add exclusion matching criteria under configuration > excludes node
  • Directly upload the code.
<build>
        <finalName>${}</finalName>
        <extensions>
            <extension>
                <!-- ... -->
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId></groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <excludes>
                        <!-- Remove the specified package,Its packaged classes-->
                        &lt;exclude&gt;**/demo/**</exclude>
                         <!-- Remove the specified class-->
                         <exclude>**/&lt;/exclude&gt;
                        &lt;exclude&gt;**/&lt;/exclude&gt;
                    &lt;/excludes&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
       &lt;/plugins&gt;
&lt;build&gt;

3. Pay attention

  • ** Indicates multi-level directory matching.
  • * Indicates first-level directory matching.

Notice:

  • It is the condition that adds excludes to the maven-compiler-plugin
  • I added it to maven-jar-plugin after reading it wrong at the beginning
  • The printed bag cannot be removed
  • I was puzzled for a long time before I realized that I had misunderstood. Be careful, please be careful

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.