eclipsemavenxtext

Porting Xtext Eclipse project to Maven


I am trying to port an existing Xtext 2.21.0 project from Eclipse 2020-03 to Maven.

I have generated an example Xtext Maven project using Eclipse, and then populated it with my DSL. The issue is that my grammars cannot find the references to other grammars.

529  ERROR XtextGeneratorLanguage - [XtextLinkingDiagnostic: null:1 Couldn't resolve reference to Grammar 'abc.generator.ServiceGenerator'., XtextLinkingDiagnostic: null:4 Couldn't resolve reference to EPackage 'http://www.generator.abc/ServiceGenerator'., XtextLinkingDiagnostic: null:117 STRING cannot be resolved to a rule, XtextLinkingDiagnostic: null:249 INT cannot be resolved to a rule, TransformationDiagnostic: null:248 Datatype rules may only use other datatype rules, lexer rules and keywords. (ErrorCode: InvalidDatatypeRule)]
531  ERROR Mwe2Launcher       - Problems running workflow abc.generator.GenerateRawNotificationGenerator: Problem parsing 'file:/C:/Users/xxx/Desktop/eclipse_plugin_pom/abc.generator/../abc.generator/src/abc/generator/VarDefinitionGenerator.xtext':
XtextLinkingDiagnostic: null:1 Couldn't resolve reference to Grammar 'abc.generator.ServiceGenerator'.
XtextLinkingDiagnostic: null:4 Couldn't resolve reference to EPackage 'http://www.generator.abc/ServiceGenerator'.
XtextLinkingDiagnostic: null:117 STRING cannot be resolved to a rule
XtextLinkingDiagnostic: null:249 INT cannot be resolved to a rule
TransformationDiagnostic: null:248 Datatype rules may only use other datatype rules, lexer rules and keywords. (ErrorCode: InvalidDatatypeRule)

POM files are just the default generated POM by Eclipse Xtext 2.21.0, the only modification is my project names and URIs.

Mwe2 file:

module abc.generator.GenerateRawNotificationGenerator

import org.eclipse.xtext.xtext.generator.*
import org.eclipse.emf.mwe.utils.StandaloneSetup
import org.eclipse.xtext.xtext.generator.model.project.*

var rootPath = ".."

Workflow {
    
    component = XtextGenerator 
    {
        configuration = 
        {
            project = StandardProjectConfig 
            {
                baseName = "abc.generator"
                rootPath = rootPath
                runtimeTest = 
                {
                    enabled = true
                }
                
                eclipsePlugin = 
                {
                    enabled = true
                }
                
                eclipsePluginTest = 
                {
                    enabled = true
                }
                
                createEclipseMetaData = true
            }
            code = 
            {
                encoding = "windows-1252"
                lineDelimiter = "\r\n"
                fileHeader = "/*\n * generated by Xtext \${version}\n */"
            }
        }
        
        language = StandardLanguage 
        {
            name = "abc.generator.ServiceGenerator"
            fileExtensions = "services"

            serializer = 
            {
                generateStub = false
            }
            
            validator = 
            {
                composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
            }
        }

        language = StandardLanguage 
        {
            name = "abc.generator.VarDefinitionGenerator"
            fileExtensions = "vardefinition"

            serializer = 
            {
                generateStub = false
            }
            
            validator = 
            {
                composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
            }
        }
    }
}

ServiceGenerator header:

grammar abc.generator.ServiceGenerator with org.eclipse.xtext.common.Terminals
generate serviceGenerator "http://www.generator.abc/ServiceGenerator"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

VarDefinitionGenerator header:

grammar abc.generator.VarDefinitionGenerator with abc.generator.ServiceGenerator
generate varDefinitionGenerator "http://www.generator.abc/VarDefinitionGenerator"

import "http://www.generator.abc/ServiceGenerator"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

I must mention I am not familiar at all with Xtext, but I am familiar with Maven. This project is working just fine in Eclipse 2020-03, but we need to get rid of Eclipse because of its bugs. I tried upgrading to Eclipse 2023-09, Java 17 and Xtext 2.32.0, but then the project doesn't work (some Injected variables come back as null).

What I cannot understand is whether the Eclipse plugin.xml file should be added somewhere in the mwe2 file.

<?xml version="1.0" encoding="windows-1252"?>
<?eclipse version="3.0"?>
<plugin>
    <extension point="org.eclipse.emf.ecore.generated_package">
        <package 
            uri = "http://www.generator.abc/ServiceGenerator"
            class = "abc.generator.serviceGenerator.ServiceGeneratorPackage"
            genModel = "model/generated/ServiceGenerator.genmodel" />
    </extension>
    <extension point="org.eclipse.emf.ecore.generated_package">
        <package 
            uri = "http://www.generator.abc/VarDefinitionGenerator"
            class = "abc.generator.varDefinitionGenerator.VarDefinitionGeneratorPackage"
            genModel = "model/generated/VarDefinitionGenerator.genmodel" />
    </extension>
</plugin>

Solution

  • I have found a solution. It seems that there is a bug in xtext Maven which is solved by adding the src directory to additionalClasspathElements.

    By doing this you do not need to define multiple referencedResource inside your .mwe2 file anymore.

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        ...
        <configuration>
            <mainClass>org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher</mainClass>
            ...
    
            <additionalClasspathElements>
                <additionalClasspathElement>${project.basedir}/src</additionalClasspathElement>
            </additionalClasspathElements>
        </configuration>
    

    Many many thanks for the contribuitor of the solution here: https://github.com/eclipse/xtext/issues/2394#issuecomment-1511031257

    I accidentally found it by first defining multiple referencedResource for each language in the .mwe2 file, but this lead to another PDA error which led me to the proper solution for both issues.