如何在osgi Liferay DXP中添加第三方依赖项

时间:2017-04-09 18:32:24

标签: oracle jdbc gradle liferay osgi

我正在使用Liferay DXP,我已经创建了一个服务构建器模块。在这个模块中,我必须使用" oracle.jdbc.driver.OracleDriver" class因为我已经开发了一个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类?

非常感谢你! 帕特里夏

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

非常感谢您的回答!

我已经尝试过但它不起作用。另外,我试图使用初始上下文实例在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)的服务构建器。

谢谢!