How to add TLD and Tag Lib files into a Maven's jar project

14,091

Solution 1

The practice these days is to put the TLD files into the tag library JAR and let the class loader find them. Download the Apache JSTL JARs and see how they do it. I'd recommend following that convention. It'll simplify your app too, because you won't have to declare the TLD in your web.xml file: just put the JAR in your CLASSPATH and make sure that the URI in your .jsp matches that in the TLD.

Solution 2

@duffymo - Your solution totally works. Adding graphic to your description.

Create a maven project that generates JAR. keep the structure like this below

 src -- main
            |-- java
            |      `-- net
            |            `-- madhur
            |                 `-- helloTag.java
            `-- resources
                  `-- META-INF
                        `-- tags
                             `-- customTags.tld

To your customTags.tld file add uri something like this

<uri>http://www.xyzabc.com/taglibs/customTags</uri>

Accessing tags in you WAR file

War should have following structure

  META-INF/
  META-INF/MANIFEST.MF
  WEB-INF/
  WEB-INF/classes/
  WEB-INF/lib/
  WEB-INF/lib/{tagLibrary}.jar
  WEB-INF/web.xml
  WEB-INF/customTags.tld

web.xml

    <jsp-config>
        <taglib>
            <taglib-uri>www.xyzabc.com/taglibs/customTags</taglib-uri>
            <taglib-location>/WEB-INF/customTags.tld</taglib-location>
        </taglib>
    </jsp-config>

Using tag in FTL or JSP file

Ftl:

<#assign ct = JspTaglibs["www.xyzabc.com/taglibs/customTags"]>
Share:
14,091

Related videos on Youtube

bluefoot
Author by

bluefoot

Java developer trying to get back to SO as soon as possible.

Updated on June 04, 2020

Comments

  • bluefoot
    bluefoot almost 4 years

    I have a Maven project that is packaged as jar.

    And I also have a Maven project that is packaged as war. This war project has a tld file and some xhtml files (the tag libs). The structure of the war project is (basically):

    pom.xml
    src
        main
           java
               ...
           webapp
               WEB-INF
                   my-facelets.taglib.xml
                   facelets
                       aTag.xhtml
                       anotherTag.xhtml
               META-INF
                   my-facelets.taglib.tld
    

    And then appeared a requirement to remove those xml, xhtml and tld files from the war project and package them into the jar project. So my first try was add in the jar project's POM:

    <resources>
      <resource>
        <directory>src/main/tld</directory>
        <targetPath>META-INF</targetPath>
      </resource>
    
      <resource>
        <directory>src/main/taglib</directory>
        <targetPath>WEB-INF</targetPath>
      </resource>
    </resources> 
    

    And put the files, of course, into src/main/tld (the ones I wanted to export into META-INF) and src/main/taglib (the ones I wanted to export into WEB-INF). And the jar was created as I wish:

    myjar
        com
            my
                classes
        WEB-INF
    
        META-INF
            my-facelets.taglib.tld
        WEB-INF
            ...
    

    And then I added this new jar to my first war project, as a maven dependency.

    The problem is that those .tld, .xhtml, .xml files that are inside the jar's META-INF, WEB-INF (the jar is inside war's WEB-INF/lib) are not recognized. Apparently they should be directly into the war structure, unless some other configuration is performed. This is a must-have requirement, because multiple war projects will use the features (classes and taglibs) of the jar project.

  • bluefoot
    bluefoot about 13 years
    I was thinking too of downloading something an see how they do. I'll check it out and post the results.
  • bluefoot
    bluefoot about 13 years
    it works correctly. No need to declare anything into Manifests. Just inserting all the files inside jar's META-INF and letting maven build the war (with the jar inside it's WEB-INF/lib) did the trick. The error was the <targetPath> of my second <resource> element.

Related