How can I include lib into my embedded OpenLiberty jar?

bmeynier :

I'm trying to set h2 database configuration in my OpenLiberty application.
You could find the project in my Github account I use the maven profile OpenLiberty to build an embedded jar.

<groupId>net.wasdev.wlp.maven.plugins</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<configuration>
    <assemblyArtifact>
        <groupId>io.openliberty</groupId>
        <artifactId>openliberty-runtime</artifactId>
        <version>${version.openliberty}</version>
        <type>zip</type>
    </assemblyArtifact>
    <serverName>GettingStartedServer</serverName>
    <stripVersion>true</stripVersion>
    <configFile>src/main/liberty/config/server.xml</configFile>
    <looseApplication>true</looseApplication>
    <installAppPackages>project</installAppPackages>
    <packageFile>${package.file}</packageFile>
    <include>${packaging.type}</include>
    <bootstrapProperties>
        <default.http.port>8080</default.http.port>
        <default.https.port>443</default.https.port>
        <app.context.root>${project.name}</app.context.root>
    </bootstrapProperties>
</configuration>

The build will provide a Jar where I can find my war application in the following folder: /wlp/usr/servers/GettingStartedServer/dropins/simple-microservice.war
Inside the war there is the H2 driver /WEB-INF/lib/h2-1.4.199.jar

My persistence.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="BOOK_PU" transaction-type="JTA">
        <jta-data-source>jdbc/bookjpadatasource</jta-data-source>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url"    value="jdbc:h2:mem:test" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />

            <property name="javax.persistence.jdbc.driver"    value="org.h2.Driver"/>
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
            <property name="javax.persistence.schema-generation.create-script-source" value="META-INF/sql/create.sql" />
            <property name="javax.persistence.sql-load-script-source" value="META-INF/sql/import.sql" />

            <property name="show_sql" value="true"/>
            <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>

        </properties>
    </persistence-unit>
</persistence>

And the server.xml

<server description="Sample Liberty server">
    <featureManager>
        <feature>jaxrs-2.1</feature>
        <feature>cdi-2.0</feature>
        <feature>jpa-2.2</feature>
    </featureManager>

    <applicationManager autoExpand="true" />
    <quickStartSecurity userName="admin" userPassword="adminpwd" />
    <keyStore id="defaultKeyStore" password="mpKeystore" />
    <logging traceSpecification="com.ibm.ws.microprofile.health.*=all" />
    <httpEndpoint host="*" httpPort="${default.http.port}"
                  httpsPort="${default.https.port}" id="defaultHttpEndpoint"/>

    <variable name="io_openliberty_guides_system_inMaintenance" value="false"/>

    <!-- H2 Library Configuration -->
    <library id="H2JDBCLib">
        <fileset dir="${server.config.dir}/lib" includes="h2-1.4.199.jar"/> <=== HERE
    </library>

    <!-- Datasource Configuration -->
    <dataSource id="h2test" jndiName="jdbc/bookjpadatasource">
        <!-- Define the DataSource class names on the <jdbcDriver> element -->
        <jdbcDriver
                javax.sql.XADataSource="org.h2.jdbcx.JdbcDataSource"
                javax.sql.ConnectionPoolDataSource="org.h2.jdbcx.JdbcDataSource"
                javax.sql.DataSource="org.h2.jdbcx.JdbcDataSource"
                libraryRef="H2JDBCLib"/>
        <!-- set the connection URL on the <properties> element.
             this corresponds to the setURL() method on H2's JdbcDataSource class.
             you may also list any properties here that have a corresponding setXXX method on H2's JdbcDataSource class -->
        <properties URL="jdbc:h2:mem:testdb"/>
    </dataSource>

</server>

`

When I launch my application: java -jar target/simple-microservice-1.0-SNAPSHOT.jar

I get a ClassNotFoundException:

[baptiste@DESKTOP-FUI7H3K simple-microservice]$ java -jar target/simple-microservice-1.0-SNAPSHOT.jar <br/>
Extraction des fichiers dans /home/baptiste/wlpExtract/simple-microservice-1.0-SNAPSHOT_8298781231136/wlp<br/>
Tous les fichiers du produit ont été extraits.<br/>
Lancement de GettingStartedServer (Open Liberty 19.0.0.9/wlp-1.0.32.cl190920190905-0148) sur OpenJDK 64-Bit Server VM, version 1.8.0_222-b10 (fr_FR)<br/>
[AUDIT   ] CWWKE0001I: Le serveur GettingStartedServer a été lancé.<br/>
[AUDIT   ] CWWKZ0058I: Recherche d'applications dans dropins.<br/>
[ERROR   ] CWWKZ0005E: Le serveur n'est pas configuré pour traiter la ressource à l'emplacement /home/baptiste/wlpExtract/simple-microservice-1.0-SNAPSHOT_8298781231136/wlp/usr/servers/GettingStartedServer/dropins/h2-1.4.199.jar.<br/>
[ERROR   ] CWWKE0701E: FrameworkEvent ERROR org.osgi.framework.ServiceException: Exception in com.ibm.ws.resource.internal.ResourceFactoryTrackerData$1.getService()<br/>
        at org.eclipse.osgi.internal.serviceregistry.ServiceFactoryUse.factoryGetService(ServiceFactoryUse.java:226)<br/>
        at [internal classes]<br/>
Caused by: java.lang.RuntimeException: java.sql.SQLNonTransientException: DSRA4000E: Aucune implémentation de org.h2.jdbcx.JdbcDataSource n'a été trouvée pour dataSource[h2test] avec la bibliothèque H2JDBCLib. Il se peut que le nom ou l'emplacement des fichiers JAR du pilote JDBC soit incorrect ou inaccessible. Recherche dans : []. Recherche sous les packages : [org.h2.jdbcx].<br/>
        at com.ibm.ws.resource.internal.ResourceFactoryTrackerData$1.getService(ResourceFactoryTrackerData.java:122)<br/>
        ... 1 more<br/>
Caused by: java.sql.SQLNonTransientException: DSRA4000E: Aucune implémentation de org.h2.jdbcx.JdbcDataSource n'a été trouvée pour dataSource[h2test] avec la bibliothèque H2JDBCLib. Il se peut que le nom ou l'emplacement des fichiers JAR du pilote JDBC soit incorrect ou inaccessible. Recherche dans : []. Recherche sous les packages : [org.h2.jdbcx].<br/>
        at com.ibm.ws.jdbc.internal.JDBCDriverService.classNotFound(JDBCDriverService.java:223)<br/>
        ... 1 more<br/>
<b>Caused by: java.lang.ClassNotFoundException: org.h2.jdbcx.JdbcDataSource</b><br/>
        at com.ibm.ws.classloading.internal.AppClassLoader.findClassCommonLibraryClassLoaders(AppClassLoader.java:546)<br/>
        ... 1 more<br/>

I'm suppose that my mistake coming from my server.xml at this line:

<fileset dir="${server.config.dir}/lib" includes="h2-1.4.199.jar"/>
Or in the OpenLiberty plugin configuration

What do you advise me to correct this issue ? Thx

njr :

The server.config.dir environment variable points to

/wlp/usr/servers/GettingStartedServer/

If you move the H2 jar to the following location to match the config, then it should work:

/wlp/usr/servers/GettingStartedServer/lib/h2-1.4.199.jar

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325765&siteId=1