Confluence plugin does not import jtds driver
Asked by Tijmen Mulder:

Hi all,

I am writing a plugin to execute SQL commands and modify data from an external SQL server to show the user certain document data safed in SQL.

The plugin contains a servlet. JS sends a query query to a rest resource, which should execute the sql commands.

Everything works fine when I run it in intellij with a test class, but when uploading to confluence, or running the atlas-run command, the jtds driver does not seem to be imported, so I get a classnotfound error when trying to load the driver.

I have a dependency on jtds, but in my plugin osgi bundel, the jtds driver is not an import package.

pom.xml:

```
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.atlassian.plugins</groupId>
 <artifactId>Servlet</artifactId>
 <version>1.0.0-SNAPSHOT</version>
 <organization>
 <name>Example Company</name>
 <url>http://www.example.com/</url>
 </organization>
 <name>Servlet</name>
 <description>This is the com.atlassian.plugins:Servlet plugin for Atlassian Confluence.</description>
 <packaging>atlassian-plugin</packaging>
 <dependencies>
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.4</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.10</version>
 </dependency>
 <dependency>
 <groupId>com.atlassian.confluence</groupId>
 <artifactId>confluence</artifactId>
 <version>${confluence.version}</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>com.atlassian.sal</groupId>
 <artifactId>sal-api</artifactId>
 <version>3.0.6</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>com.atlassian.plugin</groupId>
 <artifactId>atlassian-spring-scanner-annotation</artifactId>
 <version>${atlassian.spring.scanner.version}</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>javax.inject</groupId>
 <artifactId>javax.inject</artifactId>
 <version>1</version>
 <scope>provided</scope>
 </dependency>
 <!-- WIRED TEST RUNNER DEPENDENCIES -->
 <dependency>
 <groupId>com.atlassian.plugins</groupId>
 <artifactId>atlassian-plugins-osgi-testrunner</artifactId>
 <version>${plugin.testrunner.version}</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>javax.ws.rs</groupId>
 <artifactId>javax.ws.rs-api</artifactId>
 <version>2.0</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>com.google.code.gson</groupId>
 <artifactId>gson</artifactId>
 <version>2.2.2-atlassian-1</version>
 </dependency>
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.6.6</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>org.apache.httpcomponents</groupId>
 <artifactId>httpclient</artifactId>
 <version>4.1.1</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>com.atlassian.plugins.rest</groupId>
 <artifactId>atlassian-rest-common</artifactId>
 <version>5.0.1</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>1.8.5</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>com.atlassian.templaterenderer</groupId>
 <artifactId>atlassian-template-renderer-api</artifactId>
 <version>2.0.0</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>javax.xml.bind</groupId>
 <artifactId>jaxb-api</artifactId>
 <version>2.1</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>com.microsoft.sqlserver</groupId>
 <artifactId>mssql-jdbc</artifactId>
 <version>6.1.0.jre8</version>
 <scope>provided</scope>
 </dependency>
 </dependencies>
 <build>
 <plugins>
 <plugin>
 <groupId>com.atlassian.maven.plugins</groupId>
 <artifactId>maven-confluence-plugin</artifactId>
 <version>${amps.version}</version>
 <extensions>true</extensions>
 <configuration>
 <productVersion>${confluence.version}</productVersion>
 <productDataVersion>${confluence.data.version}</productDataVersion>
 <enableQuickReload>true</enableQuickReload>
 <enableFastdev>false</enableFastdev>
 <!-- See here for an explanation of default instructions: -->
 <!-- https://developer.atlassian.com/docs/advanced-topics/configuration-of-instructions-in-atlassian-plugins -->
 <instructions>
 <Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>
 <Spring-Context>*</Spring-Context>
 <!-- Add package to export here -->
 <Export-Package>com.atlassian.plugins.api,</Export-Package>
 <!-- Add package import here -->
 <Import-Package>*</Import-Package>
 <!-- Ensure plugin is spring powered -->

 </instructions>
 </configuration>
 </plugin>
 <plugin>
 <groupId>com.atlassian.plugin</groupId>
 <artifactId>atlassian-spring-scanner-maven-plugin</artifactId>
 <version>${atlassian.spring.scanner.version}</version>
 <executions>
 <execution>
 <goals>
 <goal>atlassian-spring-scanner</goal>
 </goals>
 <!-- process-classes seems to be skipped if you are using scala
 so perhaps use prepare-package -->
 <phase>process-classes</phase>
 </execution>
 </executions>
 <configuration>
 <!-- Enable this to get build-time logging of annotations atlassian-spring-scanner-maven-plugin has noticed -->
 <verbose>false</verbose>
 </configuration>
 </plugin>
 </plugins>
 </build>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <confluence.version>6.0.1</confluence.version>
 <confluence.data.version>6.0.1</confluence.data.version>
 <amps.version>6.2.11</amps.version>
 <plugin.testrunner.version>1.2.3</plugin.testrunner.version>
 <atlassian.spring.scanner.version>2.1.3</atlassian.spring.scanner.version>
 <!-- This key is used to keep the consistency between the key in atlassian-plugin.xml and the key to generate bundle. -->
 <atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
 </properties>
</project>?
```

myRestResource.java:

```
?package com.atlassian.plugins.rest;

import com.atlassian.plugins.rest.MyRestResourceModel;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import com.microsoft.sqlserver.jdbc.*;

@Path("/message")
public class MyRestResource {
 public MyRestResource() {
 }

 @GET
 @Produces({"application/json", "application/xml"})

 public Response getMessage(@QueryParam("key") String key) {
 Connection conn = null;
 Object statement = null;
 Object resultSet = null;
 Object resultString = null;
 String result = null;


 try {
 Class.forName("net.sourceforge.jtds.jdbc.Driver");
 conn = DriverManager.getConnection("jdbc:jtds:sqlserver://myserver;databaseName=name;", "username",
 "password");
 } catch (Exception e) {
 e.printStackTrace();
 result=e.getMessage();
 }

 if(conn != null) {
 result = "Connected!";
 }

 return Response.ok(new MyRestResourceModel(key, result)).build();
 }
}
```

If anyone knows what I might be doing wrong, please don't hesitate to notify me!

Much thanks in advance!


Answers:
author: Tijmen Mulder
body: Hi Robert,

Thank you for your answer!

I tried to do as you suggested, but the plugin still fails with the error:

Caused by: org.osgi.framework.BundleException: Unresolved constraint in bundle com.atlassian.plugins.tutorial.admin-ui \[277\]: Unable to resolve 277.0: missing requirement \[277.0\] osgi.wiring.package; (osgi.wiring.package=jcifs)

When looking at the import-packages of the plugin in the osgi browser, there is a package for net.sourceforge.jtds.

I hope you can help, thanks in advance!


author: Robert Krause
body: Hi Tijmen,

you can try to add the package to the Import-Package Section in the maven-confluence-plugin section. This way it should be bundled in your OSGI-Package.

Best regards,

Robert


Tags: confluence-app

developer

java

ms-sql

osgi

rest-api

sql