Maven: include folder in resource folder in the war build

10,393

Solution 1

For jars that are not distributed by a Maven repository, the simplest way is place the extra jars in the src/main/webapp/WEB-INF/lib directory of your project. Maven will by convention, include everything under the src/main/webapp in the final war artifact.

An additional method is to use the Maven War Plugin. It has the ability to add additional files to the final war artifact though plugin configuration.

In the build section of the POM add something like the following:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.4</version>
      <configuration>
        <webResources>
          <resource>
            <directory>src/main/resource/extra-jars</directory>
            <includes>
              <include>*.jar</include>
            </includes>
            <targetPath>WEB-INF/lib</targetPath>
          </resource>
        </webResources>
      </configuration>
    </plugin>
  </plugins>
</build>

The <configuration> section is the key to adding additional files.

  • The <directory> element defines the source location of the resource. The path is relative to pom.xml.
  • The <includes> element defines what files found in the above directory to include.
  • The <targetPath> element defines the destination directory in the WAR to which the files are copied.

Solution 2

These jars should be added as Maven dependencies, not by copying them into the lib folder. This is the sort of thing Maven is designed for.

Share:
10,393

Related videos on Youtube

TDC
Author by

TDC

Updated on September 15, 2022

Comments

  • TDC
    TDC over 1 year

    I've a folder named extra-jars in the src/main/rescource, but how can I include these in the build? I want them to be put in the lib folder with the rest of the jars. I tried including them through , but that didnt work.

  • TDC
    TDC about 10 years
    That's something I tried for the last three days, but these last jars won't startup my program if I retrieved them with a dependency, so I did it this way. It's not the Maven way, but there was no other way.
  • TrueDub
    TrueDub about 10 years
    What error messages were you getting? Adding jars to the lib folder and classpath is Maven's bread-and-butter, so maybe we can assist you with that
  • TDC
    TDC about 10 years
    It was with the Birt Run Time Viewer. Some jars weren't correct and I couldn't retrieve the correct ones for a reason. With those jars (from the dependency) it gave me a catalina internal error etc and with the 'local' jars it worked/s.
  • TrueDub
    TrueDub about 10 years
    Sounds like your "local" jars are of a different version that the ones Maven was retrieving. Can you verify that? Alternatively, you can add your "local" ones to your local maven repo and retrieve them from there. This is a simple one-command procedure. However, if the app you're building will be re-built by others this will not be a good idea.
  • TDC
    TDC about 10 years
    It will be re-built by others indeed. So that's why I was thinking to put them in the resource folder.
  • TrueDub
    TrueDub about 10 years
    Do you have a remote repository e.g. Nexus or Artifactory? Frankly, if others will be building it using Maven it should resolve all dependencies in the Maven way - are you sure your versions are the same?