Applicable: | This tutorial applies to Confluence 4.3 |
Level of experience: | This is an intermediate tutorial. You should have completed at least one beginner tutorial before working through this tutorial. See the list of developer tutorials . |
Time estimate: | It should take you approximately 45 minutes to complete this tutorial. |
Status | LEGACY This tutorial applies to Confluence versions that have reached end of life. |
From Confluence 7.0 com.atlassian.mail.queue.MailQueueItem
no longer extends com.atlassian.core.task.Task
which will lead to compile errors around task managers used in email notifications.
You will need to wrap the MailQueueItem
in a lambda before passing it down to the task manager, for example:
1 2taskManager.addTask(name, () -> item.send())
See Preparing for Confluence 7.0 for details of all breaking changes in 7.0.
This tutorial shows you how to write a service within a plugin in Confluence, to send an email message. Your completed plugin will consist of the following components:
When you have finished, all these components will be packaged in a single JAR file.
To complete this tutorial, you need to know the following:
We encourage you to work through this tutorial. If you want to skip ahead or check your work when you have finished, you can find the plugin source code on Atlassian Bitbucket. Bitbucket serves a public Git repository containing the tutorial's code. To clone the repository, issue the following command:
1 2git clone https://bitbucket.org/atlassian_tutorial/confluence-email-demo
Alternatively, you can download the source using the get source option here: bitbucket.org/atlassian_tutorial/confluence-email-demo
About these Instructions
You can use any supported combination of OS and IDE to construct this plugin. These instructions were written using Eclipse on a MacPro running Mac OS X. If you are using another combination, you should use the equivalent operations for your specific environment.
In this step, you'll use the two atlas-
commands to generate stub code for your plugin and set up the stub code as an Eclipse project. The atlas-
commands are part of the Atlassian Plugin SDK, and automate much of the work of plugin development for you.
Open a terminal and navigate to your Eclipse workspace directory.
Enter the following command to create a plugin skeleton:
1 2atlas-create-confluence-plugin
When prompted, enter the following information to identify your plugin:
group-id |
|
artifact-id |
|
version |
|
package |
|
Confirm your entries when prompted.
Change to the example-mail-sending-plugin
directory created by the previous step.
Run the following command:
1 2atlas-mvn eclipse:eclipse
Start Eclipse.
Select File > Import.
Eclipse starts the Import wizard.
Filter for Existing Projects into Workspace (or expand the General folder tree).
Choose Next and browse to the root directory of your plugin (where the pom.xml
file is located).
Your Atlassian plugin folder should appear under Projects.
Select your plugin and choose Finish.
Eclipse imports your project.
It is a good idea to familiarise yourself with the project configuration file, known as the POM (Project Object Model definition file). In this section, you will review and tweak the pom.xml
file. Open your plugin project in Eclipse and follow along in the next sections.
The POM (Project Object Model definition file).is located at the root of your project and declares the project dependencies and other information.
Add some metadata about your plugin and your company or organisation.
Edit the pom.xml
file in the root folder of your plugin.
Add your company or organisation name and your website to the <organization>
element (the following code blocks show how it looks in plain text):
1 2<organization> <name>Example Company</name> <url>http://www.example.com/</url> </organization>
Update the <description>
element:
1 2<description>This plugin provides a simple example of how to write a service that will send email.</description>
Save the file.
When you generated the stub files, a default Confluence version was included in your pom.xml
file. Take a moment and examine the Confluence dependency:
pom.xml
file.<properties>
element.atlas-
commands (AMPS commands) you are running.confluence.version
and confluence.data.version
to the correct versions. If you are not sure which version of Confluence you need, check the version table at the top of this tutorial.pom.xml
fileYour stub code contains a plugin descriptor file atlassian-plugin.xml
. This is an XML file that identifies the plugin to the host application (Confluence) and defines the required plugin functionality. In your IDE (integrated development environment, such as Eclipse or IDEA) open the descriptor file which is located in your project under src/main/resources
. You should see something like this:
1 2<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.artifactId}" plugins-version="2"> <plugin-info> <description>${project.description}</description> <version>${project.version}</version> <vendor name="${project.organization.name}" url="${project.organization.url}" /> </plugin-info> </atlassian-plugin>
Now you will use the plugin module generator (another atlas-
command) to generate the stub code for modules needed by the plugin.
For this tutorial, you will need a 'Component' plugin module. You'll add this via the atlas-create-confluence-plugin-module
command.
Open a command window and go to the plugin root folder (where the pom.xml
is located).
Run atlas-create-confluence-plugin-module
.
Choose the number corresponding to the option labelled Component
.
Supply the following information as prompted:
Enter Interface Name | MailService |
Package Name | com.example.plugins.tutorial.confluence.mail |
Enter Class Name | MailServiceImpl |
Enter Package Name | com.example.plugins.tutorial.confluence.mail |
Choose N
for Show Advanced Setup.
Choose N
for Add Another Plugin Module.
Return to Eclipse and Refresh your project.
Review the components added by the plugin module generator to your plugin descriptor.
| The |
You can also safely remove the xhtml-macro
plugin module descriptor from the atlassian-plugin.xml
since this is a boiler plate example macro that isn't needed by this tutorial.
If you change your Atlassian project, Eclipse is not automatically aware of the changes. Moreover, sometimes your project dependencies require an update. We need to fix that.
Switch to a terminal window.
Change directory to the project root.
This is the directory that contains the pom.xml
file.
Update your project metadata with the new POM information.
1 2atlas-mvn eclipse:eclipse
Back in Eclipse, refresh the plugin project to pick up the changes.
Remember to do this update and refresh step each time you edit your pom.xml
and whenever you modify your plugin source with an Atlassian command.
You have already generated the stub for your Component plugin module. Now, you will write some code that will make your plugin do something. Recall that this plugin will allow you to send an email message from within Confluence. To do this, you will implement the MailService interface, and hook this up to the TaskManger within Confluence.
To see the auto-generated Java code, right-click the com.example.plugins.tutorial.confluence.mail
package name, and choose 'Refresh'.
This is the MailService interface that we are going to implement. You will need a new method sendEmail(MailQueueItem mailQueueItem).
Add this method to your currently blank interface:
MailService.java
1 2package com.example.plugins.tutorial.confluence.mail; import com.atlassian.mail.queue.MailQueueItem; /** * This service has the responsibility of sending an email */ public interface MailService { /** * This will send an email based on the details stored in the ConfluenceMailQueueItem * * @param mailQueueItem the item to send */ void sendEmail(MailQueueItem mailQueueItem); }
You will now need to implement this method in the MailServiceImpl class, which was also auto-generated for you earlier. Our implementation will make use of the com.atlassian.core.task.MultiQueueTaskManager
that Confluence uses for task management.
Here is an example implementation of this class.
MailServiceImpl.java
1 2package com.example.plugins.tutorial.confluence.mail; import com.atlassian.core.task.MultiQueueTaskManager; import com.atlassian.mail.queue.MailQueueItem; /** * Default implementation of the {@link MailService} */ public class MailServiceImpl implements MailService { public static final String MAIL = "mail"; private final MultiQueueTaskManager taskManager; public MailServiceImpl(MultiQueueTaskManager taskManager) { this.taskManager = taskManager; } /** * This will use a MultiQueueTaskManager to add add the mailQueueItem to a queue * to be sent * * @param mailQueueItem the item to send */ @Override public void sendEmail(MailQueueItem mailQueueItem) { taskManager.addTask(MAIL, mailQueueItem); } }
Note that this class is pretty simple, but it makes use of a TaskManager. This task manager will need to be imported into the plugin via a component-import
module descriptor, so lets do that now.
As before, you will use the plugin module generator to generate the stub code for modules needed by the plugin.
You will need a Component Import plugin module. You'll add this via the atlas-create-confluence-plugin-module
command.
Open a command window and go to the plugin root folder (where the pom.xml
is located).
Run atlas-create-confluence-plugin-module
.
Choose the option labelled Component Import
.
Supply the following information as prompted:
Enter Fully Qualified Interface | com.atlassian.core.task.MultiQueueTaskManager |
Module Key | task-manager |
Filter (not required) | press enter |
Enter N
for Add Another Plugin Module.
Return to Eclipse and Refresh your project.
Review the components added by the plugin module generator.
| The |
This is effectively it, you now have a basic service in place that can be used to send email within Confluence, but how do you use it? Lets put together a quick unit test that covers how to actually use the MailService.
There should already be a MailServiceImplTest class present in the src/test/java
directory. (Right-click the package name and choose 'Refresh' to see it.) Modify this test class to look like the following:
1 2package com.example.plugins.tutorial.confluence.mail; import com.atlassian.confluence.mail.template.ConfluenceMailQueueItem; import com.atlassian.core.task.MultiQueueTaskManager; import com.atlassian.mail.queue.MailQueueItem; import com.google.common.collect.Lists; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import javax.activation.DataSource; import javax.mail.util.ByteArrayDataSource; import static com.atlassian.confluence.mail.template.ConfluenceMailQueueItem.MIME_TYPE_HTML; import static com.atlassian.confluence.mail.template.ConfluenceMailQueueItem.MIME_TYPE_TEXT; import static org.mockito.Mockito.verify; @RunWith(MockitoJUnitRunner.class) public class MailServiceImplTest { private MailService mailService; @Mock private MultiQueueTaskManager taskManager; @Before public void setUp() { mailService = new MailServiceImpl(taskManager); } @Test public void testSendEmail() throws Exception { MailQueueItem mailQueueItem = new ConfluenceMailQueueItem("whoever@atlassian.com", "A test email", "The body of the message", MIME_TYPE_HTML); mailService.sendEmail(mailQueueItem); verify(taskManager).addTask(MailServiceImpl.MAIL, mailQueueItem); } }
This test class uses Mockito to mock our call to the task manager and it will simply verify that our task manager is called once.
As you can see, we simply construct a ConfluenceMailQueueItem providing all the required attributes for the email, and then pass this into our service. If you take a look at the source code for this tutorial from Bitbucket you will see that I have included a secondary example of how you can add attachments to an email as well.
We are not writing any integration tests for this tutorial, so you will need to remove the functional test class automatically generated by the SDK.
MailServiceImplFuncTest.java
, which you will find in location src/test/java/it/com/example/plugins/tutorial/confluence/mail/
ExampleMacro.java
class from your project, since this is a boiler plate example macro that isn't needed by this tutorialFollow these steps to build and install your plugin, so that you can test your code.
Make sure you have saved all your code changes to this point.
Open a terminal window and navigate to the plugin root folder (where the pom.xml
file is).
Run the following command:
1 2atlas-run
This command builds your plugin code, starts a Confluence instance, and installs your plugin in it. This may take several seconds. When the process has finished, you will see many status lines on your screen concluding with something like the following:
1 2[INFO] HOSTAPP started successfully in 71s at http://localhost:XXXX/confluence [INFO] Type CTRL-D to shutdown gracefully [INFO] Type CTRL-C to exit
Open your browser and navigate to the local Confluence instance started by atlas-run
: By default, this is at http://localhost:1990/confluence
.
At the Confluence login screen, enter a username of admin
and a password of admin
.
Navigate to the Universal Plugin Manager in the Administration section of Confluence, by choosing Browse > Confluence Admin > Plugins.
Choose Show System Plugins.
Here you will see that the example-mail-sending-plugin plugin was successfully installed.
Now you have a basic plugin that can provide a way for you to send email messages. You could now think about creating a Struts Action, Servlet, in fact any feature that can make use of this mailing service!
Congratulations, that's it
Have a chocolate!
Rate this page: