2

I have written code for Google Analytics API in Liferay DXP. I have imported the following jars by writing them in build.gradle

    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'

Also I have included the same in bnd.bnd file using the following code:

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

The module compiles fine and I am able to get jar. But when I deploy this jar on server I keep getting Unresolved requirement:Import-Package: error.

I know that the issue is with the Transitive dependencies. I read somewhere on Liferay community that the things that we mention in build.gradle is available at compile time and it does not looks for Transitive dependencies at compile time but at runtime we need to have transitive dependencies as well.

Isnt there any way that Gradle can download transitive dependencies on its own instead of me mentioning them individually in build.gradle file.

Community
  • 1
  • 1
Dhruv Pandey
  • 482
  • 6
  • 18

1 Answers1

0

With Include-Resource (or -includeresource) specifying @some.jar, you're copying all of the jar's content into your own jar. This is a build instruction.

For compile time, gradle can identify and download transitive dependencies, but note that some of them might be optional - in that case it's questionable if you want to have them all. And also: Note that a compile time dependency does not necessarily mean that this dependency must be packaged for runtime. In fact, the packaging that you do, with IncludeResource, is done by bnd, not by gradle at all.

What you specify here, including 30 jars in your own jar, is not best practice. If they're OSGi bundles, you should just deploy them to the OSGi runtime separately. If they're not OSGi bundles, you should look for places where they're transformed to OSGi bundles. And only then should you include the left-over non-bundles in your own jar, unless you find better options (e.g. turn them into bundles yourself and send pullrequests upstream)

For demonstration: Let me unroll your list of jars from the IncludeResource directive, that you have entered in one line:

  • @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 (oops, repeated)
  • @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

While I'd discourage to keep up including 30 jars in your own jar, there's another option for those dependencies that aren't OSGi Bundles: If there are any required (non-optional) transitive dependencies that would need to be included, I understand that the compileInclude directive for gradle will automatically include them.

I've recently recorded a video about the different gradle options, that soon (after proper edits) will be added to the (free) OSGi Basics lesson on Liferay University - the preliminary edit of the video might help understand what's happening when you include third party code in your own jars- you can find it here.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • thanks I will go through the video..Actually I had not included all the 30 dependenices in first place. If you check this link https://developers.google.com/api-client-library/java/apis/analytics/v3 I had added only 1 gradle dependency mentioned in this page. But at time of deployment I kept getting the Unresolved requirement:Import-Package: error – Dhruv Pandey Jun 14 '19 at 08:34
  • The missing import is a runtime issue, not a compiletime issue. You'll need to make sure that the transitive dependencies (optional - if you use them - and mandatory ones) are resolvable at runtime. But it's got nothing to do with the compiletime dependency. By including them all in your own jar, you're just making it extremely heavyweight. – Olaf Kock Jun 14 '19 at 09:33
  • Its getting a bit cofusing for me. Things work at compile time. This means that those dependenices are available at compile time. What different I need to do at runtime to fix the issue. For example I am currently getting this error: Unresolved requirement: Import-Package: com.google.api.client.googleapis; version="[1.25.0,2.0.0)"_ [Sanitized] Although I have already inlcuded google-api-client-1.25.0.jar in my bnd.bnd file – Dhruv Pandey Jun 14 '19 at 09:43
  • Note that the package's version isn't necessarily the bundle's version. In fact, [it shouldn't be](https://liferay.dev/blogs/-/blogs/best-practice-track-package-versions-not-bundle-versions). (on the other hand: You're consuming external libraries, so they might be identical - you'll have to validate which package you have available in that jar. I'm not sure if you can lose this version information when you just `IncludeResource` all the jars - that *might* be the culprit. – Olaf Kock Jun 14 '19 at 11:23