1

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.

Charles
  • 4,372
  • 9
  • 41
  • 80
Hantsy
  • 8,006
  • 7
  • 64
  • 109
  • for a temporary workaround, you can put a downloaded version of the Derby `.jar` to your `src/main/config/liberty` folder. – rieckpil Jan 30 '20 at 13:23

2 Answers2

2

Starting from version 3.3 of the Liberty Maven Plug-in there is a copyDependencies parameter:

pom.xml

<plugins>
  <plugin>
    <groupId>io.openliberty.tools</groupId>
    <artifactId>liberty-maven-plugin</artifactId>
    <version>3.3.4</version> 
    <configuration>
      <!-- Usually best to add configuration at the plugin level rather than trying to configure particular executions -->
      <copyDependencies>
        <dependencyGroup>
          <!-- Relative to server config directory -->
          <location>lib/global/jdbc</location>
          <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
          </dependency>
        </dependencyGroup>
      </copyDependencies>
    </configuration>
      ...

server.xml

...
<!-- Derby Library Configuration -->    
<library id="derbyJDBCLib">
  <fileset dir="${server.config.dir}/lib/global/jdbc" includes="derby*.jar"/>
</library>
...

See documentation for further details.

Scott Kurz
  • 4,985
  • 1
  • 18
  • 40
ltlBeBoy
  • 1,242
  • 16
  • 23
1

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.

Charles
  • 4,372
  • 9
  • 41
  • 80
  • The `dependency-maven-plugin` copies the derby to *${project.build.directory}/liberty/wlp/usr/shared/resources* firstly, then `liberty:run` will remove the *${project.build.directory}/liberty* and create a openlibery in the *${project.build.directory}/liberty*, this is not relevant to Maven `clean` goal. – Hantsy Jan 30 '20 at 06:44
  • 1
    That is correct, the Liberty Maven Plugin uses an install marker to detect whether the runtime has been installed. It will force a reinstall of the runtime if it does not detect an existing install marker or it is out of date. This will delete anything already in the runtime directory. The provided workaround should work for you. – Charles Jan 30 '20 at 19:37
  • While using the dependency plugin was the best solution at the time, the answer from @ltlBeBoy leverages the 'copyDependencies' support added to liberty-maven-plugin. This is usually a better solution, since it is integrated into the "dev mode" loop and less verbose (at the expense of supporting fewer options than the dependency plugin). – Scott Kurz Jul 27 '21 at 15:22