Generate Code from Thrift Definitions

Problem

You've created Thrift definitions (structs, services, etc.) and you need to generate either Thrift-based

  • classes for use within your Scala or Java project, or
  • libraries that can be used by your project or other projects.

Solution

Use the gen goal to generate code from Thrift definitions. Here's an example:

$ ./pants gen src/thrift/example:thrift-scala

If you need to compile a Scala or Java library target instead, use the compile goal instead.

Discussion

There are two types of Thrift target definitions that you will find in BUILD files in existing projects:

  • java_thrift_library (for Scala and Java)
  • python_thrift_library (for Python)

You can use the gen and compile goals directly with java_thrift_library targets. Thus, you could target a BUILD file containing this definition...

java_thrift_library(
  name='thrift-java',
  # Other parameters
)

...like this using Pants:

$ ./pants gen src/thrift/example:thrift-java

To learn more about Thrift, check out the Thrift Example docs.

See Also

Generated by publish_docs from dist/markdown/html/src/docs/common_tasks/thrift_gen.html 2022-12-03T01:09:00.401358