mavenjakarta-eeopen-libertyjava-ee-8liberty-maven-plugin

Install a default DataSource in OpenLiberty by maven plugin


I was trying to set a DefaultDataSource for openliberty 20.0.0.1.

The src/liberty/config/server.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>javaee-8.0</feature>
    </featureManager>

    <!-- This template enables security. To get the full use of all the capabilities, a keystore and user registry are required. -->

    <!-- For the keystore, default keys are generated and stored in a keystore. To provide the keystore password, generate an 
         encoded password using bin/securityUtility encode and add it below in the password attribute of the keyStore element. 
         Then uncomment the keyStore element. -->
    <!--
    <keyStore password=""/> 
    -->

    <!--For a user registry configuration, configure your user registry. For example, configure a basic user registry using the
        basicRegistry element. Specify your own user name below in the name attribute of the user element. For the password, 
        generate an encoded password using bin/securityUtility encode and add it in the password attribute of the user element. 
        Then uncomment the user element. -->
    <basicRegistry id="basic" realm="BasicRealm"> 
        <!-- <user name="yourUserName" password="" />  --> 
    </basicRegistry>

    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint id="defaultHttpEndpoint"
                  httpPort="9080"
                  httpsPort="9443" />

    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>

    <!-- Derby Library Configuration -->    
    <library id="derbyJDBCLib">
      <fileset dir="${shared.resource.dir}" includes="derby*.jar"/>
    </library>

    <!-- Datasource Configuration -->
    <!-- remove jndiName="" to serve java:comp/DefaultDataSource for Java EE 7 or above -->
    <dataSource id="DefaultDataSource">
      <jdbcDriver libraryRef="derbyJDBCLib" />
      <properties.derby.embedded databaseName="taskdb" createDatabase="create"/>
    </dataSource>

</server>

And the liberty-maven-plugin is configured in pom.xml.

 <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>3.1.1</version>
                        <executions>
                            <execution>
                                <id>copy</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy</goal>
                                </goals>   
                            </execution>
                        </executions>                     
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.derby</groupId>
                                    <artifactId>derby</artifactId>
                                    <version>10.14.2.0</version>
                                    <type>jar</type>
                                    <overWrite>false</overWrite>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/liberty/wlp/usr/shared/resources</outputDirectory>
                        </configuration>                                    
                    </plugin>             
                    <!-- Enable liberty-maven-plugin -->
                    <plugin>
                        <groupId>io.openliberty.tools</groupId>
                        <artifactId>liberty-maven-plugin</artifactId>
                        <version>3.1</version>
                        <configuration>
                            <libertyRuntimeVersion>20.0.0.1</libertyRuntimeVersion>
                        </configuration>
                    </plugin>

I use maven-dependency-plugin to copy the derby to ${project.build.directory}/liberty/wlp/usr/shared/resources.

But when I run mvn clean liberty:run -Popenliberty. I found the derby is copied firstly, then the liberty:run goal will remove target/liberty, how to prevent liberty maven plugin to remove this folder?

I used a Maven profile openliberty for liberty server, check the complete codes.


Solution

  • It seems like you're running the mvn clean liberty:run -Popenliberty command after a previous build invocation that performs the app compilation, war packaging, etc. By running the clean phase in the command you provided, Maven will delete the target folder and thus the output of any previous invocations. This is not related to the behavior of the Liberty Maven Plugin.

    There is an open issue in the Liberty Maven Plugin repository to support copying of dependencies into the shared resource directory.

    As a workaround, you could do:

    mvn clean install liberty:create dependency:copy liberty:run -Popenliberty
    

    This way the LMP will create the server, then copy the Derby dependency, then start the server in the foreground.