Search code examples
mavenwebspherewebsphere-libertyopen-liberty

How do I build a runnable JAR from a WebSphere Liberty / Open Liberty Maven WAR project?


I want to build a runnable/executable JAR (to run with java -jar my.jar), using Open Liberty, from my Maven WAR project.

If I run mvn package I get a WAR, but if I then run mvn liberty:package I get something like:

[INFO] CWWKM2001I: Invoke command is ["C:\git\lmparch1\test\target\liberty\wlp\bin\server.bat", package, defaultServer, --archive="C:\git\lmparch1\test\target\test.jar", --include=runnable].
[INFO] CWWKE0005E: The runtime environment could not be launched.
[INFO] CWWKE0031E: Specified defaultServer server does not exist; use the create action to create a new server. serverPath: C:\git\lmparch1\test\target\liberty\wlp\usr\servers\defaultServer
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  20.531 s
[INFO] Finished at: 2021-04-09T15:32:01-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal io.openliberty.tools:liberty-maven-plugin:3.3.4:package (default-cli) on project test: CWWKM2002E: Failed to invoke ["C:\git\lmparch1\test\target\liberty\wlp\bin\server.bat", package, defaultServer, --archive="C:\git\lmparch1\test\target\test.jar", --include=runnable]. RC= 2 but expected=[0]. -> [Help 1]

What goals am I supposed to configure and bind to which phases, etc. ?


Solution

  • The answer depends on whether you're using the liberty-maven-app-parent to set up your lifecycle (or similarly binding goals to phases on your own), vs. using something closer to the built-in war lifecycle bindings (without all the extra 'liberty' plugin goal bindings).

    Using Liberty Maven app parent (or similar)

    First, if you're using the io.openliberty.tools:liberty-maven-app-parent, e.g. from generating with the liberty-archetype-webapp_ then you simply need to use this as your liberty-maven-plugin configuration:

        <plugin>
            <groupId>io.openliberty.tools</groupId>
            <artifactId>liberty-maven-plugin</artifactId>
            <version>3.3.4</version>
            <configuration>
                <include>minify,runnable</include>
    

    and do mvn package.

    You'll see the runnable JAR created as something like: target/xyz.jar

    Using generic WAR project interactively

    The simplest way to do this is to let dev mode install Liberty and package and deploy the WAR, then Ctrl+C cancel out of it to package the runnable JAR.

    1. Run mvn liberty:dev to launch "dev mode" and wait for the server install and app deploy
    2. Type <Ctrl+C> when dev mode completes startup (either when you see Liberty is running in dev mode or once you see the app started CWWKT0016I application available message).
    3. Run mvn liberty:package -Dinclude=minify,runnable to generate the runnable JAR (something like: target/xyz.jar).

    Using generic WAR project in a CI env (e.g. build pipeline)

    If you're running non-interactively, where issuing the Ctrl+C isn't easy, you could do

    mvn package liberty:create liberty:install-feature liberty:deploy liberty:package -Dinclude=minify,runnable

    We have opened this issue to consider improving this use case.