Getting access to exported functions from a lib in a different app

Ok, this is going to be quite the ramble… I hope you understand what I am trying to do, bear with me :smiley:

I am trying to fiddle around with the different parts of Enonic XP, trying to get to grips with how everything is connected together / learn. As part of this I have been trying to make a js lib in one .jar. I have another separate .jar that contains an app with a controller, from which I want to use the first js lib.

I was hoping that I could require() the lib into my app, much like you do with lib-thymeleaf for rendering content, but I am having some trouble figuring out what path to use for referring to a file in a “different jar” (if this is at all possible).

The app is installed on the site I created. Should libs appear in the install list for a site? Mine doesn’t, but I don’t know if this is intended or not.

Lets say the app has package name no.tine.xp.testapp
and the lib har package name no.tine.xp.testlib

First I tried var testlib = require('/lib/test/testlib.js'); but naturally this made it look for the testlib.js in the app .jar instead of inside the lib jar.

Next I tried: var testlib = require('no.tine.xp.testlib:/lib/test/test.js');
This gives me a a:

Resource [no.tine.xp.testapp:/lib/no.tine.xp.testlib:/lib/test/testlib.js] was not found (com.enonic.xp.resource.ResourceProblemException)

so I guess that’s not the correct way to do it either.

I read that you can include libs in your app using gradle dependencies over at http://xp.readthedocs.io/en/stable/reference/libraries/index.html but I was hoping to just reference a separately installed package without including it as a specific dependency in the gradle file (since it’s not available on maven etc).

Is it possible to access a lib (that is not necessarily referenced in the deps of build.gradle in the app) or do I have to make the lib available to the app at build time by including it in the the gradle dependencies or something?

If I am barking up the wrong tree, let me know :wink:

You can use a lib in another lib. See an example of this in the lib-recaptcha.

See where the build.gradle dependencies has include "com.enonic.xp:lib-portal:${xpVersion}"

This only works because the portal lib exists in the repositories.maven url 'http://repo.enonic.com/public'

I think you will need to put it up on a repo somewhere like GitLab.

Then in your other lib file, you would use a require to load the lib file.
var myTestLib = require('/lib/test/testLib');
This will look for an index.js file in the testLib folder, or a file named testLib.js in the test folder.

Ok, looks like I will have to host the lib somewhere to get this working then. Thanks for the pointers :slight_smile:

Apparently I might be able to use Jitpack to reference a github repo directly from the gradle dependencies, like this: http://stackoverflow.com/a/32682034

If you don’t want to bother with setting up maven repositories etc you can always do this in build.gradle:

dependencies {
    ...
    include 'com.enonic.lib:text-encoding:0.5.0'
    runtime files('../projectName/build/libs/projectName-1.0.0-SNAPSHOT.jar')
}
3 Likes

Ah, naturally. That’s very handy.