websphere-libertyopen-libertyliberty-maven-plugin

How can I use Open Liberty dev mode with my Maven WAR project and include JavaScript and CSS files from outside my project (eg from my local machine)?


I have some JavaScript and CSS files which are not part of my WAR module source repository. They are not packaged in a Maven dependency either. I separately extract them to a path on my local system.

How can I use include these resources in my Maven WAR application and still take advantage of liberty dev mode for rapid, iterative development?


Solution

  • To include web resources from locations other than the typical src/main/webapp directory, configure a property and the maven-war-plugin with the <webResources> parameter with something like this:

    pom.xml

      ...
          <properties>
              <webResDir>/path/to/js-css</webResDir>
          <properties>
      ...
          <build>
            <plugins>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.2</version>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>${webResDir}</directory>
                           </resource>
                        </webResources>
                        <!-- I recommend this config which does a better job of removing any deleted/outdated resources, as of v3.3.2 -->
                        <outdatedCheckPath>/</outdatedCheckPath>
                    </configuration>
                </plugin>
    

    NOTES

    While the basic idea still applies at earlier versions, you'll get a better experience with recommended minimum versions:

    PREREQ MINIMUM VERSIONS

    PLUGIN-LEVEL CONFIGURATION

    Note too the maven-war-plugin configuration should be added at the plugin level for liberty dev mode to access it, rather than at the level of an execution defined under the maven-war-plugin.

    PROPERTY OVERRIDE

    As with Maven project properties in general, this can now be overridden:

    REFERENCES

    More examples here: https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html