Java-based macros give you the most power to create your own Confluence macros that can produce complex HTML, retrieve data from Confluence, manipulate pages and interact with other systems and resources.
Adding a Java macroTo add a Java macro library, drop the macro library JAR (called by convention macros-name.jar - ie macros-tasklist.jar) into the WEB-INF/lib directory within your Confluence web application and restart Confluence. Structure of a Java macro libraryA Confluence macro library is a JAR file containing classes and resources (ie Velocity templates) used by the macros in the library, as well as a macro library descriptor file macro-library.xml. This descriptor defines and documents the macro library, like the example below: <macro-libraries> <macro-library name="Task List Macros" key="confluence.extra.tasklist" description="An example macro library which generates task lists."> <macro name="tasklist" class="com.atlassian.confluence.extra.tasklist.TaskListMacro" description="Creates a very simple task list, with user checkable tasks."/> <!-- more macros --> </macro-library> <!-- more macro libraries --> </macro-libraries> A descriptor can contain multiple library definitions, each with multiple macros. Building Java macrosThe Confluence installation contains a custommacros directory with a "Confluence Macro Builder" Ant script and some sample macro projects. Task List Example LibraryAs an example, tasklist is one of the example macros, which builds a simple task list within a Confluence page and allows users to complete and uncomplete the various tasks simply. It also serves as a good example of how easy it is to build add functionality to Confluence. ExampleHere's a screenshot of the tasklist macro in action, showing a simple shopping list that's half completed: Unable to render embedded object: File (tasklist-screenshot.gif) not found. This was generated by notation like the following, with tasks completed or uncompleted by clicking on the relevant
{tasklist:Shopping List}
Buy apples
Buy bananas
Purchase shopping bag
Collect laundry
Deposit money
{tasklist}
You can download a prebuilt tasklist library or build it yourself as below. Building the tasklist libraryTo build tasklist, go to the custommacros directory within the expanded distribution, and run: ant -Dlibrary=tasklist build Your macro library is now located at custommacros/tasklist/dist/macros-tasklist.jar Installing the tasklist libraryTo install the library straight into the web application, run: ant -Dlibrary=tasklist install and then restart your Confluence instance. Similarly you can uninstall the library like so: ant -Dlibrary=tasklist uninstall Note: Confluence must first be shutdown before you can uninstall macros. |

Comments (7)
Jun 11, 2004
Mike Aizatsky says:
How can I put static resource to a macros? Our macros shows some custom images. ...How can I put static resource to a macros? Our macros shows some custom images. Where should I locate them and how should I reference that images in the generated code?
Jun 14, 2004
Mike Cannon-Brookes says:
Mike, Good question - for static resources your macros need to read (ie propert...Mike,
Good question - for static resources your macros need to read (ie properties / XML files or Velocity templates etc), just bundle them in the JAR and load them via the classpath.
For images, I think you'd have to put them into the WAR itself at the moment and then reference them as $req.contextPath/images/mymacrolibrary/image.gif or something similar.
It would be useful to have an ability to serve images from the classpath I suppose via some sort of servlet, we don't have that yet but I can see it would be very neat now. We'll add that this issue. The big issue there would be security of course, we wouldn't want to allow the web interface to serve up generic resources from the classpath!
Would it be OK if you had to alias all required resources in your XML file and then use those aliases in your reference? The servlet would then only serve resources that it had aliases for. It could be a pain if you had a lot of images I suppose - hrm... ideas?
Cheers,
Mike
Jun 14, 2004
Mike Cannon-Brookes says:
You can find more macro libraries in the [Custom Macros] space. Cheers...You can find more macro libraries in the [Custom Macros] space.
Cheers,
Mike
Jun 14, 2004
Mike Aizatsky says:
> Would it be OK if you had to alias all required resources in your XML file ...> Would it be OK if you had to alias all required resources in your XML file and then use those aliases in your reference?
Shouldn't be a problem.
Jun 14, 2004
Mike Aizatsky says:
>You can find more macro libraries in the [Custom Macros] space. Ther...>You can find more macro libraries in the [Custom Macros] space.
There seem to be no such space.
Jun 14, 2004
Mike Cannon-Brookes says:
Apologies - I hadn't set up the permissions for the new space, it should now be ...Apologies - I hadn't set up the permissions for the new space, it should now be [accessible]
Dec 10, 2004
Sulka Haro says:
All the links above are still broken and I can't find documentation for creating...All the links above are still broken and I can't find documentation for creating the custom Java macros. I know for example that 1.3 added the support for putting images into the JAR but is there documentation for this other than the JIRA issue?
Add Comment