Expand my Community achievements bar.

SOLVED

Issue with creating custom tag library using OSGI bundle

Avatar

Former Community Member

I am trying to create custom tag libs by using OSGI bundles in cq. Actually i created all the necessary files but i could not place my mytags.tld file under META-INF folder in build time. For that i used "maven-bundle-plugin" plugin. Here is my code to include the resource in META-INF folder

    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
        <instructions>
        <Include-Resource>META-INF/myTags-${project.version}.tld=target/classes/META-INF/myTags-${project.version}.tld</Include-Resource>
        </instructions>
        <Export-Package>com.mine.*</Export-Package>
        <Import-Package>*;resolution:=optional</Import-Package>                    
        </configuration>
    </plugin>

Note: i have placed my tag file under "src\main\resources\META-INF\myTags"
I have used "<Include-Resource>" to include my tld file. But i could see my tld file under META-INF folder after jar file is created.

Could you anyone tell what could be the problem?

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Level 7

Do you get any other errors?
This is an example how  you can set it up (note that this is just a simple test for a function but works the same way for tags):

1) In the tld file:

<?xml version="1.0" encoding="ISO-8859-1" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>My tag library</description> <tlib-version>1.0</tlib-version> <short-name>mytags</short-name> <uri>http://my.company.com/taglibs/mytags/1.0</uri> <function> <name>getSomeText</name> <function-class>com.mycompany.common.MyTest</function-class> <function-signature>String getSomeText(java.lang.String)</function-signature> </function> </taglib>

2. In the java file for the tag (called MyTest.java in the package com.mycompany.common):

public class MyTest { public static String getSomeText(String name) { return "Hi " + name + ", here is some text for you"; } }

3. Then exported the classes (in the pom) as you did with:
 

//... <Export-Package>com.mycompany.common.*</Export-Package> //..

4. Then in the .JSP file

//.. code <%@taglib prefix="mycompany" uri="http://my.company.com/taglibs/mytags/1.0" %> <div class="test-div"> ${mycompany:getSomeText("Johan")} </div> //... more code


Hope you get the idea.
/Johan

View solution in original post

19 Replies

Avatar

Level 7

I've got the same setup as you have written and put the file in the same location,
but I have the following line just underneat <import-package></import-package>:

<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
 

Maybe you could try and see if that solves your problem ?
Do you get any build messages/errors ?

/Johan

Avatar

Former Community Member

Hi Johan,

Thanks for your quick reply

I have included the above line in my pom.xml like

<plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
        <instructions>
        <Include-Resource>META-INF/myTags-${project.version}.tld=target/classes/META-INF/myTags-${project.version}.tld</Include-Resource>
        </instructions>
        <Export-Package>com.mine.*</Export-Package>
        <Import-Package>*;resolution:=optional</Import-Package>                    
        <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
        </configuration>
    </plugin>

 

I am getting the same error as below

[ERROR] Bundle com.mine-bundle:bundle:1.0-SNAPSHOT : Input file does not exist: target/classes/META-INF/myTags-1.0-SNAPSHOT.tld

 

Thanks

Avatar

Employee

You should remove all of that configuration from the bundle plugin. What you are describing is the default behavior and these configuration settings are only going to make your situation worse.

Avatar

Level 7

but your .tld file is not named myTags-1.0-SNAPSHOT.tld is it ?

Avatar

Former Community Member

Yes, My tld name is myTags.tld which is located into src\main\resources\META-INF\myTags.tld.

I have tried it like this "<Include-Resource>META-INF/myTags.tld=target/classes/META-INF/myTags.tld</Include-Resource>"

Again i am getting the error like as below

[ERROR] Bundle com.mine-bundle:bundle:1.0-SNAPSHOT : Input file does not exist: target/classes/META-INF/myTags.tld

 

Thanks

Avatar

Level 7

Okey. Seems to be a problem with the version numbers there.
The extra -${project.version} is somehow not recognised. Can you first try to verify that it works without that like:
myTags.tld=target/classes/META-INF/myTags.tld

In your tld file. Have you set the right version number (the same) there aswell ? 

Avatar

Former Community Member

Hi,

I have removed the version no and tried it like this

<Include-Resource>myTags.tld=target/classes/META-INF/myTags.tld</Include-Resource>

Bu ti am getting the error like "[ERROR] Bundle com.mine-bundle:bundle:1.0-SNAPSHOT : Input file does not exist: target/classes/META-INF/myTags.tld"

Here is my tld file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">
    <tlib-version>0.0.1</tlib-version>
    <short-name>POC</short-name>
    <uri>http://mine.com/bundles/cq/tags123</uri>
    <tag>
      <name>hello</name>
      <tagclass>com.mine.Hello</tagclass>
      <bodycontent>empty</bodycontent>
      <info>This is a simple hello tag</info>
    <attribute>
      <name>name</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    </tag>
</taglib>


Does it related to tlib-version?

Can you please tell how did you solve this issue? Is it possible you to provide your "maven-bundle-plugin" plugin? So that i can incorporate it

Thanks

Avatar

Level 7

I've done just like you wrote above,  META-INF/mytld.tld=target/classes/META-INF/mytld.tld 
Could there be a problem with the copying of files (during the package phase) with the maven-resources-plugin  ?
 

Avatar

Former Community Member

Am i using <Include-Resource> tag correctly? do i need to declare anywhere in my pom.xml in order to use Include-Resource tag?

I am sure there is a way to achieve it. Could you please suggest is there any other way to achieve this one?

Can you please share your <Include-Resource> tag alone if possible ? so that i can incorporate it.

Thanks

Avatar

Level 7

Oki, here's the part:

 

<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package> //Package exports </Export-Package> <Import-Package> *;resolution:=optional </Import-Package> <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> <Sling-Test-Regexp>.*Test</Sling-Test-Regexp> <Include-Resource>META-INF/myTld.tld=target/classes/META-INF/myTld.tld</Include-Resource> </instructions> </configuration> </plugin>

This is from one of the child pom files.
In the parent pom (parent for all the other bundles) there is also the maven-resources plugin which handles the copying of resources to the output directory.

This is the plugin code from there:
 

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> <version>2.6</version> </plugin>

Hope this will help you.
/Johan

Avatar

Former Community Member

Finally that tld file has placed correctly under META-INF folder by using resource tag as like below in pom file

            <resource>
            <targetPath>META-INF</targetPath>
            <directory>src/main/resources</directory>
            <includes>
            <include>myTags.tld</include>
            </includes>
            </resource>


Here is the another issue. When i include that tag library by using taglib directive like

<%@ taglib uri=" http://mine.com/bundles/cq/mytags" prefix="sample"%>

I am getting the below error like "The absolute uri: http://mine.com/bundles/cq/mytags cannot be resolved in either web.xml or the jar files deployed with this application"


But deployed jar file has the above uri. i don't know the exact issue. Do you have any idea why it is giving such error?

Thanks in advance.

Avatar

Former Community Member

anderson.nt wrote...

Finally that tld file has placed correctly under META-INF folder by using resource tag as like below in pom file

            <resource>
            <targetPath>META-INF</targetPath>
            <directory>src/main/resources</directory>
            <includes>
            <include>myTags.tld</include>
            </includes>
            </resource>


Here is the another issue. When i include that tag library by using taglib directive like

<%@ taglib uri=" http://mine.com/bundles/cq/mytags" prefix="sample"%>

I am getting the below error like "The absolute uri: http://mine.com/bundles/cq/mytags cannot be resolved in either web.xml or the jar files deployed with this application"


But deployed jar file has the above uri. i don't know the exact issue. Do you have any idea why it is giving such error?

Thanks in advance.

 


I have a similar problem. Which POM and where-abouts in that POM did you put the <resource> section you show above ?

I am using the CQ Blueprints archetype. For example this is what gets auto-generated in the xxx-taglib module POM (I added some comments starting with FRG) :-

<plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-Activator>com.cqblueprints.example.taglib.osgi.Activator</Bundle-Activator>
                        <!-- FRG
                        
                        The file : cqblueprints-example-taglib-1.0.0-SNAPSHOT.tld
                        
                        does NOT get generated when CLEANing this project in eClipse and will create an error
                        
                        doing :- mvn clean install
                        
                        DOES create the file successfully
                       
                         -->
                       <Include-Resource>META-INF/${project.artifactId}-${project.version}.tld=${project.build.outputDirectory}/META-INF/${project.artifactId}-${project.version}.tld,{maven-resources}</Include-Resource>
                       <Include-Resource>{maven-resources}</Include-Resource>
                        <Sling-Bundle-Resources>/META-INF/tags</Sling-Bundle-Resources>
                    </instructions>
                </configuration>
            </plugin>

Regards

Fraser.

Avatar

Level 7

I have included mine like this in the jsp:

<%@ taglib prefix="mytaglib" uri="http://taglib.my.test.com/taglibs/mytaglib/1.0"%>

and in my .tld file I have the following:


//code...
    <uri>http://taglib.my.test.com/taglibs/mytaglib/1.0</uri>
//code...

So they both needs to match for the jsp file to know which taglib it's looking for.

Do you have those as the same ?

/Johan

Avatar

Former Community Member

Thanks. URI issue has solved as i have followed your points above.

Now i am getting error - Unable to load tag handler class "com.mine.tags.Hello" for tag "sample:hello" through i have included that package in export package like

<Export-Package>com.mine.tags.Hello</Export-Package>
<Import-Package>*;resolution:=optional</Import-Package>

I also tied it like  <Export-Package>com.mine.*</Export-Package> But getting the same error. Is there anything i missed it in export package section?

Thanks

Avatar

Correct answer by
Level 7

Do you get any other errors?
This is an example how  you can set it up (note that this is just a simple test for a function but works the same way for tags):

1) In the tld file:

<?xml version="1.0" encoding="ISO-8859-1" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>My tag library</description> <tlib-version>1.0</tlib-version> <short-name>mytags</short-name> <uri>http://my.company.com/taglibs/mytags/1.0</uri> <function> <name>getSomeText</name> <function-class>com.mycompany.common.MyTest</function-class> <function-signature>String getSomeText(java.lang.String)</function-signature> </function> </taglib>

2. In the java file for the tag (called MyTest.java in the package com.mycompany.common):

public class MyTest { public static String getSomeText(String name) { return "Hi " + name + ", here is some text for you"; } }

3. Then exported the classes (in the pom) as you did with:
 

//... <Export-Package>com.mycompany.common.*</Export-Package> //..

4. Then in the .JSP file

//.. code <%@taglib prefix="mycompany" uri="http://my.company.com/taglibs/mytags/1.0" %> <div class="test-div"> ${mycompany:getSomeText("Johan")} </div> //... more code


Hope you get the idea.
/Johan

Avatar

Former Community Member

Hi,

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-Activator>com.cqblueprints.example.taglib.osgi.Activator</Bundle-Activator>
            <Export-Package>com.cb.*</Export-Package>
            <Import-Package>*;resolution:=optional</Import-Package>
            <Include-Resource>META-INF/tags.tld=src/main/resources/META-INF/tags.tld;{maven-resources}</Include-Resource>
            <Sling-Bundle-Resources>/META-INF/</Sling-Bundle-Resources>
        </instructions>
    </configuration>
</plugin>

I have tried with <resource> element and also tried as above as you have mentioned. But it still gives the same error as

org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.jsp.jasper.JasperException: /apps/cb/components/content/test/test.jsp(4,0) Unable to load tag handler class "com.cb.Hello" for tag "mytest3:hello"

Here is my maven folder strusture

bundle -> src -> main -> java -> com-> cb -> Hello.java
bundle -> src -> main -> resources -> tags.tld

and here is my jar file structure

cb-bundle-1.0-SNAPSHOT -> META-INF -> tags.tld
cb-bundle-1.0-SNAPSHOT -> OSGI-INF -> .....
cb-bundle-1.0-SNAPSHOT -> com.cb -> Hello.class

Is there anything i am missing here?

 

Thanks

Avatar

Former Community Member

Hi, There was some problem with package name. Now Sling can read my tag handler class after i renamed the package name.

The error "Unable to load tag handler class" also has gone.

Now i am getting error as "org.apache.sling.api.scripting.ScriptEvaluationException: javax.servlet.ServletException: javax.servlet.jsp.JspException: com.testcb.TestCustomTag cannot be cast to javax.servlet.jsp.tagext.Tag"

I have the following dependency in pom.xml

<dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
</dependency>

And Here is my tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
    <description>My tag library123</description>
    <tlib-version>1.0</tlib-version>
    <short-name>TagLib-Test</short-name>
    <uri>http://cs.test.com/bundles/cq/1.0</uri>
    <jspversion>2.1</jspversion>
    <tag>
      <name>testcustomtag</name>
      <tagclass>com.testcb.TestCustomTag</tagclass>
      <bodycontent>empty</bodycontent>
      <info>This is a simple hello tag</info>
    <attribute>
      <name>name</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    </tag>
</taglib>

Is there any problem with jsp version?

Thanks

Avatar

Former Community Member

Sorry, There was some conflict between these two threads.

Yes, It is working as expected after removed <Embed-Dependency> tag. :)

Thanks a lot for your interest!

Avatar

Employee

Hi,

First off, I find it very confusing that you are posting to two separate topics on the same issue. If your issue is still persisting, I'd recommend starting a new thread and only use a single thread.

The issue you are describing is caused by embedding the JSP API. I wrote this yesterday in the other thread:

"In addition, you should not be embedding the JSP API. This will cause other issues.  In fact, it looks like 100% of the configuration you have of the maven-bundle-plugin is either unnecessary or causing issues."

Regards,

Justin