使用Maven构建XText 2.0和XPand 1.1

时间:2011-10-04 10:28:06

标签: java maven xtext

我正在为使用XText 2.0的项目寻找一个示例pom.xml,尤其是“普通”maven项目中的代码生成器XP和1.1。

我已经花时间与谷歌合作,但我可能会使用错误的术语,或者没有例子。

我已经为xtext 0.7.2工作了pom,我想将项目更新为2.0。但我不知道从哪里开始。 我目前所拥有的是4个maven项目的结构:

  • 看MyDSL
  • mydsl.generator(未使用)
  • mydsl.ui
  • 应用程序(包含用于生成代码的xpand模板)

mydsl项目是xtext 0.7.2项目和(mydsl)中的附加pom,它们将生成的类提供为maven依赖项。

application有一个mwe工作流程和xpand模板来生成源代码。该项目与mydsl

具有maven依赖关系

因为我没有实现如此多的gui编辑器功能,所以我甚至会抛弃所有的xtext东西(除了语法和xpand模板)并构建一个全新的xtext 2项目。

但我真的没有胶水如何为(新)mydsl项目建造pom。

2 个答案:

答案 0 :(得分:2)

我找到了真正的maven xtext2项目。

检查一下: http://code.google.com/a/eclipselabs.org/p/spray/

答案 1 :(得分:1)

最棘手的部分之一是在XText2语法项目中,xtend-gen文件夹中的文件名为<<project>>Generator.java。该文件不是由Generate<<project>>.mwe2工作流创建的,而是由一些eclipse魔法创建的。但是编译代码需要这个文件!

所以我的解决方案是让eclipse生成该文件,然后将其添加到svn,就像任何普通的手写类一样。

然后只需要将这个pom添加到项目中。所以maven也会启动工作流程。 但是有一个缺点:工作流还为邻居项目.ui.test生成类,因此它需要清单文件。为了克服maven发布插件的麻烦,可以添加一个父maven项目,其中包含语法项目和两个假人.ui.test

包含xtext语法的项目的Pom:

...
<dependencies>
    <dependency>
        <groupId>org.eclipse.plugins</groupId>
        <artifactId>org.eclipse.xtext.generator</artifactId>
        <version>2.0.0.v201106070531</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.plugins</groupId>
        <artifactId>org.eclipse.emf.codegen.ecore</artifactId>
        <version>2.6.1.v20100914-1218</version>
    </dependency>
    <dependency>
        <groupId>org.antlr.generator</groupId>
        <artifactId>org.antlr.generator</artifactId>
        <version>3.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.plugins</groupId>
        <artifactId>org.eclipse.emf.mwe2.launch</artifactId>
        <version>2.0.0.v201106070634</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.13</version>
    </dependency>
</dependencies>
<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>src</directory>
        </resource>
        <resource>
            <directory>src-gen</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <filesets>
                    <fileset>
                        <directory>${basedir}/src-gen</directory>
                        <includes>
                            <include>com/</include>
                            <include>org/</include>
                        </includes>
                        <followSymlinks>false</followSymlinks>
                    </fileset>
                    <fileset>
                        <directory>${basedir}/xtend-gen</directory>
                        <includes>
                            <include>com/</include>
                            <include>org/</include>
                        </includes>
                        <excludes>
                            <exclude>**/ProdDef2Generator.java</exclude>
                        </excludes>
                        <followSymlinks>false</followSymlinks>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.fornax.toolsupport</groupId>
            <artifactId>fornax-oaw-m2-plugin</artifactId>
            <version>3.2.1</version>

            <configuration>
                <!-- <checkResources> <checkResource> src/com\queomedia\bcsweb\productdefinition\ProdDef2.xtext 
                    </checkResource> </checkResources> -->
                <outletSrcDir>src-gen</outletSrcDir>
                <outletSrcOnceDir>src</outletSrcOnceDir>
                <rootDir>${project.basedir}</rootDir>
            </configuration>
            <executions>
                <execution>
                    <id>generateGrammer</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>run-workflow</goal>
                    </goals>


                    <configuration>
                        <workflowDescriptor>src/org/test/test/GenerateProdDef2.mwe2</workflowDescriptor>
                        <workflowEngine>mwe2</workflowEngine>
                        <timestampFileName>generator-generateGrammer-timestamp.tmp</timestampFileName>
                    </configuration>
                </execution>
            </executions>
        </plugin>


        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src-gen</source>
                            <source>xtend-gen</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- The plugin.xml must be included, but is not on a resource path -->
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <configuration>
                        <tasks>
                            <copy todir="${basedir}/target/classes" file="${basedir}/plugin.xml" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

使用grammer和dsl生成源代码的项目的POM。这与xtext 0.7.1几乎相同。但有一点不同。 mwe文件中使用的MweReader不再退出(因为xtext 1.0.1)因此需要使用org.eclipse.xtext.mwe.Reader。此读取器不再将模型文件作为输入,而是文件的路径。它还将它存储在一个所谓的“槽”中。但是这个插槽是一个元素列表。因此,需要更改:FOR中的<expand value="templates::Main::main FOR model"/>FOREACH

<dependencies>

    <dependency>
        <groupId>test.xtext</groupId>           
        <artifactId>grammerProject</artifactId>         
        <version>1.0.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.plugins</groupId>
        <artifactId>org.eclipse.xtext.generator</artifactId>
        <version>2.0.0.v201106070531</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.plugins</groupId>
        <artifactId>org.eclipse.emf.codegen.ecore</artifactId>
        <version>2.6.1.v20100914-1218</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.antlr.generator</groupId>
        <artifactId>org.antlr.generator</artifactId>
        <version>3.2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.plugins</groupId>
        <artifactId>org.eclipse.emf.mwe2.launch</artifactId>
        <version>2.0.0.v201106070634</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.13</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
        <!-- there is the .mwe workflow located, in a sub dir called model, the dsl and in an other sub dir called templates, the xpt files. -->
            <directory>src/main/xtext</directory>               
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.fornax.toolsupport</groupId>
            <artifactId>fornax-oaw-m2-plugin</artifactId>
            <version>3.2.1</version>
            <configuration>

                <rootDir>${project.basedir}</rootDir>

                <!--  run only if this file is changed -->
                <checkResources>
                    <checkResource>
                        src/main/xtext/model/GermanDesk.proddef2
                    </checkResource>
                </checkResources>
                <!--  
                <outletSrcDir>src-gen</outletSrcDir>
                <outletSrcOnceDir>src</outletSrcOnceDir>
                -->                 
            </configuration>
            <executions>
                <execution>
                    <id>generateJavaClassesFromXText</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>run-workflow</goal>
                    </goals>
                    <configuration>
                        <!-- Die Workflow-Description wird in einem Ressourcen-Verzeichnis gesucht. -->
                        <workflowDescriptor>ProdDef2JavaGenerator.mwe</workflowDescriptor>
                        <workflowEngine>mwe</workflowEngine>
                        <properties>
                            <projectBasedir>${project.basedir}</projectBasedir>
                        </properties>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>     
                        <sources>
                            <source>${basedir}/target/generated-sources</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

该项目中的Mwe工作流程

<workflow>  
    <property name="project.src.directory" value="${projectBasedir}/src/main"/>
    <property name="project.target.directory" value="${projectBasedir}/target"/>

    <property name="modelFileDir" value="${project.src.directory}/xtext/model/"/>   
    <property name="srcGenTargetDir" value="${project.target.directory}/generated-sources"/>    
    <property name="templateTargetDir" value="${project.target.directory}/generated-sources-templates"/>

    <bean class="org.eclipse.emf.mwe.utils.StandaloneSetup" platformUri=".."/>

    <component class="org.eclipse.emf.mwe.utils.DirectoryCleaner" directory="${srcGenTargetDir}"/>
    <component class="org.eclipse.emf.mwe.utils.DirectoryCleaner" directory="${templateTargetDir}"/>
    <component class="org.eclipse.xtext.mwe.Reader" path="${modelFileDir}" >
        <register class="com.queomedia.bcsweb.productdefinition.ProdDef2StandaloneSetup"/>
        <load slot='model' type='Model'/>
    </component>

    <component class="org.eclipse.xpand2.Generator">
        <metaModel class="org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel"/>
        <fileEncoding value="UTF-8"/>
        <expand value="templates::ProductCustomClass::productCustomClasses FOREACH model"/>
        <genPath value="${templateTargetDir}"/>

        <beautifier class="org.eclipse.xpand2.output.JavaBeautifier"/>  
    </component>

    <component class="org.eclipse.xpand2.Generator">
        <metaModel class="org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel"/>
        <fileEncoding value="UTF-8"/>       
        <expand value="templates::Main::main FOREACH model"/>
        <genPath value="${srcGenTargetDir}"/>

        <beautifier class="org.eclipse.xpand2.output.JavaBeautifier"/>
    </component>

</workflow>