A maven plugin that can be used to generate Java source files from Flatbuffers schema files.
It works by getting a specified tagged version of the flatbuffers source code from google's repositories,
storing it to a directory ~/.flatbuffers
, checking out the required tag, compiling it to obtain the flatc
executable and then using it to generate code from the schemas in the generate-sources
compilation maven phase.
It can also be used in a simpler way with a flatc
executable manually installed to the .flatbuffers
directory.
Use the plugin as below in your maven build, making sure you also bring the required flatbuffers dependency:
<properties>
<flatbuffers.version>1.12.0</flatbuffers.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>${flatbuffers.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.sequsoft.maven.plugins</groupId>
<artifactId>flatbuffers-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>compile-flatbuffers</goal>
</goals>
</execution>
</executions>
<configuration>
<version>1.12.0</version>
<sources>
<source>${basedir}/src/main/resources/F_HouseGroup.fbs</source>
</sources>
<generators>
<generator>all</generator>
</generators>
<includes>
<include>${basedir}/src/main/resources</include>
</includes>
<destination>${basedir}/target/generated-sources</destination>
</configuration>
</plugin>
</plugins>
</build>
Available parameters for the configuration
section are:
version
- the tagged version of flatbuffers to use, e.g.1.12.0
(withoutv
at the front).flatbuffersUrl
- the url to the flatbuffers repository, useful if you have your own version.sources
- a list ofsource
values, the schema filesincludes
- a list ofinclude
values for directories to search for schema includesdestination
- the destination directory into which the generated java code files should be placedgenerators
a list ofgenerator
options, the same as those available in flatc, from the list:all
- generate all filesmutable
- generate code with mutator methodsnullable
- generate@Nullable
attributes on method parametersgenerated
- generate@Generated
attribute on all classes
When the plugin runs, it first checks for the existence of a compiled flatc file in the ~/.flatbuffers
directory.
If this file exists and reports it is the required version, then the generation of sources proceeds immediately.
If the flatc version is incorrect, it is ensured that the .flatbuffers
directory is a Git repository retrieved from
github. It is ensured that it is checked out at the right tag version and then flatc is compiled. Once this is done,
the generation of sources can proceed.
Note that retrieval and compilation of the executable is useful so that the plugin will work on multiple linux platforms (and on OSX).
If you already have the compiled flatc executable, it is simply enough to place it in the .flatbuffers
directory and it
will be used with no further effort.
Your platform must be able to retrieve and build the flatbuffers code. Generally this requires the additional tools beyond
what would be available on a purely Java building platform, namely: cmake
, make
and a c++ compiler.
For example on CentOS 7 it was adequate to install: cmake
, gcc
and gcc-c++
.
Tested on:
- linux, e.g. CentosOS, Ubuntu
- OSX
- Windows 10 (with
flatc.exe
manually installed)
- I haven't been able to make this work on Windows as I haven't been able to get the
cmake
process to work. Anyway, on Windows maybe it is better just to obtain and install theflatc.exe
file to the.flatbuffers
directory.