The repository contains multiple xtext projects:
- mla: grammar a which is independent
- mlb: grammar b, which inherits from grammar a
- mlc: grammar c, which inherits from grammar b
and simple demo-project that use the grammars and have each a very simple model file:
- demoA: uses grammar
mla
(which is independent) - demoB: uses grammar
mlb
(which inherits from grammar mla) and is dependent ondemoA
- demoC: uses grammar
mlc
(which inherits from grammar mlb) and is dependent ondemoC
in the dsl
folder execute gradlew build install
- it will install the projects to the local maven repository
in the demo
folder, execute gradlew build
For example mlb inherits from mla.
The gradle build file in mlb
must declare a dependency on mla
group = 'com.tmtron.ex.xtext.mlb'
dependencies {
// OTHER DEPENDENCIES
compile project(':mla')
}
In the mwe2 workflow file we must mention the referenced Ecore model:
Workflow {
language = StandardLanguage {
referencedResource = "platform:/resource/mla/model/generated/DslA.genmodel"
In the xtext file we inherit from DslA
:
grammar com.tmtron.ex.xtext.mlb.DslB with com.tmtron.ex.xtext.mla.DslA
generate dslB "http://www.tmtron.com/ex/xtext/mlb/DslB"
import "http://www.tmtron.com/ex/xtext/mla/DslA" as dsla
demoA
wants to use DslA.
We use the xtext-gradle-plugin which will configure the gradle build and start the xtext-generator.
First we make the xtext-gradle-plugin available to the gradle build: in the parent build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.xtext:xtext-gradle-plugin:1.0.20'
}
}
Then we can use and configure the xtext-gradle-plugin in the build.gradle file of demoA
:
apply plugin: 'org.xtext.builder'
dependencies {
xtextLanguages 'com.tmtron.ex.xtext.mla:mla:1.0.0-SNAPSHOT'
}
xtext {
version = "$xtextVersion"
languages {
dsla {
setup = 'com.tmtron.ex.xtext.mla.DslAStandaloneSetup'
generator {
outlet.producesJava = true
}
}
}
Now we can simply create a model file, with the correct file extension: modelA.dsla and start the gradle build.
The xtext-gradle-plugin will start our generator before the java compilation and infer the correct classpath.
For example demoB uses the model from demoA.
demoB
needs to read modelA.dsla. Thus we must make sure that the model file ends up in the generated .jar
file.
We can simply add the java source dir to the build resources in the gradle build file of demoA:
// we must also export the model because the other project/s will refer to it
sourceSets.main.resources.srcDirs += ["src/main/java"]
In the gradle.build file of demoB
we must declare a dependency to demoA
, because we need access to modelA.dsla (and we may also need some java files from demoA
)
dependencies {
compile project(':demoA')
The language configuration is the same as described in the previous section.
Now we can create a modelB.dslb file and import/use definitions from modelA.dsla:
package com.tmtron.ex.dslb
import com.tmtron.ex.dsla.*
def fieldB: String;
ref fieldA;