URCap - Importing third-party JAR file
Guide on how to import a third-party jar-file
These following examples demonstrate how to import a third-party jar to your Maven project when working with a URCap. Be aware that there are two types of third-party jar (Library) that can be imported to your project:
- A jar that already exist in the Maven central repository:
- In this case you just want to declare it as a dependency in the pom.xml file like the example in the section "Import Third-Party jar - located in the Maven central repository" . There are two examples:
One where the needed library does not have any dependency on other libraries and one where the needed library does have a dependency on other libraries.
- In this case you just want to declare it as a dependency in the pom.xml file like the example in the section "Import Third-Party jar - located in the Maven central repository" . There are two examples:
- A jar that does not exist in the Maven central repository:
- This is not covered by the article.
WARNING: Please be aware that this example provided has been made on Linux or the URCaps Starter Package.
Import Third-Party jar - located in the Maven central repository - not including other dependencies.
If the jar-file already exists in the Maven central repository, it is easy to just declare it as dependency in the pom.xml file in your project. An example could be that we want to import the Google Gson library. This library is available on this link. When clicking on the link, the website for the maven central repository is shown for the specific jar; GSON Library.
Because the jar already exists in the Maven central repository, it is possible to copy the dependency text from the textfield and paste it in the pom.xml file in your URCap project. The following image shows the dependency declared in the pom.xml file. When look at this site, it appears that the library Gson does not have any dependency other than the JUnit. Since JUnit is already a part of the URCap project, we should not worry about it.
The dependency can easily be inserted into the pom.xml file of the project by copying the following:
It is then inserted in the correct place in the pom.xml as illustrated in below code:
//In the pom.xml file
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>com.google.code.gson</groupId>
|
|
<artifactId>gson</artifactId>
|
|
<version>2.8.5</version>
|
|
</dependency>
|
|
</dependencies>
|
When adding the dependency in pom.xml like the example above, it is possible to use the library in the code. The problem occurs when trying to deploy the URCap to the robot. The library is not included in the deployed jar file. To solve the problem, the jar of the library must be embedded inside the deployed jar of the URCap. This is done by include the following code in pom.xml:
//must be declared inside the "instructions" tag
|
|
<plugin>
|
|
<configuration>
|
|
<instructions>
|
|
<Embed-Dependency>gson</Embed-Dependency>
|
|
<Embed-Transitive>true</Embed-Transitive>
|
|
</instructions>
|
|
</configuration>
|
|
</plugin>
|
In the embed statement the artifactId of the dependency is added. When the dependency has been embedded and the project has been deployed. It should be possible to see the deployed jar inside the target folder of the project:
To confirm that the Gson library is included in the deployed jar, the jar is opened with the archive manager:
The gson-2.8.5.jar is included in the project, which concludes that the library has been successfully embedded inside the deployed jar of the URCap. To completely understand what embedding a jar means, it could be a good idea to take a look at the Manifest file which is found under the target/classes/MANIFEST.MF path of the project. The Manifest file should include something like this:
Import Third-Party jar - located in the Maven central repository - including other dependencies
When including a library, it might be the case that the library has a dependency on other libraries as well. An example on a library with a dependency on other library might looks like this:
The picture above shows that the library JDBC Translator library has 4 compile dependency. If this is the case, it is a good idea to include these as well as dependency and embed them in the URCap. If they are not included an exception might be generated in the command screen when starting PolyScope/URSim - like the following picture:
This exception is due to a missing dependency. It states in "osgi.wiring.package" that it is missing the "org.hibernate.boot" package which is located in the org.hibernate library shown in the above picture. If all the 4 libraries are inserted as dependency and embedded into the urcap and the exception is still shown. Try the following code:
// under instructions statement insert:
|
|
<Import-Package>
|
|
com.ur.urcap.api*;version="[1.5.0,2.0.0)",
|
|
*;resolution:=optional
|
|
</Import-Package>
|
|
//inserted the ";resolution:=optional" line.
|
The reason for adding this statement is to optionally import the needed packages. If a specific package is needed, then the package name can be specified in this section.