1

I need to use some classes in a liferay module project from another Java project. I'm using gradle and when I deploy my app I get the Unresolved requirement error even though the gradle compiles my jar file.

Here is what I did so far, My gradle.build:

compile files('libs/p01-jdbc.jar')  
compileOnly group: "org.mql.biblio.dao.jdbc", name: "p01-jdbc", version: "default"

My bnd file:

Bundle-Name: Inter_Portlet_Communication
Bundle-SymbolicName: Inter_Portlet_Communication
Bundle-Version: 1.0.0
Export-Package: \
    com.mql.ipc.constants
Include-Resource: @p01-jdbc.jar

And my imports

import org.mql.biblio.dao.jdbc.DataBase;
import org.mql.biblio.dao.jdbc.DataSource;
import org.mql.biblio.dao.jdbc.MySQLDataSource;

P.S: The jar I'm compiling also uses a local mysql.jar (in a folder called lib).

Amine
  • 1,396
  • 4
  • 15
  • 32
  • 2
    Have you tried using `compileInclude` instead of `compileOnly`? Do you know the differences between `compile`, `compileOnly` and `compileInclude`? If not, you should take a look at this blog post: https://community.liferay.com/blogs/-/blogs/gradle-compile-vs-compileonly-vs-compileinclude – qutax Mar 20 '19 at 08:53
  • Using compileInclude solved the problem. – Amine Mar 20 '19 at 21:48

2 Answers2

3

You shouldn't blindly compileInclude everything until "it works". Check this article for what you've done - it's way more preferable to just deploy the dependencies to the OSGi container as well - in case they're also OSGi bundles. Or alternatively, make them OSGi bundles as well.

In case p01-jdbc.jar is already an OSGi bundle: Deploy it to Liferay. In case it's a custom library and not yet an OSGi bundle, I'd prefer to make it one, then see above.

In the case of the database driver, this might hit you later, because the mysql driver is often deployed globally as well, and you might hit ambiguous classes that can't be substituted for each other, or identical classes in different versions, coming through different classloaders.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • I actually tried the OSGi approach but it didn't work. The only way was to compileInclude both jars – Amine Mar 30 '19 at 19:31
  • 1
    I'd rephrase this slightly: `compileInclude` was *the first* way that worked *for you*. Note that compileInclude transitively includes all mandatory dependencies: It might be as simple as a missed transitive dependency, that you didn't deploy to the OSGi runtime. It's fine if compileInclude works for you, but I'll keep this answer here to give others the hint that it's not the preferable option, if they find the question & answers with their own problems. – Olaf Kock Mar 31 '19 at 09:40
  • Hi @OlafKock-I am writing for a hint :)-trying to use Orika jar as OSGI component. I have used the response from here: https://stackoverflow.com/questions/29494485/convert-existing-jar-to-osgi-bundle/29509794#answer-29509794 my .bnd file contains a classpath with core orika jar +its dependencies. I can deploy it, but at runtime, I receive an NoClassDef, but the class is there. Or I receive crazy ClassCast exceptions. When I use Option 3 from here https://liferay.dev/blogs/-/blogs/osgi-module-dependencies, I have no error. Any important aspect that must be taken into account when creating OSGI? – Victor Dec 11 '20 at 14:12
0

Found the answer to my problme, I had to use compileInclude instead of compile. I also needed to add a line for another jar needed by my dependency jar:

compileInclude files('libs/p01-jdbc.jar') 
compileInclude files('libs/mysql.jar') //This jar is used by the first one
Amine
  • 1,396
  • 4
  • 15
  • 32