Xtext Builder Plugin
A Gradle Plugin for using Xtext-based code generators. Get the latest version from the Plugin Portal
Features
- Incrementally generates code based on changed files
- Allows multiple languages to cross-reference each other
- Lets DSL files refer to the DSL files of other source sets and projects
- Enhances Java classes with debug information when using Xbase languages
- Formats DSL files and organizes their imports
- Sets up your languages in Eclipse when you import your projects with Buildship
- Works with the build cache, the configuration cache and isolated projects
- Supports Gradle 8.5 and above (tested up to 9.9.3)
- Supports Xtext 2.40 and above (tested up to 2.45.0)
- Requires Java 21 or newer
Minimal Example
Below is the minimal configuration for a language that does not integrate with Java. The plugin creates one Xtext generator task for every source set.
plugins {
id("org.xtext.builder") version "5.0.0"
}
repositories {
mavenCentral()
}
dependencies {
xtextLanguages("org.example:org.example.herolang:3.3.3")
}
xtext {
languages {
create("herolang") {
fileExtensions = setOf("hero") // required, unless the file extension is the name of the language
setup = "org.example.herolang.HerolangStandaloneSetup"
}
}
sourceSets {
create("main") {
sources.srcDir("src/main/heroes")
}
}
}
plugins {
id 'org.xtext.builder' version '5.0.0'
}
repositories {
mavenCentral()
}
dependencies {
xtextLanguages 'org.example:org.example.herolang:3.3.3'
}
xtext {
languages {
herolang {
fileExtensions = ['hero'] // required, unless the file extension is the name of the language
setup = 'org.example.herolang.HerolangStandaloneSetup'
}
}
sourceSets {
main {
sources.srcDir 'src/main/heroes'
}
}
}
Java Integration
If you apply the Java plugin, your languages work with every Java source set. They read the DSL files in its Java and resource folders, and the Java compiler compiles the code that they generate. To add another folder, see the options below.
plugins {
id("org.xtext.builder") version "5.0.0"
java
}
repositories {
mavenCentral()
}
dependencies {
xtextLanguages("org.example:org.example.mydsl:3.3.3")
}
xtext {
languages {
create("mydsl") {
setup = "org.example.mydsl.MyDslStandaloneSetup"
generator.outlet.producesJava = true
}
}
}
plugins {
id 'org.xtext.builder' version '5.0.0'
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
xtextLanguages 'org.example:org.example.mydsl:3.3.3'
}
xtext {
languages {
mydsl {
setup = 'org.example.mydsl.MyDslStandaloneSetup'
generator.outlet.producesJava = true
}
}
}
Referring to Other Projects
Java Projects
If you apply the Java plugin, the DSL files of a source set can refer to the DSL files of the projects that it depends on. The tests can also refer to the DSL files of the main source set. When you publish a Java library, its DSL files go along with it.
To refer to the DSL files of an external library, add it to the xtextModels configuration. A source set other than main has its own, for example testXtextModels.
dependencies {
xtextModels("org.example:legendary-heroes:1.0")
}
dependencies {
xtextModels 'org.example:legendary-heroes:1.0'
}
Non-Java Projects
If a project or the project it refers to does not use the Java plugin, add that project to xtextModels as well:
dependencies {
xtextModels(project(":heroes"))
}
dependencies {
xtextModels project(':heroes')
}
To publish the DSL files of a non-Java project, publish the xtext component. If you publish something else from the same project, give this component coordinates of its own.
publishing {
publications {
create<MavenPublication>("heroes") {
from(components["xtext"])
}
}
}
publishing {
publications {
heroes(MavenPublication) {
from components.xtext
}
}
}
Eclipse Integration
The plugin integrates with Buildship, so Eclipse uses the same Xtext settings as your build.
Formatting
The xtextFormat task formats the DSL files with the formatter of their language. It also organizes the imports of languages that use Xbase, like Xtend. The xtextFormatCheck task fails if a file needs formatting, for example in a CI build or a pre-commit hook. Files with syntax errors stay as they are.
The formatter reads its settings from the preferences of the language:
xtext {
languages {
named("herolang") {
preferences(mapOf("indentation" to " ", "line.width.max" to 100))
}
}
}
xtext {
languages {
herolang {
preferences indentation: ' ', 'line.width.max': 100
}
}
}
To check the formatting in every build, make the check task depend on it:
tasks.check {
dependsOn("xtextFormatCheck")
}
tasks.named('check') {
dependsOn 'xtextFormatCheck'
}
Tasks and Configurations
generateXtext,generate<SourceSet>Xtext: generate the code of a source setxtextFormat: formats the DSL files of all source setsxtextFormatCheck: fails if a DSL file needs formattingxtextEclipseSettings: writes the Eclipse settings of the languages for BuildshipxtextLanguages: the languages that the project usesxtextTooling,<sourceSet>XtextTooling: Xtext and the languages, for a source setxtextModels,<sourceSet>XtextModels: the projects and libraries whose DSL files a source set refers toxtextModelsJar: the jar with the DSL files of the project, which other projects and builds read
Configuration Options Overview
Below is a more elaborate example with two hypothetical languages that makes use of all the current configuration options of the plugin.
import org.xtext.gradle.SourceInstaller
plugins {
id("org.xtext.builder") version "5.0.0"
java
}
repositories {
mavenCentral()
}
dependencies {
xtextLanguages("org.example:org.example.herolang:3.3.3")
xtextLanguages("org.example:org.example.mydsl:1.2.3")
}
xtext {
//Xtext version, can be omitted if the languages or the compile classpath depend on Xtext already
version = "2.45.0"
languages {
//a language configuration can be very simple, everything has good defaults
create("myDsl") {
//the Setup class to use when creating the language's compiler infrastructure
setup = "org.example.mydsl.MyDslStandaloneSetup"
}
//but you can also customize everything to your liking
create("herolang") {
setup = "org.example.herolang.HerolangStandaloneSetup"
//your language's qualified name, if it cannot be determined
//by removing 'StandaloneSetup' from the class listed under 'setup'
qualifiedName = "org.example.herolang.HeroLanguage"
//the file extensions of your language, the name of the language by default
fileExtensions = setOf("hero", "villain")
//whether the language reads the DSL files of other source sets and projects, enabled by default
//disable it for a language like Xtend, whose elements are referred to as Java classes
usesModelpath = true
generator {
//whether to generate @SuppressWarnings("all"), enabled by default
suppressWarningsAnnotation = false
//the Java version of the generated code
//by default the version that the Java compiler of the source set is set to
javaSourceLevel = 17
//whether to generate the @Generated annotation, disabled by default
//needs javax.annotation:javax.annotation-api on the compile classpath
generatedAnnotation {
active = true
comment = "Copyright My Cool Company"
includeDate = true
}
//the outlets, as defined in your language's OutputConfigurationProvider
//the default outlet is available as 'outlet'
outlets {
create("HEROES") {
//the generated files are processed with the resources of the source set
producesResources = true
}
create("VILLAINS") {
//the Java compiler compiles the generated code with the Java code of the source set
producesJava = true
}
create("SIDEKICKS") {
//generate files only once and never overwrite or delete them
//they are meant to be edited, so they belong in a source folder under version control
cleanAutomatically = false
}
}
}
debugger {
//how to install debug info into the compiled classes
//SMAP adds the source of the language to the debug info, which is the default
//PRIMARY makes it the only source (throws away Java line numbers)
sourceInstaller = SourceInstaller.PRIMARY
//whether to hide synthetic variables in the debugger
hideSyntheticVariables = true
}
validator {
//adjust severities of issues
//take the issue codes from the language's validator class
error("something.you.consider.really.Bad")
warning("something.Phishy")
ignore("some.warning.you.dont.care.About")
}
//you can add arbitrary key-value pairs to the preferences
preferences(mapOf("my_special_preference_key" to true))
}
}
sourceSets {
named("main") {
//add folders with DSL files that the Java source set does not have
sources.srcDir("src/main/heroes")
output {
//adjust output directories per source set and outlet
//default here would be 'build/herolangHeroes/main'
dir(xtext.languages["herolang"].generator.outlets["HEROES"]).set(layout.buildDirectory.dir("someSpecialDir"))
dir(xtext.languages["herolang"].generator.outlets["SIDEKICKS"]).set(layout.projectDirectory.dir("src/main/sidekicks"))
}
}
}
}
//tasks that read the generated files should use generatedSources, so that the generator runs first
tasks.register<Copy>("copyHeroes") {
from(xtext.sourceSets["main"].output.generatedSources(xtext.languages["herolang"].generator.outlets["HEROES"]))
into(layout.buildDirectory.dir("heroes"))
}
import org.xtext.gradle.SourceInstaller
plugins {
id 'org.xtext.builder' version '5.0.0'
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
xtextLanguages 'org.example:org.example.herolang:3.3.3'
xtextLanguages 'org.example:org.example.mydsl:1.2.3'
}
xtext {
//Xtext version, can be omitted if the languages or the compile classpath depend on Xtext already
version = '2.45.0'
languages {
//a language configuration can be very simple, everything has good defaults
myDsl {
//the Setup class to use when creating the language's compiler infrastructure
setup = 'org.example.mydsl.MyDslStandaloneSetup'
}
//but you can also customize everything to your liking
herolang {
setup = 'org.example.herolang.HerolangStandaloneSetup'
//your language's qualified name, if it cannot be determined
//by removing 'StandaloneSetup' from the class listed under 'setup'
qualifiedName = 'org.example.herolang.HeroLanguage'
//the file extensions of your language, the name of the language by default
fileExtensions = ['hero', 'villain']
//whether the language reads the DSL files of other source sets and projects, enabled by default
//disable it for a language like Xtend, whose elements are referred to as Java classes
usesModelpath = true
generator {
//whether to generate @SuppressWarnings("all"), enabled by default
suppressWarningsAnnotation = false
//the Java version of the generated code
//by default the version that the Java compiler of the source set is set to
javaSourceLevel = 17
//whether to generate the @Generated annotation, disabled by default
//needs javax.annotation:javax.annotation-api on the compile classpath
generatedAnnotation {
active = true
comment = "Copyright My Cool Company"
includeDate = true
}
//the outlets, as defined in your language's OutputConfigurationProvider
//the default outlet is available as 'outlet'
outlets {
HEROES {
//the generated files are processed with the resources of the source set
producesResources = true
}
VILLAINS {
//the Java compiler compiles the generated code with the Java code of the source set
producesJava = true
}
SIDEKICKS {
//generate files only once and never overwrite or delete them
//they are meant to be edited, so they belong in a source folder under version control
cleanAutomatically = false
}
}
}
debugger {
//how to install debug info into the compiled classes
//SMAP adds the source of the language to the debug info, which is the default
//PRIMARY makes it the only source (throws away Java line numbers)
sourceInstaller = SourceInstaller.PRIMARY
//whether to hide synthetic variables in the debugger
hideSyntheticVariables = true
}
validator {
//adjust severities of issues
//take the issue codes from the language's validator class
error 'something.you.consider.really.Bad'
warning 'something.Phishy'
ignore 'some.warning.you.dont.care.About'
}
//you can add arbitrary key-value pairs to the preferences
preferences my_special_preference_key: true
}
}
sourceSets {
main {
//add folders with DSL files that the Java source set does not have
sources.srcDir 'src/main/heroes'
output {
//adjust output directories per source set and outlet
//default here would be 'build/herolangHeroes/main'
dir(xtext.languages.herolang.generator.outlets.HEROES).set(layout.buildDirectory.dir('someSpecialDir'))
dir(xtext.languages.herolang.generator.outlets.SIDEKICKS).set(layout.projectDirectory.dir('src/main/sidekicks'))
}
}
}
}
//tasks that read the generated files should use generatedSources, so that the generator runs first
tasks.register('copyHeroes', Copy) {
from xtext.sourceSets.main.output.generatedSources(xtext.languages.herolang.generator.outlets.HEROES)
into layout.buildDirectory.dir('heroes')
}
Creating a Plugin for your Language
If you are the maintainer of an Xtext language, do your users a favor and create a reusable Gradle plugin. Publish it on the Gradle plugin portal, so your users don’t have to copy-paste the configuration into all their build scripts. This way, for most cases the user doesn’t even need to know that your language uses Xtext.
This is how a plugin for a simple Xtext language could look like (written in Xtend). It adds the language in the version that the plugin was released with.
package org.mylang.gradle
import org.gradle.api.*
import org.xtext.gradle.*
class MyLangPlugin implements Plugin<Project> {
override apply(Project project) {
project.pluginManager.apply("org.xtext.builder")
project.dependencies.add("xtextLanguages", "org.example:org.example.mylang:1.2.3")
val xtext = project.extensions.getByType(XtextExtension)
xtext.languages.maybeCreate("myLang") => [
setup.convention("org.example.myLang.MyLangStandaloneSetup")
]
}
}
And this is how easy it would be for your users to consume:
plugins {
id("org.example.mylang") version "1.2.3"
}
plugins {
id 'org.example.mylang' version '1.2.3'
}