Note: This tutorial assumes that you have completed the previous tutorials: Creating Rosjava Packages, Writing a Publisher and Subscriber, Building RosJava Libraries.
(!) 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.

Generating RosJava Message Artifacts (Catkin Style)

Description: Generating ros-java message jars and artifacts

Keywords: rosjava

Tutorial Level: BEGINNER

Next Tutorial: Surviving RosJava Workspaces

Rosjava message creation has gone a semi-overhaul since hydro (some history here). We now support several convenience methods for generating artifacts as well as one which takes a step closer to being an official message generator.


About

Many of the awkward tricks from hydro are now no longer necessary and the biggest step forward is an implicit message generator that embeds inside the ros message generating infrastructure. So if you have a message package, and genjava installed, it will now magically generate the required java artifacts.

The only drawback right now is that this is not an official generator, so they are not available to you when downloading the corresponding msg deb. This is being worked on, but right now the message generation is just too slow (compare with python and c++ where no compile step is needed). This is not a huge drawback though - we will endeavour to host pre-built artifacts on the rosjava maven repository for as many 'official' message types as possible as well as provide a script that will let you generate them locally from what is already installed.

Prerequisites

You will need genjava, either from source:

> wstool set genjava --git https://github.com/rosjava/genjava --version=indigo

or from debs:

> sudo apt-get install ros-indigo-genjava

Usage

There are three ways you can ensure your message artifacts are created:

  • Command Line : generate for all installed messages in your local environment, in one shot.
  • Genjava : generate from your source workspaces via genjava and message_generation.
  • Meta Msg Pkgs : does message generation for a strict subset of packages.

Type

Pros

Cons

Command Line

Easy

Manual, have to do every time you build your env.

Genjava

Close to official style

Have to have every msg dependency in maven or the source workspace.

Meta Msg Pkgs

Structured

Integrates with rosdep and can go on the build farm (debs).

Command Line

The following commands build artifacts in the current directory under a build subfolder (use -o to change) and puts them in your current workspace's devel/share/maven folder.

1) Build all message artifacts discoverable in your current source workspace or its underlays:

# Make sure you have the latest message packages installed
> sudo apt-get install --only-upgrade ros-indigo-*
# Build them all!
> genjava_message_artifacts --verbose

2) Build a specific message artifact along with all its dependencies:

> genjava_message_artifacts --verbose -p std_msgs

3) Build a specific set of message artifacts along with their dependencies:

> genjava_message_artifacts --verbose -p std_msgs geometry_msgs

Genjava

Simply make sure genjava has been installed and then it will automatically generate artifacts for any source message package it finds alongside python/c++ interfaces. These artifacts will be deployed in your devel/share/maven/org/rosjava/messages folder.

Warning : A big constraint right now is that genjava is not yet official. This means deb installed packages wont have message artifacts lying around - these will have to either come from versions in your source workspace, or various pre-built message artifacts that have been uploaded to the rosjava maven repo.


Tip : If you want to build message artifacts for newer versions of message packages or your own non-deb message packages, just install genjava and drop those message package in your source workspace. Message artifacts will get created on the fly.


Tip : To avoid having genjava run in your non-java source workspaces, use the ROS_LANG_DISABLE environment variable.


Meta Msg Pkgs

It's nice to be able to structure your packages so that everything can be installed via a wstool rosinstall, and a rosdep install command. That is, you'd like your java message artifacts available as a deb. Neither of the above methods facilitate this, so this method is a stopgap to make that happen until the message generator is considered official.

An example of this kind is the rosjava_messages package. This is a strictly zero-code catkin package that depends on genjava and whatever message packages you choose and includes a cmake instruction to generate artifacts for those dependencies.

> mkdir -p ~/rosjava_messages
> wstool init -j4 ~/rosjava_messages/src https://raw.githubusercontent.com/rosjava/rosjava/indigo/rosjava_messages.rosinstall
> source /opt/ros/indigo/setup.bash
> cd ~/rosjava_messages
# Make sure we've got all rosdeps and the latest msg packages.
> rosdep update
> rosdep install --from-paths src -i -y
> sudo apt-get install --only-upgrade ros-indigo-*
> catkin_make

The package.xml dependencies:

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>rosjava_build_tools</build_depend>
  <build_depend>genjava</build_depend>
  <!-- needed for genjava rosdistro dependency -->
  <build_depend>python-rosdistro</build_depend>
  <!-- needed for genjava roslib dependency -->
  <build_depend>roslib</build_depend>

  <!-- your custom messages -->
  <build_depend>std_msgs</build_depend>
  <build_depend>ros_comm_msgs</build_depend>            # ros/ros_comm_msgs
  <build_depend>rosjava_test_msgs</build_depend>        # rosjava/rosjava_test_msgs
  <build_depend>actionlib_msgs</build_depend>           # ros/common_msgs
  <build_depend>diagnostic_msgs</build_depend>
  ...

And CMakeLists.txt excerpts:

find_package(catkin REQUIRED genjava)

generate_rosjava_messages(
  PACKAGES
    std_msgs
    ros_comm_msgs            # ros/ros_comm_msgs
    rosjava_test_msgs        # rosjava/rosjava_test_msgs
    actionlib_msgs           # ros/common_msgs
    common_msgs
    diagnostic_msgs
    ...
)

On subsequent calls to catkin_make, this will not rebuild the artifacts. You can ensure a rebuild by cleaning the package:

> catkin_make --pkg=rosjava_messages --make-args clean
OR
> cd build/rosjava_messages; make clean; cd ../..
# Rebuild
> catkin_make

Since this has a full rosdep list of dependencies, you can now depend on this package when rosinstalling sources, or even get this package rolling on the build farm as an installable deb. It's also a nice easy way to generate a subset of artifacts ready for pushing them to maven.

Tip : Use a workspace like this to generate artifacts for all the packages your require for your own custom development and use it as an underlay to all your other builds.


Tip : Put this package on the ROS build farm to have a deb built!


Warning : Since this is just an external call to artifact generation (not an officially integrated genjava as in method 2)) it is not particularly smart. That is, it won't rebuild when an underlying message file changes. You'll have to resort to manually cleaning and rebuilding in this case.


Wiki: rosjava/Tutorials/indigo/RosJava Message Artifacts (last edited 2016-06-11 17:45:01 by theosakamg)