我已经用Liferay DXP编写了Google Analytics(分析)API的代码。我通过在build.gradle中编写以下jar来导入
compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1'
compile group: 'com.google.api-client', name: 'google-api-client', version: '1.22.0'
compile group: 'com.google.api-client', name: 'google-api-client-appengine', version: '1.22.0'
compile group: 'com.google.api-client', name: 'google-api-client-gson', version: '1.22.0'
compile group: 'com.google.http-client', name: 'google-http-client-jackson2', version: '1.22.0'
compile group: 'com.google.api-client', name: 'google-api-client-java6', version: '1.22.0'
compile group: 'com.google.api-client', name: 'google-api-client-servlet', version: '1.22.0'
compile group: 'com.google.apis', name: 'google-api-services-analytics', version: 'v3-rev132-1.22.0'
compile group: 'com.google.http-client', name: 'google-http-client', version: '1.22.0'
compile group: 'com.google.http-client', name: 'google-http-client-appengine', version: '1.22.0'
compile group: 'com.google.http-client', name: 'google-http-client-gson', version: '1.22.0'
compile group: 'com.google.http-client', name: 'google-http-client-jackson2', version: '1.22.0'
compile group: 'com.google.http-client', name: 'google-http-client-jdo', version: '1.22.0'
compile group: 'com.google.oauth-client', name: 'google-oauth-client', version: '1.22.0'
compile group: 'com.google.oauth-client', name: 'google-oauth-client-appengine', version: '1.22.0'
compile group: 'com.google.oauth-client', name: 'google-oauth-client-java6', version: '1.22.0'
compile group: 'com.google.oauth-client', name: 'google-oauth-client-jetty', version: '1.22.0'
compile group: 'com.google.oauth-client', name: 'google-oauth-client-servlet', version: '1.22.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.1'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.0.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.1.3'
compile group: 'javax.jdo', name: 'jdo2-api', version: '2.3-eb'
compile group: 'org.mortbay.jetty', name: 'jetty', version: '6.1.26'
compile group: 'org.mortbay.jetty', name: 'jetty-util', version: '6.1.26'
compile group: 'com.google.code.findbugs', name: 'jsr305', version: '1.3.9'
compile group: 'javax.transaction', name: 'transaction-api', version: '1.1'
我还使用以下代码将其包含在bnd.bnd文件中:
Include-Resource: @google-api-client-1.22.0.jar,@google-http-client-1.22.0.jar,@google-api-client-java6-1.22.0.jar,@google-http-client-jackson2-1.22.0.jar,@google-http-client-gson-1.22.0.jar,@google-oauth-client-1.22.0.jar,@google-oauth-client-java6-1.22.0.jar,@gson-2.1.jar,@google-api-services-analytics-v3-rev132-1.22.0.jar,@jackson-core-2.1.3.jar,@commons-logging-1.1.1.jar,@google-api-client-appengine-1.22.0.jar,@google-api-client-gson-1.22.0.jar,@google-api-client-servlet-1.22.0.jar,@google-api-services-analytics-v3-rev132-1.22.0.jar,@google-http-client-appengine-1.22.0.jar,@google-http-client-jackson2-1.22.0.jar,@google-http-client-jdo-1.22.0.jar,@google-oauth-client-appengine-1.22.0.jar,@google-oauth-client-jetty-1.22.0.jar,@google-oauth-client-servlet-1.22.0.jar,@gson-2.1.jar,@httpcore-4.0.1.jar,@jackson-core-2.1.3.jar,@jdo2-api-2.3-eb.jar,@jetty-6.1.26.jar,@jetty-util-6.1.26.jar,@jsr305-1.3.9.jar,@transaction-api-1.1.jar
该模块编译正常,我可以得到jar。但是,当我将此jar部署到服务器上时,我会不断收到 Unresolved requirements:Import-Package:错误。
我知道问题在于传递依存关系。我在Liferay community的某个地方读到,我们在build.gradle中提到的内容在编译时可用,并且它在编译时不查找传递依赖关系,但在运行时我们也需要传递传递依赖关系。
Gradle可以通过任何方式自行下载传递依赖,而不必在build.gradle文件中单独提及它们。
答案 0 :(得分:0)
在Include-Resource(或-includeresource)中指定@some.jar
的情况下,您正在将jar的所有内容复制到自己的jar中。这是一个构建指令。
在 compile 时间内,gradle可以识别和下载传递依赖,但是请注意,其中一些可能是可选的-在这种情况下,是否要全部拥有它们是有问题的。还有:请注意,编译时相关性并不一定意味着必须为运行时将该类 打包为包装。实际上,使用IncludeResource进行的包装是由bnd完成的,而不是完全由gradle完成的。
您在此处指定的内容(不是在您自己的jar中包含30个jar)不是最佳做法。如果它们是OSGi捆绑软件,则应仅将它们分别部署到OSGi运行时。如果它们不是OSGi捆绑包,则应查找将其转换为OSGi捆绑包的地方。只有这样,您才应该将剩下的非捆绑包放入自己的jar中,除非找到更好的选择(例如,将它们自己打包成捆绑包并向上游发送pullrequests)
为进行演示:让我从您在一行中输入的IncludeResource指令展开您的jar列表:
虽然我不鼓励在自己的jar中继续包含30个jar,但对于那些不是OSGi捆绑包的依赖项,还有另一种选择:如果需要包含任何必需的(非可选)传递依赖项,我知道gradle的compileInclude指令会自动包含它们。
我最近录制了一个有关不同gradle选项的视频,不久(经过适当编辑后)将被添加到Liferay University的(免费)OSGi Basics课程中-对该视频的初步编辑可能有助于了解当您在自己的jar中包含第三方代码时,会发生什么情况-您可以find it here。