Written by The Jahia Team
 
Developers
   Estimated reading time:
7.2.x.x 7.3.x.x

How I can use a class, from another module, in a jsp (a view in another module)?

Answer

In the module where the class is definded, the package must be exported in pom.xml (e.g. org.jahia.modules.classdepmod1.utils):

...
 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <Export-Package>
                            org.jahia.modules.classdepmod1.utils
                    </Export-Package>
                    <instructions>
                        <Jahia-Depends>default</Jahia-Depends>
                    </instructions>
                </configuration>
... 

In the module where the class should be used, must be a dependency to the oder module (e.g. classdepmod1) and import the package:

...
 <properties>
    <jahia-depends>classdepmod1,default</jahia-depends>
  </properties>
....
  <dependencies>
        <dependency>
            <groupId>org.jahia.modules</groupId>
            <artifactId>classdepmod1</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  </dependencies>
....
   <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
             <Import-Package>
                 org.jahia.modules.classdepmod1.utils,           
                 ${jahia.plugin.projectPackageImport},
                 *
             </Import-Package>
          <instructions />
        </configuration>
......
  

After a rebuild and redeployment of this modules, the class could be used in the jsp as import or fully qualified with package like:

...
<%@page import="org.jahia.modules.classdepmod1.utils.*" %>
....
  <% MyDependentUtil myDepUtil = MyDependentUtil.getInstance(); %>
  .....
  <%=myDepUtil.get....%>
....

Related links