通过 maven 插件在 OpenLiberty 中安装默认数据源

Install a default DataSource in OpenLiberty by maven plugin

我正在尝试为 openliberty 20.0.0.1 设置 DefaultDataSource。

src/liberty/config/server.xml是:

<?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>

liberty-maven-plugin配置在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>

我用maven-dependency-plugin复制德比到${project.build.directory}/liberty/wlp/usr/shared/resources.

但是当我运行mvn clean liberty:run -Popenliberty。我发现德比是先复制的,然后liberty:run目标会删除target/liberty,如何防止liberty maven插件删除这个文件夹?

我使用 Maven 配置文件 openliberty 作为自由服务器,检查 complete codes.

看起来你是 运行 mvn clean liberty:run -Popenliberty 命令 之前执行应用程序编译的构建调用之后,war 打包等。通过您提供的命令中的 运行 clean 阶段,Maven 将删除 target 文件夹,从而删除任何先前调用的输出。这与 Liberty Maven 插件的行为无关。

Liberty Maven 插件存储库中有一个 open issue 支持将依赖项复制到共享资源目录中。

作为解决方法,您可以这样做:

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

这样LMP会创建服务器,然后复制Derby依赖,然后在前台启动服务器。

从 Liberty Maven 插件的 version 3.3 开始,有一个 copyDependencies 参数:

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>
...

有关详细信息,请参阅 documentation