Note: This tutorial assumes that you have completed the previous tutorials: Installation, Creating Rosjava Packages. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Building RosJava Libraries (Catkin Style)
Description: How to create, compile and deploy rosjava libraries (maven artifacts).Keywords: rosjava
Tutorial Level: BEGINNER
Next Tutorial: RosJava Message Artifacts
Contents
This tutorial describes how to create and deploy rosjava libraries (jars) along with an explanation of the mysterious process itself. It assumes that you have a ros-catkin environment from a source or deb installation and have skimmed through the creating rosjava packages tutorial.
Building the Jar
Creating a Gradle Sub-Project
This assumes you have already initialised a catkin-gradle package for your java projects. If you haven't done so already, refer to the Creating Rosjava Packages tutorial.
A catkin_create wizard is available that automagically creates a gradle subproject with the appropriate structure and a dummy class for a library jar.
> source devel/setup.bash > cd src/rosjava_catkin_package_a > catkin_create_rosjava_library_project my_java_library > cd ../.. > catkin_make
You should see it work through the gradle tasks and finally dump the resulting artifact (jar) in our local workspace maven repository (devel/share/maven).
:my_java_library:generatePomFileForMavenJavaPublication :my_java_library:compileJava :my_java_library:processResources UP-TO-DATE :my_java_library:classes :my_java_library:jar :my_java_library:publishMavenJavaPublicationToMavenRepository Uploading: com/github/rosjava/rosjava_catkin_package_a/my_java_library/0.1.0/my_java_library-0.1.0.jar to repository remote at file:~rosjava_workspace/devel/share/maven/
The actual .jar that resulted from the build process before uploading can be found in my_java_library/build/libs.
Under the Hood
The Gradle Task
When you run catkin_make, cmake runs through your entire workspace and when it gets to your new project, it will pass off the build to gradle. The tasks that gradle builds can be seen listed in your CMakeLists.txt:
catkin_rosjava_setup(publishMavenJavaPublicationToMavenRepository)
In particular, the publishMavenJavaPublicationToMavenRepository task. This creates the rules that eventually publish your jar into the local workspace's maven repository.
Compiling with Gradle
You could alternatively just compile your subproject alone with gradle (much faster than running catkin_make across your entire workspace):
> source devel/setup.bash > cd src/rosjava_catkin_package_a/my_java_library > ../gradlew publishMavenJavaPublicationToMavenRepository
You might like to set publishMavenJavaPublicationToMavenRepository as a default task inside build.gradle so you can just call gradlew without arguments.
Dependencies
Inspecting this tutorial's build.gradle illustrates an empty file (ok, just comments). If your classes were to have other rosjava dependencies, you would add them to the dependency closure here.
If you would like to make this library available to other projects, then you would need to add to the dependency closure in those projects.
If it was a sibling gradle subproject:
# build.gradle dependencies { compile project(':my_java_library') }
If it was an different catkin-gradle package altogether:
# build.gradle dependencies { compile 'com.github.rosjava.rosjava_catkin_package_a:my_java_library:[0.1,)' }
and to make sure catkin_make can sequence your source builds correctly:
# package.xml <build_depend>rosjava_catkin_package_a</build_depend> <run_depend>rosjava_catkin_package_a</run_depend>