How to specify external jar dependency in Liferay DXP

Leave a Comment
How to specify external jar dependency in Liferay DXP While doing development in Liferay DXP your module may depends on external jar files . If required jar file is available in Liferay Nexus Repository then you can include them by using BND and Gradle file.

You can see Liferay nexus repository configured in your settings.gradle file under repository tag.

repositories {
maven {
url "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public"
}
mavenLocal()



In case if you dependency on jar file which is not part of this repository then either you can install your jar file in central repository  or you can setup your own local repository and install jar file in that (see instruction here to install 3rd party jar in local repository ).  You can configure your local maven repository in your gradle file.

By using -includeresource option available in bnd file you can include dependent jar in your module. Also this option supports wildcards as you can see in below example. Here you can see [0-9] it means any version from this range would be picked.

-includeresource:\
    @httpclient-[0-9]*.jar,\
@httpcore-[0-9]*.jar,\
@httpmime-[0-9]*.jar,\
@commons-httpclient-[0-9]*.jar,\
@commons-codec-[0-9]*.jar,\

Alternate approach (not recommended) 

If you have jar file which is required by almost all the modules of your work space then you can move jar file into lib/ext directory. Copying simply jar file in [lib/ext] folder won't work in Liferay DXP (it used to work in older version of Liferay). You need to specify list of packages in your portal-ext file and listed packages will be accessible to all the modules.

Disadvantage of this approach is that if your external jar has dependency on other jar files then you will have to bring all of them in [lib/ext] folder and you need to mentioned.

 #
    # Set a comma delimited list of Java packages that are exported to the
    # module framework as extra system packages.
    #

 module.framework.system.packages.extra=\
        javax.faces.convert,\
        javax.faces.webapp,\
        \
        #
        # Dynamic References
        #
        \
        com.ibm.crypto.provider,\
        com.mysql.jdbc,\
        com.sun.security.auth.module,\
        org.apache.naming.java,\
        sun.misc,\
        sun.security.provider,\
com.custom.common.service.bean,\
com.custom.common.service.client,\
com.custom.common.service.exception

 #
    # Set a comma delimited list of headers that are required in cases where the
    # dependency analysis of the WAB does not resolve the dependency. This can
    # occur when the headers are dynamically loaded using the Java reflection
    # API.
    #

module.framework.web.generator.headers.DynamicImport-Package=\
        com.liferay.portal.kernel.*,\
        com.liferay.portal.webserver,\
        com.liferay.portal.webserver.*,\
        com.sun.el.*,\
        javax.el.*,\
        javax.portlet.*,\
        javax.servlet.*,\
        org.apache.el.*,\
        org.apache.jasper.*,\
        org.apache.taglibs.standard.*,\
        org.eclipse.jetty.*,\
        \
        #
        # Dynamic References
        #
        \
        com.liferay.portal.dao.orm.hibernate,\
        com.liferay.portal.spring.hibernate,\
        com.mysql.jdbc,\
        org.apache.naming.java,\
        org.hibernate.type,\
        org.springframework.orm.hibernate3,\
com.custom.common.service.bean,\
com.custom.common.service.client,\
com.custom.common.service.exception
Note: During testing i have noticed that this approach does not works if we use Liferay workspace plugin 1.0.37 and it works if i use 1.0.40.

You can check Liferay workspace plugin version from settings.gradle file.

How To Include Static Resources (CSS/JS/Images) In Liferay OSGI Module
How To Include Custom Portlet In Theme - Liferay DXP
How To Specify Module Dependencies In Liferay DXP
How To Disable Re-Indexing Of All Journal Article Versions In Liferay DXP

Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment