Seeker
Seeker

Reputation: 957

Exclude local WEB-INF/lib jars from packaged war file

I have mentioned the dependency for all the required JAR's in the pom.xml. Now, Since the dependencies are defined in POM, the JAR's will be automatically packed in the lib folder.

I want to exclude all the JAR's in the lib,present in the project source. The Dependency JAR's should be packaged inside the lib while building the WAR

I am using packaging excludes tag, To excludes the local jars. But it excludes all the jars in the packaged war file. I want to include only the maven dependencies.

                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <warSourceDirectory>WebContent</warSourceDirectory>
                            <warName>${war.name}</warName>
                            <webResources>
                                <resource>
                                    <directory>config</directory>
                                    <targetPath>WEB-INF/classes</targetPath>
                                    <directory>${maven.lib.dir}</directory>
                                    <targetPath>WEB-INF/lib</targetPath>                                
                                </resource>                                                                             
                            </webResources>
                            <archive>
                                <manifestEntries>
                                    <Built-Date>${maven.build.timestamp}</Built-Date>
                                </manifestEntries>
                            </archive>
                            <packagingExcludes>META-INF/*.xml</packagingExcludes>                           
                            <packagingExcludes>WEB-INF/*lib/</packagingExcludes>
                        </configuration>
                    </plugin>       

I can exclude few jars by specifying the names in the excludes tag. But i have too many jars to be explicitly written under the excludes tags

<packagingExcludes>
        WEB-INF/lib/commons-logging-*.jar,
        %regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
      </packagingExcludes>

I need some configuration, where in i can exclude the complete folder.

Upvotes: 2

Views: 8405

Answers (3)

Konstantin Pavlov
Konstantin Pavlov

Reputation: 985

Just set <scope>provided</scope> for the dependency and it will not be packaged into war. See more about maven scopes

If you need to exclude certain files from the WAR file, use packagingExcludes. Files will not be included to the final war but still be present in webappDirectory (the directory where the webapp is built).

As stated in Maven WAR Plugin reference:

Including and Excluding Files From the WAR

It is possible to include or exclude certain files from the WAR file, by using the and configuration parameters. They each take a comma-separated list of Ant file set patterns. You can use wildcards such as ** to indicate multiple directories and * to indicate an optional part of a file or directory name.

Here is an example where we exclude all JAR files from WEB-INF/lib:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Upvotes: 0

Seeker
Seeker

Reputation: 957

I got a work around for this. I am renaming the lib folder to ant-libs and then excluding it from packaging. Once my war is created i am renaming ant-libs folder back to lib.

Maven coderplus plugin:

              <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0.1</version>
                <executions>
                  <execution>
                    <id>rename-file</id>
                    <phase>compile</phase>
                    <goals>
                      <goal>rename</goal>
                    </goals>
                    <configuration>
                      <sourceFile>${basedir}/WebContent/WEB-INF/lib</sourceFile>
                      <destinationFile>${basedir}/WebContent/WEB-INF/ant-libs</destinationFile>
                    </configuration>
                  </execution>
                  <execution>
                    <id>rename-file-back</id>
                    <phase>package</phase>
                    <goals>
                      <goal>rename</goal>
                    </goals>
                    <configuration>
                      <sourceFile>${basedir}/WebContent/WEB-INF/ant-libs</sourceFile>
                      <destinationFile>${basedir}/WebContent/WEB-INF/lib</destinationFile>
                    </configuration>
                  </execution>
                </executions>
              </plugin>

Maven war plugin:

                 <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>WebContent</warSourceDirectory>
                        <warName>${war.name}</warName>
                        <webResources>
                            <resource>
                                <directory>config</directory>
                                <targetPath>WEB-INF/classes</targetPath>                                    
                            </resource>
                            <resource>
                            <directory>${maven.lib.dir}</directory>
                            <targetPath>WEB-INF/lib</targetPath>
                            </resource> 
                        </webResources>
                        <archive>
                            <manifestEntries>
                                <Built-Date>${maven.build.timestamp}</Built-Date>
                            </manifestEntries>
                        </archive>
                        <packagingExcludes>WEB-INF/*ant-libs/</packagingExcludes>
                    </configuration>
                </plugin>

Upvotes: 1

abc
abc

Reputation: 2381

I finally (after few edits) understand your problem. Unfortunately I think it would be extremely hard to do without something you would call a hack. This is hard because maven do not really support manually added jars. It's considered bad practice but we try do something about it. Here is what you might do:

  1. Copy maven dependencies to some temp directory
  2. Insert sub-directory into WEB-INF/lib
  3. Remove every *.jar from WEB-INF/lib, but not from sub-directory.

Here are more detailed steps:

First add copy-dependencies to some temp directory (in my case it is maven-deps)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/maven-deps</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>

Take in mind that in Eclipse IDE you get error in line with opening <execution>, but from tool-tip menu you can solve it by installing m2e connector from market (This plugin is not covered by default life-cycle. That's why you need connector).

After that we need tell maven to put this directory into war under WEB-INF/lib. Do the following:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>

        <webResources>
            <resource>
                <directory>target/maven-deps</directory>
                <includes>
                    <include>*.jar</include>
                </includes>
                <targetPath>WEB-INF/lib/maven-deps</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

This will insert directory maven-deps as sub-directory into WEB-INF/lib and exclude every *.jar from WEB-INF/lib but NOT from WEB-INF/lib/maven-deps

That way you will get only maven managed dependencies in WEB-INF/lib/maven-deps and this according to spec should be visible from war class loader, because class loader should load all jars from WEB-INF/lib and it's sub-directories.

I know this is kind of hack but at the moment I can't think any better solution to this.

Upvotes: 3

Related Questions