JIRA PDF View Plugin: ScriptingScriptingPrerequisitesPlease study the basics of template development for the JIRA PDF View Plugin first, otherwise this tutorial will be more difficult to follow. Why scripting?The JIRA PDF View Plugin relies on Velocity as its primary language to define PDF document templates. In actual templates, Velocity logic statements are mixed with FO formatting tags to get the final result. Although Velocity is powerful at what it does, it is still purely a template language, not a full-blown programming language. That may impose serious limitations if you want to implement more complex PDF documents. In many cases, this limitation is not a real problem as your documents do not require any complex logic. You just want to display some fields, do some formatting, make trivial if-then switches. To implement these, using Velocity alone is sufficient, thus you should not complicate your life with scripting. In other situations, the requirements for the logic that needs to be built into your templates are more complex than simple if-then statements or iterating over a collection of objects. To implement these, Velocity alone will not be enough, you will need to do some scripting. Don't worry, scripting is easy and it opens new horizons for your PDF documents!
When to use scripting?Some use case examples that you can implement only with scripting (not in Velocity alone):
What is Groovy?Groovy is the primary scripting language supported by the JIRA PDF View Plugin. Groovy is agile scripting language for the Java platform, the same platform on which JIRA runs. What are the advantages of Groovy, compared to other scripting languages?
How can I learn Groovy?The basics of Groovy can be learnt literally in hours, assuming that you have some background in modern programming languages, like Java, Javascript or C++. Useful resources:
Writing Groovy scriptsYour first script in 2 minutesHere is the good old Hello world example, implemented with Groovy for the JIRA PDF View Plugin.
First, save your logic to a Groovy script file hello-world.groovy:
Then, execute it in your template hello-world-fo.vm:
Tadaam. That's it. Now you have a block in the PDF, containing the text generated by the Groovy code. Tip: it is usually a good idea to follow the naming convention used above. If your template is an implementation of "my document type", then save the template code to my-document-type-fo.vm and the script to my-document-type.groovy. It helps to see what files belong together. Passing objects from templates to scriptsBesides the basics of script execution, it is important to learn how to share and pass objects between template codes and scripts. Scripts have a dead-simple way to access the Velocity context objects used in templates. There is an important aspect of script execution, which is albeit transparent, yet important to understand. Before execution, each script is converted to an actual Groovy class in the background. Our script hello-world.groovy will be converted to the class named hello-world, for instance. And then, the Velocity context objects will available as properties of that class. Because the generated class is the "outermost" class in the script, its properties appear like "global variables" (more on this later). Simple, right?
Here is an example. You probably know that the currently signed in JIRA user is available as $user in the template code. Also, this is available as the object user in Groovy!
Let's greet him:
You can also pass arguments to the Groovy methods with ease:
Final note: although from the above code it may feel like as if we had a global variable "user", this is not true. In fact, there is no such thing like "global" in Groovy! Read this article to avoid surprises. Passing objects from scripts to templatesThe recipe is simple: all so-called "binding variables" instantiated in Groovy will be automatically available in the templates.
What is a binding variable? When it is not defined, it is in the binding, thus it will be available in templates, too.
Therefore, we recommend capturing your logic in lightweight classes, instantiating them as binding variables in Groovy and calling their methods in the template code. You may also want to follow the naming convention of calling your tool classes SomethingTool and instantiating them called something, just as we did in the examples. Good practices
Advanced examplesViewing the example templates shipped in the plugin distribution is a great way to study implementations of real life document types. (You can find the related files in the classes/templates/plugins/pdfview directory.)Gantt chartsA Groovy script is used to compute the start- and end date of tasks, to calculate the spanned period and the geometry of the bars. Based on that, the template renders an appropriately sized table, where the cells simulate the sections of the task bars. Files: gantt-chart-fo.vm, gantt-chart.groovy Traceability matrixesA simple Groovy script collects the dependencies defined by issue links into an efficient data structure. It uses a Table object (pairs of issues are mapped to their dependency) from the Google Guava library, so it also demonstrates using a 3rd party library in scripts. Then, the template iterates over the issues and renders a table by putting the dependency direction images to the cells. Files: traceability-matrix-fo.vm, traceability-matrix.groovy Recommended tools for scriptingIf you are a purist (or don't have the time), you can actually do all your work with a simple text editor! If you would like to enjoy syntax highlighting, code-completion and so, we recommend to use the Eclipse IDE (a great general purpose development environment) combined with the following plugins:
Questions?Ask us any time. |
JIRA PDF View PluginOverview Template Directory Plugin at Atlassian MarketplaceDocumentationInstallation Version History (Upgrade Guides) FAQConfiguration & CustomizationConfiguring the Plugin & Customizing Templates Scripting Recipes
Troubleshooting
TutorialsCharts Embedding Attachments |
