【问题标题】:How to add third party dependency in osgi Liferay DXP如何在 osgi Liferay DXP 中添加第三方依赖
【发布时间】:2017-04-09 18:32:23
【问题描述】:

我正在使用 Liferay DXP 并创建了一个服务构建器模块。在这个模块中,我必须使用“oracle.jdbc.driver.OracleDriver”类,因为我已经开发了一个 FinderImpl 来添加一个方法。这个方法调用了一个数据库中的程序,这个数据库不是Liferay的数据库,我正在使用其他数据库作为程序。

我在 lib/ext 中有 JDBC 驱动程序 (ojdbc7.jar)。

build.gradle 文件是:

repositories {
   mavenLocal()
   mavenCentral()
}

dependencies {
   compileOnly group: "biz.aQute.bnd", name: "biz.aQute.bndlib", version: "3.1.0"
   compileOnly group: "com.liferay", name: "com.liferay.osgi.util", version: "3.0.0"
   compileOnly group: "com.liferay", name: "com.liferay.portal.spring.extender", version: "2.0.0"
   compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.6.0"
   compileOnly project(":modules:test-manager:test-manager-api")
   compileOnly group: "com.oracle", name: "ojdbc7", version: "12.1.0"
}

buildService {
   apiDir = "../test--manager-api/src/main/java"
   osgiModule = true
   propsUtil = "com.test.manager.service.util.ServiceProps"
}

group = "com.test.manager"

bnd.bnd 文件是:

Bundle-Name: test-manager-service 
Bundle-SymbolicName: com.test.manager.service 
Bundle-Version: 1.0.0 
Liferay-Require-SchemaVersion: 1.0.0 
Liferay-Service: true

当我部署模块时出现此错误:

Error while starting bundle: file:/C:/projects/test/modules/test-manager-service/test-manager-service-service/build/libs/com.test.manager.service-1.0.0.jar
org.osgi.framework.BundleException: Could not resolve module: com.test.manager.service [536]_  Unresolved requirement: Import-Package: oracle.jdbc.driver_ [Sanitized]
        at org.eclipse.osgi.container.Module.start(Module.java:429)
        at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:402)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1253)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1225)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:512)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:361)
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:312)

有人知道是哪个问题吗?我应该如何在我的模块中添加 ojdbc7.jar 以使用 oracle.jdbc.driver.OracleDriver 类?

非常感谢您! 帕特里夏

【问题讨论】:

标签: oracle jdbc gradle liferay osgi


【解决方案1】:

不要将 JDBC 驱动程序添加到 lib/ext,而是尝试将其嵌入到您的模块中。您可以将-includeresource: lib/ojdbc7.jar=ojdbc7-[0-9]*.jar;lib:=true 之类的内容添加到模块的bnd.bnd 文件中,或者关注these instructions

【讨论】:

    【解决方案2】:

    非常感谢您的回答!

    我试过了,但它不起作用。另外,我尝试使用初始上下文实例在 FinderImpl 中查找 JNDI 资源,但出现此错误:

    javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.naming.java.javaURLContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.naming.java.javaURLContextFactory cannot be found
    

    所以,我更改了类加载器并且它起作用了。代码是:

    ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(PortalClassLoaderUtil.getClassLoader());
    InitialContext ctx = new InitialContext();
    DataSource datasource = (DataSource) ctx.lookup("java:comp/env/jdbc/test1");
    Connection connection = datasource.getConnection();
    Thread.currentThread().setContextClassLoader(origLoader);
    

    我不太喜欢这段代码,所以最后,我创建了一个带有外部数据库的服务构建器 (https://web.liferay.com/es/web/user.26526/blog/-/blogs/liferay-7-service-builder-and-external-databases)。

    谢谢!

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 2013-08-30
      • 2018-03-15
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2019-08-09
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多