Need to exclude class files from the WAR using Maven

2.1k views Asked by At

This is one of those Ant to Maven headaches that we sometimes have. A few years ago, I had restructured many of these projects to use Ivy with Ant, and while I was at it, I did changes to make moving over to Maven (if we ever did) much simpler. That time has come.

I now have the build working and almost an exact match of our Ant build except that the Maven build is including classes that were excluded in the Ant build. Here's the build in Ant:

<target name="package" depends="compile,-ivy.retrieve">
    <war.macro destfile="${target.dir}/${war.name}" webxml="${main.webinf.dir}/web.xml">
        <webinf dir="${main.webinf.dir}"/>
        <lib dir="${lib.dir}"/>
        <classes dir="${main.destdir}">
            <!-- Wait. These weren't here before -->
            <exclude name="**/ejb/**" />
            <exclude name="**/FooJava.java" />
            <exclude name="**/BarJava.java" /> 
        </classes>  
    </war.macro>
</target>

Those two .java files, of course, do nothing. However, it's been this way in four releases, so apparently, it hasn't affected anything. However, that **/ejb/** line seems to be a problem.

In my pom.xml, I have the following:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
              <!-- None of these three lines work -->
              <packagingExcludes>**/ejb/**</packagingExcludes>
              <packagingExcludes>WEB-INF/classes/com/vegicorp/foo/bar/ejb/**</packagingExcludes>
              <packagingExcludes>${project.build.outputDirectory}/com/vegicorp/foo/bar/ejb/**</packagingExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

The build works. I can see the exclusion when I do mvn -X, but those three classfiles are still copied over. I should simply delete them from the source code if they're not used, but that's up to the developer to do. What I'm trying to do now is just get a matching *.war file that they get through Ant/Ivy builds.

According the maven-war-plugin documentation:

The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).

So, what am I doing wrong?

What is the Effective-Pom? - JimHawkens

<plugins>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
      <execution>
        <id>default-war</id>
        <phase>package</phase>
        <goals>
          <goal>war</goal>
        </goals>
        <configuration>
          <packagingExcludes>**/ejb/**</packagingExcludes>
          <archive>
            <manifestSections>
              <manifestSection>
                <name>Build-Information</name>
                <manifestEntries>
                  <Project-Name>${env.JOB_NAME}</Project-Name>
                  <Build-Number>${env.BUILD_NUMBER}</Build-Number>
                  <SVN-Revision>${env.SVN_REVISION}</SVN-Revision>
                </manifestEntries>
              </manifestSection>
              <manifestSection>
                <name>Module-Information</name>
                <manifestEntries>
                  <Group-ID>com.vegicorp</Group-ID>
                  <Artifact-ID>foobar</Artifact-ID>
                  <Version>6.0</Version>
                </manifestEntries>
              </manifestSection>
            </manifestSections>
          </archive>
        </configuration>
      </execution>
    </executions>
    <configuration>
      <packagingExcludes>**/ejb/**</packagingExcludes>
      <archive>
        <manifestSections>
          <manifestSection>
            <name>Build-Information</name>
            <manifestEntries>
              <Project-Name>${env.JOB_NAME}</Project-Name>
              <Build-Number>${env.BUILD_NUMBER}</Build-Number>
              <SVN-Revision>${env.SVN_REVISION}</SVN-Revision>
            </manifestEntries>
          </manifestSection>
          <manifestSection>
            <name>Module-Information</name>
            <manifestEntries>
              <Group-ID>com.vegicorp</Group-ID>
              <Artifact-ID>foobar</Artifact-ID>
              <Version>6.0</Version>
            </manifestEntries>
          </manifestSection>
        </manifestSections>
      </archive>
    </configuration>
  </plugin>

This just shows one of the three <packagingExcludes> I used. I also tried this with the other two.

1

There are 1 answers

5
JimHawkins On

You have to specify Version 2.1.1 of the plugin:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <packagingExcludes>**/ejb/**</packagingExcludes>                    
                <packagingExcludes>WEB-INF/classes/com/vegicorp/foo/bar/ejb/**</packagingExcludes>
                <packagingExcludes>${project.build.outputDirectory}/com/vegicorp/foo/bar/ejb/**</packagingExcludes>
            </configuration>
        </plugin>

Update:
the last version is 3.0.0