Search code examples
mavenwarmaven-war-plugin

maven-war-plugin ignores <archiveClasses>


The <archiveClasses> option has no effect.

Running mvn clean compile war:exploded produces a war directory with .class files in the classes directory, and they are not archived into a jar in the lib directory neither. war:war produces same result.

Plugin configuration:

...
<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
      <archiveClasses>true</archiveClasses>
  </configuration>
</plugin>
...

Workarounds?

Maven version 3.3.3, maven-war-plugin version 2.6.

JIRA ticket – https://issues.apache.org/jira/browse/MWAR-355

This is the project in question: https://bitbucket.org/dmos62/raudondvaris


Solution

  • The first thing is you should move the plain configuration into a pluginManagement block like this:

     <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-war-plugin</artifactId>
              <version>2.6</version>
              <configuration>
                <archiveClasses>true</archiveClasses>
              </configuration>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    

    If you do the above the classes will be created within the war archive by using: mvn clean compile war:war

    ~/ws-git/so-questions/so-5 (master)$ unzip -t target/web-1.0.0-SNAPSHOT.war
    Archive:  target/web-1.0.0-SNAPSHOT.war
        testing: META-INF/                OK
        testing: META-INF/MANIFEST.MF     OK
        testing: WEB-INF/                 OK
        testing: WEB-INF/classes/         OK
        testing: WEB-INF/lib/             OK
        testing: WEB-INF/lib/commons-fileupload-1.1.1.jar   OK
        testing: WEB-INF/lib/commons-io-1.1.jar   OK
        testing: WEB-INF/lib/web-1.0.0-SNAPSHOT.jar   OK
        testing: WEB-INF/web.xml          OK
        testing: META-INF/maven/com.soebes.examples.so/web/pom.xml   OK
        testing: META-INF/maven/com.soebes.examples.so/web/pom.properties   OK
        testing: META-INF/INDEX.LIST      OK
    No errors detected in compressed data of target/web-1.0.0-SNAPSHOT.war.
    

    This will also working for your call mvn clean compile war:exploded.

       └── web-1.0.0-SNAPSHOT
            ├── META-INF
            └── WEB-INF
                ├── classes
                ├── lib
                │   ├── commons-fileupload-1.1.1.jar
                │   ├── commons-io-1.1.jar
                │   └── web-1.0.0-SNAPSHOT.jar
                └── web.xml
    

    The reason for this behaviour is simply cause by using a goal like war:war, or war:exploded there will be no life cycle started which means the configuration in the pom is not taken into account. If you like having a configuration for your command line calls you can do this by using a special configuration for command line calls like this (The id default-cli is the important part):

    <project>
      <build>
        <plugins>
          <plugin>
            <groupId...>
            <artifactId...>
            <executions>
              <execution>
                <id>default-cli</id>
                <configuration>
                  .....
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    which means having a special configuration for command line calls. Starting with Maven 3.3.1 it is possible having more than one configuration for command line calls by using it like:

    <project...>
      <build>
        <plugins>
          <plugin>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
            <executions>
              <execution>
                <id>first-cli</id>
                <configuration>
                     ....
                </configuration>
              </execution>
              <execution>
                <id>second-cli</id>
                <configuration>
                     ....
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    This can be used by maven via the following:

    mvn plugin:goal@second-cli
    mvn plugin:goal@first-cli
    

    See also the release notes for Maven 3.3.1.