Search code examples
mavenopen-liberty

How do I include third party libraries in openLiberty maven build


I'm currently running a PoC with open Liberty and I'm having a bit of trouble with libraries.

The short of it is our project has a few third party jars that need to be included as libraries, but I can't figure out how to include them when I run mvn install in my Open Liberty project.

I'm trying to configure them in server.xml as follows:

    <library id="MyLib" name="My Libraries">
        <fileset dir="${server.config.dir}/myLib/" includes="*.jar" id="myLib"/>
    </library>

I had hoped they would be picked up by the maven build, but obviously not.

What steps do I need to take to make make sure my library jars are placed in the correct place when running mvn install?


Solution

  • First of all, I will recommend the easiest approach which is packaging the libraries inside of your application (no server.xml <library> config needed this way). If you are using the maven-war-plugin, then any non-provided dependencies will automatically end up in WEB-INF/lib/ of your application. However, if you have more than 1 application in your Liberty server that need the same libraries, this may not be a good solution.


    On to your original question, you can use the maven-dependency-plugin to copy any maven artifact into a particular location during the build. When using this plugin to set up a <library>, be sure to bind the copy step to the prepare-package phase.

    Here is an example of adding JUnit to ${server.config.dir}/myLib/ during a Maven build:

    <project>
      [...]
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
              <execution>
                <id>copy</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>copy</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>3.8.1</version>
                  <type>jar</type>
                  <overWrite>false</overWrite>
                  <outputDirectory>${project.build.directory}/liberty/wlp/usr/servers/myServer/myLib</outputDirectory>
                  <destFileName>junit.jar</destFileName>
                </artifactItem>
              </artifactItems>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>