Xtend Compiler

A Gradle plugin for compiling Xtend source code.

Getting Started

Apply the latest org.xtext.xtend plugin. Then add the Xtend library.

plugins {
  id("org.xtext.xtend") version "5.0.0"
}

repositories {
  mavenCentral()
}

dependencies {
  implementation("org.eclipse.xtext:org.eclipse.xtend.lib:2.45.0")
}
plugins {
  id 'org.xtext.xtend' version '5.0.0'
}

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.eclipse.xtext:org.eclipse.xtend.lib:2.45.0'
}

Features

Source Folders

The plugin compiles the Xtend files in the Java folders of each source set. If your Xtend files are in another folder, add it to the Xtend sources:

sourceSets {
  main {
    xtend.sources.srcDir("src/main/xtend")
  }
}
sourceSets {
  main.xtend.sources.srcDir 'src/main/xtend'
}

You can configure the output folder for each source set. By default it is build/xtend/<sourceSet.name>.

sourceSets {
  main {
    xtend.outputDir = file("xtend-gen")
  }
  test {
    xtend.outputDir = file("test/xtend-gen")
  }
}
sourceSets {
  main.xtend.outputDir = file('xtend-gen')
  test.xtend.outputDir = file('test/xtend-gen')
}

Active Annotations

Xtend runs active annotations from the annotation processor path, like the Java compiler does with annotation processors. Add the library that contains them to the annotationProcessor configuration. Your code refers to the annotations, so the compile classpath needs the library, too.

dependencies {
  implementation(project(":annotations"))
  annotationProcessor(project(":annotations"))
}
dependencies {
  implementation project(':annotations')
  annotationProcessor project(':annotations')
}

Xtend’s own active annotations, like @Accessors and @Data, come with the Xtend compiler. They need nothing on the annotation processor path.

Formatting

The xtextFormat task formats your Xtend files and organizes their imports. The xtextFormatCheck task fails if a file needs formatting. The formatter reads its settings from the Xtend preferences:

xtend {
  preferences(mapOf("indentation" to "  ", "line.width.max" to 100))
}
xtend {
  preferences indentation: '  ', 'line.width.max': 100
}

See the Xtext Builder for more.

Options

The Xtend plugin comes with good default settings that will suit most projects. But you can customize every aspect:

import org.xtext.gradle.SourceInstaller

xtend {
  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
    }
  }
  debugger {
    //how to install debug info into the compiled classes
    //SMAP adds the Xtend source to the debug info, which is the default
    //PRIMARY makes Xtend the only source (throws away Java line numbers)
    sourceInstaller = SourceInstaller.PRIMARY
    //whether to hide synthetic variables in the debugger
    hideSyntheticVariables = true
  }
  validator {
    //adjust severity of issues
    //available levels are error, warning, info and ignore
    error("org.eclipse.xtend.core.validation.IssueCodes.unused_private_member")
    //these issue codes can be found in your Eclipse project's .settings/org.eclipse.xtend.core.Xtend.prefs
    //after enabling Project > Properties > Xtend > Errors/Warnings: [X] Enable project specific settings
  }
}
import org.xtext.gradle.SourceInstaller

xtend {
  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
    }
  }
  debugger {
    //how to install debug info into the compiled classes
    //SMAP adds the Xtend source to the debug info, which is the default
    //PRIMARY makes Xtend the only source (throws away Java line numbers)
    sourceInstaller = SourceInstaller.PRIMARY
    //whether to hide synthetic variables in the debugger
    hideSyntheticVariables = true
  }
  validator {
    //adjust severity of issues
    //available levels are error, warning, info and ignore
    error 'org.eclipse.xtend.core.validation.IssueCodes.unused_private_member'
    //these issue codes can be found in your Eclipse project's .settings/org.eclipse.xtend.core.Xtend.prefs
    //after enabling Project > Properties > Xtend > Errors/Warnings: [X] Enable project specific settings
  }
}