PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/content/server/framework/atlassian-sdk/create-a-confluence-hello-world-macro.md

https://bitbucket.org/zchristmas/atlassian-sdk-docs
Markdown | 393 lines | 293 code | 100 blank | 0 comment | 0 complexity | e1e26ab74ee5c69eebe3597b1df6f808 MD5 | raw file
Possible License(s): LGPL-2.0
  1. ---
  2. aliases:
  3. - /server/framework/atlassian-sdk/44044634.html
  4. - /server/framework/atlassian-sdk/44044634.md
  5. category: devguide
  6. confluence_id: 44044634
  7. dac_edit_link: https://developer.atlassian.com/pages/editpage.action?cjm=wozere&pageId=44044634
  8. dac_view_link: https://developer.atlassian.com/pages/viewpage.action?cjm=wozere&pageId=44044634
  9. date: '2017-12-08'
  10. guides: tutorials
  11. legacy_title: Create a Confluence 'Hello World' Macro
  12. platform: server
  13. product: atlassian-sdk
  14. subcategory: learning
  15. title: Create a Confluence 'Hello World' macro
  16. ---
  17. # Create a Confluence 'Hello World' macro
  18. This tutorial builds on the concepts introduced in [Set Up the Atlassian SDK and Build a Project](https://developer.atlassian.com/docs/getting-started/set-up-the-atlassian-plugin-sdk-and-build-a-project). You'll delve a little deeper into the SDK environment and so to complete this tutorial successfully, you should have already installed the SDK and created a plugin project. 
  19. ## About this tutorial
  20. <table>
  21. <colgroup>
  22. <col style="width: 20%" />
  23. <col style="width: 80%" />
  24. </colgroup>
  25. <tbody>
  26. <tr class="odd">
  27. <td>Pre requisites</td>
  28. <td><ul>
  29. <li>Atlassian Plugin SDK 6.2.x</li>
  30. <li>Oracle Java JDK 8</li>
  31. <li>It's a good idea to have completed <a href="https://developer.atlassian.com/docs/getting-started/set-up-the-atlassian-plugin-sdk-and-build-a-project">Set Up the Atlassian SDK and Build a Project</a></li>
  32. <li>A basic understanding of Java, javascript and css</li>
  33. </ul></td>
  34. </tr>
  35. <tr class="even">
  36. <td>Experience Level</td>
  37. <td>BEGINNER</td>
  38. </tr>
  39. <tr class="odd">
  40. <td>Recommended Environment</td>
  41. <td><ul>
  42. <li>Atlassian Plugin SDK 6.2.9</li>
  43. <li>AMPS Version 6.2.6</li>
  44. <li>Confluence Version</li>
  45. </ul></td>
  46. </tr>
  47. <tr class="even">
  48. <td>Source</td>
  49. <td><a href="https://bitbucket.org/serverecosystem/myconfluencemacro/src" class="uri external-link">https://bitbucket.org/serverecosystem/myconfluencemacro/src</a></td>
  50. </tr>
  51. </tbody>
  52. </table>
  53. ## Create the plugin skeleton
  54. Just like in the last tutorial, you'll use the Atlassian Plugin SDK to create a skeleton for our plugin.  This time you will use the `atlas-create-confluence-plugin` command because you're creating a plugin to work in Confluence.
  55. {{% tip %}}
  56. You can find a list of all the **atlas-** commands in the [Command reference](https://developer.atlassian.com/docs/developer-tools/working-with-the-sdk/command-reference)
  57. {{% /tip %}}
  58. 1. Open a terminal window and navigate to the directory where you'd like to create your plugin.
  59. 2. Run the command `atlas-create-confluence-plugin` and fill in the plugin details as follows:
  60. ``` bash
  61. Define value for groupId: : com.atlassian.tutorial
  62. Define value for artifactId: : myConfluenceMacro
  63. Define value for version: 1.0.0-SNAPSHOT: :
  64. Define value for package: com.atlassian.tutorial: :
  65. Confirm properties configuration:
  66. groupId: com.atlassian.tutorial
  67. artifactId: myConfluenceMacro
  68. version: 1.0.0-SNAPSHOT
  69. package: com.atlassian.tutorial
  70. Y: : Y
  71. ```
  72. 3. The plugin skeleton will be created automatically and you'll receive a confirmation message as follows:
  73. ``` bash
  74. [INFO] ------------------------------------------------------------------------
  75. [INFO] BUILD SUCCESS
  76. [INFO] ------------------------------------------------------------------------
  77. [INFO] Total time: 02:53 min
  78. [INFO] Finished at: 2016-10-10T15:39:56+10:00
  79. [INFO] Final Memory: 16M/309M
  80. [INFO] ------------------------------------------------------------------------
  81. ```
  82. 4. Now, navigate to the **myConfluenceMacro** directory that was created by the Atlassian Plugin SDK in the last step. 
  83. 5. Update the organization details for your plugin in the pom.xml file (see [modify the plugin - step 1](https://developer.atlassian.com/docs/getting-started/set-up-the-atlassian-plugin-sdk-and-build-a-project/modify-the-plugin#ModifythePlugin-Step1.UpdatetheorganizationdetailsthatappearonthePlugin) from the [Set up the Atlassian Plugin SDK and Build a Project tutorial](https://developer.atlassian.com/docs/getting-started/set-up-the-atlassian-plugin-sdk-and-build-a-project) if you're stuck).
  84. 6. Start up confluence with your plugin installed (and download all the necessary files to do that) by running the command `atlas-run.`
  85. 7. Check that Confluence started up with your plugin installed by navigating to <a href="http://localhost:1990/confluence/plugins/servlet/upm/manage/all" class="uri external-link">localhost:1990/confluence/plugins/servlet/upm/manage/all</a> (you can login using **username: admin, password: admin** just like you did in the last tutorial).
  86. 8. Leave Confluence up and running now
  87. ## Create a Macro Element in Confluence
  88. In the previous tutorial, we used the `atlas-create-jira-plugin-module` command to create a JIRA menu module which we then customised.  This command essentially modified the** /src/main/resources/atlassian-plugin.xml** file in your plugin directory to create some extra elements in JIRA.  
  89. In this tutorial, you will create the plugin elements manually rather than using an SDK command.  This is because `atlas-create-confluence-plugin-module` command doesn't have an option for the Module type you need to use.
  90. 1. Open the **atlassian-plugin.xml **file in your favourite editor.
  91. 2. Locate the end of the &lt;web-resource&gt;...&lt;/web-resource&gt; section in the file and enter the following:
  92. ``` xml
  93. <xhtml-macro name="helloworld" class="com.atlassian.tutorial.macro.helloworld" key='helloworld-macro'>
  94. <description key="helloworld.macro.desc"/>
  95. <category name="formatting"/>
  96. <parameters/>
  97. </xhtml-macro>
  98. ```
  99. save the changes to your file.  Note: your macro name and macro key should both be lower case. 
  100. 3. Open the file **/src/main/resources/myConfluenceMacro.properties** and add the following line at the bottom of the file:
  101. ``` bash
  102. helloworld.macro.desc="Hello World"
  103. ```
  104. save the changes to you file. 
  105. 4. Next you need to create the java class that you made reference to in Step 2. Because you'll be creating a couple of macros in this tutorial, you can create a **macro **folder to keep things tidy.
  106. 5. Open a new terminal window so you can create a new folder in the **/src/main/java/com/atlassian/tutorial **directory called **macro**:
  107. ``` bash
  108. cd /src/main/java/com/atlassian/tutorial
  109. mkdir macro
  110. cd macro
  111. ```
  112. 6. In the folder, create a file called **helloworld.java** and open it in your favourite editor.
  113. 7. Enter the following code into **helloworld.java**:
  114. ``` java
  115. package com.atlassian.tutorial.macro;
  116. import com.atlassian.confluence.content.render.xhtml.ConversionContext;
  117. import com.atlassian.confluence.macro.Macro;
  118. import com.atlassian.confluence.macro.MacroExecutionException;
  119. import java.util.Map;
  120. public class helloworld implements Macro {
  121. public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {
  122. return "<h1>Hello World</h1>";
  123. }
  124. public BodyType getBodyType() { return BodyType.NONE; }
  125. public OutputType getOutputType() { return OutputType.BLOCK; }
  126. }
  127. ```
  128. This is the minimum skeleton your Macro will require to implement the [confluence Macro class](https://developer.atlassian.com/static/javadoc/confluence/4.0/reference/com/atlassian/confluence/macro/Macro.html) and display a Macro object in Confluence.  
  129. 8. In your terminal window, change directory back to the top directory for your plugin (eg `cd <home>/AtlassianTutorial/myConfluenceMacro`)
  130. 9. Run the command `atlas-mvn package` to re-package your add-on and reinstall it using QuickReload. You should see a confirmation message:
  131. ``` bash
  132. [INFO] BUILD SUCCESS
  133. [INFO] ------------------------------------------------------------------------
  134. [INFO] Total time: 4.656 s
  135. [INFO] Finished at: 2016-10-10T18:33:09+10:00
  136. [INFO] Final Memory: 37M/433M
  137. [INFO] ------------------------------------------------------------------------
  138. ```
  139. 10. Monitor the window where confluence was run originally and confirm that QuickReload finished loading. You should see a confirmation message:
  140. ``` bash
  141. [INFO] [talledLocalContainer] 2016-10-10 18:33:16,082 INFO [QuickReload - Plugin Installer] [atlassian.plugin.manager.DefaultPluginManager] updatePlugin Updating plugin 'com.atlassian.tutorial.myConfluenceMacro-tests' from version '1.0.0-SNAPSHOT' to version '1.0.0-SNAPSHOT'
  142. [INFO] [talledLocalContainer] 2016-10-10 18:33:16,083 INFO [QuickReload - Plugin Installer] [atlassian.plugin.manager.DefaultPluginManager] broadcastPluginDisabling Disabling com.atlassian.tutorial.myConfluenceMacro-tests
  143. [INFO] [talledLocalContainer] 2016-10-10 18:33:17,512 INFO [QuickReload - Plugin Installer] [plugins.quickreload.install.PluginInstallerMechanic] installPluginImmediately
  144. [INFO] [talledLocalContainer] ^
  145. [INFO] [talledLocalContainer] |
  146. [INFO] [talledLocalContainer] |
  147. [INFO] [talledLocalContainer] |
  148. [INFO] [talledLocalContainer] |
  149. [INFO] [talledLocalContainer] |
  150. [INFO] [talledLocalContainer]
  151. [INFO] [talledLocalContainer] A watched plugin never boils!
  152. [INFO] [talledLocalContainer]
  153. [INFO] [talledLocalContainer] Quick Reload Finished (5954 ms) - 'myConfluenceMacro-1.0.0-SNAPSHOT-tests.jar'
  154. ```
  155. 11. Now you can try adding the Macro to a test page in Confluence (you'll need to make a new Confluence Space and Page before you can test it out so go ahead and do that first).
  156. {{% note %}}
  157. Note
  158. Note: It can take the Confluence macro browser a little bit of time to realise that there's a new macro available, so if it doesn't show up right away give it a little while and try again.  
  159. {{% /note %}}
  160. <img src="/server/framework/atlassian-sdk/images/confluence-macro-browser-showing-helloworld-macro.png" title="Macro Browser" alt="Confluence macro browser showing helloworld macro" width="500" height="305" />
  161. <img src="/server/framework/atlassian-sdk/images/helloworld-macro-on-page-in-edit-view.png" title="Helloworld Macro - Edit Mode" alt="Helloworld macro showing on confluence page in edit mode" width="500" height="268" />
  162. <img src="/server/framework/atlassian-sdk/images/helloworld-macro-showing-message-after-saving-page.png" title="Helloworld Macro - Confluence Page" alt="Helloworld Macro shown on Confluence page that has been saved" width="880" height="250" />
  163. ## Customize the Hello World macro
  164. Now, you will allow the user to specify their name using a parameter to learn about how parameters can be set, and used.
  165. 1. Open the **atlassian-plugin.xml **file in your favourite editor.
  166. 2. Locate the `<parameters/>` element within the` <xhtml-macro>` element you created in the first part of this tutorial.  
  167. 3. Replace the `<parameters/>` element with the following:
  168. ``` xml
  169. <parameters>
  170. <parameter name="Name" type="string" />
  171. </parameters>
  172. ```
  173. This specifies that the parameter is called 'Name' and is of type 'string'.  You can find the full list of types under the **Parameters** heading in the [macro module documentation](https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/macro-module/including-information-in-your-macro-for-the-macro-browser)
  174. 4. Save and close the changes you made to **atlassian-plugin.xml**
  175. 5. Open **helloworld.java **(it'll be in the /src/main/java/com/atlassian/tutorial/macro directory)
  176. 6. Modify the **execute **function as follows:
  177. ``` java
  178. public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {
  179. if (map.get("Name") != null) {
  180. return ("<h1>Hello " + map.get("Name") + "!</h1>");
  181. } else {
  182. return "<h1>Hello World!<h1>";
  183. }
  184. }
  185. ```
  186. 7. Save your changes to **helloworld.java**
  187. 8. In your terminal window, make sure you're in the top directory for your project (eg &lt;home&gt;/AtlassianTutorial/myConfluenceMacro) and run the command:
  188. ``` bash
  189. atlas-mvn package
  190. ```
  191. 9. Monitor the terminal window where Confluence is running to confirm that the plugin has been reloaded by QuickReload.  
  192. 10. Log in and check your changes are working:
  193. <img src="/server/framework/atlassian-sdk/images/updated-macro---macro-editor.png" width="462" height="250" />
  194. <img src="/server/framework/atlassian-sdk/images/updated-macro---confluence-editor.png" width="466" height="250" />
  195. <img src="/server/framework/atlassian-sdk/images/updated-macro---saved-macro-test-page.png" width="754" height="250" />
  196. ## Format the macro appearance using css
  197. At the moment, all the formatting work is being done in the **execute **function. In this section you'll set up a [Web Resource Plugin Module](/server/framework/atlassian-sdk/web-resource-plugin-module) to allow css to control the appearance of your macro.  
  198. 1. Open the **atlassian-plugin.xml **file in your favourite editor.
  199. 2. Notice that the &lt;`web-resource> `parameter already exists:
  200. ``` xml
  201. <web-resource key="myConfluenceMacro-resources" name="myConfluenceMacro Web Resources">
  202. <dependency>com.atlassian.auiplugin:ajs</dependency>
  203. <resource type="download" name="myConfluenceMacro.css" location="/css/myConfluenceMacro.css"/>
  204. <resource type="download" name="myConfluenceMacro.js" location="/js/myConfluenceMacro.js"/>
  205. <resource type="download" name="images/" location="/images"/>
  206. <context>myConfluenceMacro</context>
  207. </web-resource>
  208. ```
  209. In this tutorial you don't need to make any changes to the web-resource module itself. 
  210. 3. Below your `<parameter name="Name" type="string" />`, add a new `<parameter name="Color" type="enum">` as follows:
  211. ``` xml
  212. <parameter name="Color" type="enum">
  213. <value name="red"/>
  214. <value name="green"/>
  215. <value name="blue"/>
  216. </parameter>
  217. ```
  218. This will create a drop down menu in the macro editor for the user to select their color as either red, green or blue. 
  219. 4. Save your changes to atlassian-plugin.xml and close the file.
  220. 5. Next, open the **src/main/resources/css/myConfluenceMacro.css** file
  221. 6. Add the following css to the file:
  222. ``` css
  223. .blue h1 {
  224. color: blue;
  225. }
  226. .red h1 {
  227. color: red;
  228. }
  229. .green h1 {
  230. color: green;
  231. }
  232. ```
  233.  This css will change the text colour for a .blue, .green and .red h1 element (once you've updated your helloworld.java of course).
  234. 7. Save your changes to myConfluenceMacro.css and close the file. 
  235. 8. Now, open the **src/main/java/com/atlassian/tutorial/macro/helloworld.java** file. 
  236. 9. Modify the **execute **function to create &lt;div&gt; elements as follows:
  237. ``` java
  238. public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {
  239. String output = "<div class =\"helloworld\">";
  240. output = output + "<div class = \"" + map.get("Color") + "\">";
  241. if (map.get("Name") != null) {
  242. output = output + ("<h1>Hello " + map.get("Name") + "!</h1>");
  243. } else {
  244. output = output + "<h1>Hello World!<h1>";
  245. }
  246. output = output + "</div>" + "</div>";
  247. return output;
  248. }
  249. ```
  250. Notice that you are now creating a &lt;div .../&gt; element with a class matching the users nominated color.
  251. 10. Next, under the existing imports in your java file, add the following new lines:
  252. ``` java
  253. import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
  254. import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
  255. import com.atlassian.webresource.api.assembler.PageBuilderService;
  256. import org.springframework.beans.factory.annotation.Autowired;
  257. ```
  258. These are required so you can use <a href="https://bitbucket.org/atlassian/atlassian-spring-scanner" class="external-link">spring scanner</a>, along with the PageBuilderService to ensure the css you've created is being included in Confluence.  
  259. 11. Above the `public class helloworld implements Macro` definition, add the following Spring annotations:
  260. ``` java
  261. @Scanned
  262. ```
  263. The `@scanned` annotation is an instruction for <a href="https://bitbucket.org/atlassian/atlassian-spring-scanner" class="external-link">spring scanner</a>.  In this tutorial we used spring scanner 1.2.13 - so we needed to tell spring scanner to scan this class. 
  264. 12. Now, within the `helloworld` class definition, add a `PageBuilderService` variable, as well as a constructor for your `helloworld` class as follows:
  265. ``` java
  266. public class helloworld implements Macro {
  267. private PageBuilderService pageBuilderService;
  268. @Autowired
  269. public helloworld(@ComponentImport PageBuilderService pageBuilderService) {
  270. this.pageBuilderService = pageBuilderService;
  271. }
  272. ```
  273. The pageBuilderService is required for the [Web Resource Plugin Module](/server/framework/atlassian-sdk/web-resource-plugin-module)  
  274. 13. Finally, add the following line to your execute function:
  275. ``` java
  276. pageBuilderService.assembler().resources().requireWebResource("com.atlassian.tutorial.myConfluenceMacro:myConfluenceMacro-resources");
  277. ```
  278. Notice that you're using the pageBuilderService to require the web-resource key that was generated automatically (see step 2).
  279. 14. Save your changes and run `atlas-mvn package` to update your plugin.  
  280. 15. Make sure your plugin is working with the new changes:
  281. {{% note %}}
  282. Note
  283. You might need to hold down the shift key while reloading the page to see the changes to the page itself!
  284. {{% /note %}}
  285. <img src="/server/framework/atlassian-sdk/images/final-changes-with-css.png" width="502" height="250" />
  286. ## Need Help?
  287. The source code for this tutorial is available <a href="https://bitbucket.org/serverecosystem/myconfluencemacro/src" class="external-link">on Bitbucket</a>  
  288. Or, check out the questions on <a href="http://answers.atlassian.com/" class="external-link">Atlassian Answers</a> or create a request in our <a href="https://ecosystem.atlassian.net/servicedesk/customer/portal/14" class="external-link">Developer Technical Support Portal</a>.
  289. ## Resources
  290. - The source code for this tutorial is available <a href="https://bitbucket.org/serverecosystem/myconfluencemacro/src" class="external-link">on Bitbucket</a>  
  291. - You can find documentation for Spring Scanner at <a href="https://bitbucket.org/atlassian/atlassian-spring-scanner" class="uri external-link">bitbucket.org/atlassian/atlassian-spring-scanner</a>
  292. - Learn more about the [Web Resource Plugin Module](/server/framework/atlassian-sdk/web-resource-plugin-module)
  293. - [Learn more](https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/macro-module/including-information-in-your-macro-for-the-macro-browser) about the [Macro Module](https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/macro-module)