Friday 6 September 2013

Resolving an issue with maven-tycho-plugin(P2) and xtext-maven-examples

My problem

A while ago now, I created a very simple project with examples of how to build XText projects with Maven / Tycho plugins.  https://github.com/aphethean/xtext-maven-examples  Thank you to everyone who has shown interest and starred the project - I'm utterly amazed that anyone has followed the project, but it must be useful to a few people who also struggled to get a maven build for XText working.  Anyway, back to the problem.  It stopped building!

A good build should always be stable; and only unstable if the code has changed.  In my case our internal project stopped working and when I checked my little example it too had stopped working.

My build error is as follows:
     [INFO] --- fornax-oaw-m2-plugin:3.4.0:run-workflow (default) @ org.xtext.example.mydsl ---
[INFO] Fornax Model Workflow Maven2 Plugin V3.4.0
[INFO] Executing workflow in forked mode.
[INFO] 0    INFO  StandaloneSetup    - Registering platform uri 'C:\projects\xtext-maven-examples1\2.3.0-example'
[INFO] 186611 INFO  StandaloneSetup    - Adding generated EPackage 'org.eclipse.xtext.xbase.XbasePackage'
[INFO] 187449 INFO  GenModelHelper     - Registered GenModel 'http://www.eclipse.org/Xtext/Xbase/XAnnotations' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmod
'
[INFO] 187453 INFO  GenModelHelper     - Registered GenModel 'http://www.eclipse.org/xtext/xbase/Xtype' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel'
[INFO] 187467 INFO  GenModelHelper     - Registered GenModel 'http://www.eclipse.org/xtext/xbase/Xbase' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel'
[INFO] 187468 INFO  GenModelHelper     - Registered GenModel 'http://www.eclipse.org/xtext/common/JavaVMTypes' from 'platform:/resource/org.eclipse.xtext.common.types/model/JavaV
ypes.genmodel'
[INFO] 188862 INFO  DirectoryCleaner   - Cleaning C:\projects\xtext-maven-examples1\2.3.0-example\org.xtext.example.mydsl\..\org.xtext.example.mydsl\src-gen
[INFO] 188866 INFO  DirectoryCleaner   - Cleaning C:\projects\xtext-maven-examples1\2.3.0-example\org.xtext.example.mydsl\..\org.xtext.example.mydsl.ui\src-gen
[INFO] 189378 INFO  LanguageConfig     - generating infrastructure for org.xtext.example.mydsl.MyDsl with fragments : ImplicitRuntimeFragment, ImplicitUiFragment, GrammarAccessFr
ment, EcoreGeneratorFragment, SerializerFragment, ResourceFactoryFragment, XtextAntlrGeneratorFragment, JavaValidatorFragment, ImportNamespacesScopingFragment, QualifiedNamesFrag
nt, BuilderIntegrationFragment, GeneratorFragment, FormatterFragment, LabelProviderFragment, OutlineTreeProviderFragment, QuickOutlineFragment, QuickfixProviderFragment, JavaBase
ontentAssistFragment, XtextAntlrUiGeneratorFragment, Junit4Fragment, RefactorElementNameFragment, TypesGeneratorFragment, XbaseGeneratorFragment, CodetemplatesGeneratorFragment,
mpareFragment
[INFO] 193899 INFO  GenModelHelper     - Registered GenModel 'http://www.xtext.org/example/mydsl/MyDsl' from 'platform:/resource/org.xtext.example.mydsl/src-gen/org/xtext/example
ydsl/MyDsl.genmodel'
[ERROR] 195973 ERROR CompositeGeneratorFragment - Explicit encoding was set but is not supported by the available version of the AntlrToolRunner.
[INFO] Please use the ANTLR parser generator in version 2.1 or better
[INFO] java.lang.IllegalStateException: Explicit encoding was set but is not supported by the available version of the AntlrToolRunner.
[INFO] Please use the ANTLR parser generator in version 2.1 or better
[ERROR]         at org.eclipse.xtext.generator.parser.antlr.AntlrToolFacade.runWithEncodingAndParams(AntlrToolFacade.java:178)
[ERROR]         at org.eclipse.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment.generate(XtextAntlrGeneratorFragment.java:47)
[ERROR]         at org.eclipse.xtext.generator.CompositeGeneratorFragment.generate(CompositeGeneratorFragment.java:92)
[ERROR]         at org.eclipse.xtext.generator.LanguageConfig.generate(LanguageConfig.java:113)
[ERROR]         at org.eclipse.xtext.generator.Generator.generate(Generator.java:366)
[ERROR]         at org.eclipse.xtext.generator.Generator.invokeInternal(Generator.java:132)
[ERROR]         at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:126)
[ERROR]         at org.eclipse.emf.mwe.core.lib.Mwe2Bridge.invoke(Mwe2Bridge.java:34)
[ERROR]         at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:201)
[ERROR]         at org.eclipse.emf.mwe2.runtime.workflow.AbstractCompositeWorkflowComponent.invoke(AbstractCompositeWorkflowComponent.java:35)
[ERROR]         at org.eclipse.emf.mwe2.runtime.workflow.Workflow.run(Workflow.java:19)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:102)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:62)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:52)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.run(Mwe2Launcher.java:74)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.main(Mwe2Launcher.java:35)
[INFO] 195995 INFO  JavaValidatorFragment - generating Java-based EValidator API
[ERROR] 196438 ERROR CompositeGeneratorFragment - Explicit encoding was set but is not supported by the available version of the AntlrToolRunner.
[INFO] Please use the ANTLR parser generator in version 2.1 or better
[INFO] java.lang.IllegalStateException: Explicit encoding was set but is not supported by the available version of the AntlrToolRunner.
[INFO] Please use the ANTLR parser generator in version 2.1 or better
[ERROR]         at org.eclipse.xtext.generator.parser.antlr.AntlrToolFacade.runWithEncodingAndParams(AntlrToolFacade.java:178)
[ERROR]         at org.eclipse.xtext.generator.parser.antlr.XtextAntlrUiGeneratorFragment.generate(XtextAntlrUiGeneratorFragment.java:85)
[ERROR]         at org.eclipse.xtext.generator.CompositeGeneratorFragment.generate(CompositeGeneratorFragment.java:92)
[ERROR]         at org.eclipse.xtext.generator.LanguageConfig.generate(LanguageConfig.java:113)
[ERROR]         at org.eclipse.xtext.generator.Generator.generate(Generator.java:366)
[ERROR]         at org.eclipse.xtext.generator.Generator.invokeInternal(Generator.java:132)
[ERROR]         at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:126)
[ERROR]         at org.eclipse.emf.mwe.core.lib.Mwe2Bridge.invoke(Mwe2Bridge.java:34)
[ERROR]         at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:201)
[ERROR]         at org.eclipse.emf.mwe2.runtime.workflow.AbstractCompositeWorkflowComponent.invoke(AbstractCompositeWorkflowComponent.java:35)
[ERROR]         at org.eclipse.emf.mwe2.runtime.workflow.Workflow.run(Workflow.java:19)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:102)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:62)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:52)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.run(Mwe2Launcher.java:74)
[ERROR]         at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.main(Mwe2Launcher.java:35)
[INFO] 196439 INFO  Junit4Fragment     - generating Junit4 Test support classes
[INFO] 196495 INFO  CompareFragment    - generating Compare Framework infrastructure
[INFO] 196731 INFO  Workflow           - Done.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] org.xtext.example.mydsl.MyDsl - Parent ............ SUCCESS [3.759s]
[INFO] org.xtext.example.mydsl.MyDsl - Grammar ........... FAILURE [3:51.033s]
[INFO] org.xtext.example.mydsl.MyDsl - UI ................ SKIPPED
[INFO] org.xtext.example.mydsl.sdk ....................... SKIPPED
[INFO] org.xtext.example.mydsl.tests ..................... SKIPPED
[INFO] org.xtext.example.mydsl.MyDsl - Repackaged Eclipse plugins  SKIPPED
[INFO] org.xtext.example.mydsl.MyDsl - Generator ......... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7:13.750s
[INFO] Finished at: Thu Sep 05 16:27:04 BST 2013
[INFO] Final Memory: 98M/625M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.fornax.toolsupport:fornax-oaw-m2-plugin:3.4.0:run-workflow (default) on project org.xtext.example.mydsl: Workflow execution failed. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :org.xtext.example.mydsl
C:\projects\xtext-maven-examples1\2.3.0-example\org.xtext.example.mydsl.parent>



Searching for a solution (ask Google approach)

After a couple false starts I started to get the feeling that I'd have to debug this one.  The error seemed to point to an encoding problem "Explicit encoding was set but is not supported by the available version of the AntlrToolRunner" - this just doesn't make sense.  My test source file might have a dodgy character it it (but the source hadn't changed remember) or it could also have been an Antlr version problem, but I'm still using the same configuration.

Here are a few sites I search for answers:
Maybe its something to do with Antlr?  No.  http://mojo.codehaus.org/antlr-maven-plugin/
Maybe the project pom configuration is wrong?  No.  http://kthoms.wordpress.com/2010/08/18/building-xtext-projects-with-maven-tycho/
Something to do with the P2 resolver that I don't understand very well?  Maybe.  http://stackoverflow.com/questions/6682028/use-dependencies-from-eclipse-p2-repository-in-a-regular-maven-build
More P2 knowledge required I think:  http://thomaskratz.blogspot.de/2013/04/consuming-emf-bundles-from-p2-with.html
Trying to understand Tycho a bit better:  http://eclipsesource.com/blogs/2012/08/26/building-an-eclipse-project-with-tycho-experience-report/
Tycho configuration issue?  Maybe.  http://stackoverflow.com/questions/15006721/configuration-issue-with-tycho-maven-tycho-build-extension-not-configured

After this problem and another problem where the build mysteriously broke, I was pretty sure that I was somehow picking up a different version of the XText libraries.  Turns out I was, and I simply didn't understand how the P2 resolving worked.  Basically the OSGi configuration in the MANIFEST.MF is used to form the maven dependencies.  I hadn't configured a specific version, this is the gold:
http://stackoverflow.com/questions/13288822/maven-tycho-takes-the-wrong-bundle-version/13290831#13290831
http://fusesource.com/docs/esb/4.4/esb_deploy_osgi/BestPractices-BuildBundles.html


My configuration

It's all there in the GitHub project, but the two main pieces were the pom.xml and the MANIFEST.MF


Debugging the problem

To debugging the problem I used mvn -X and the mvn dependency:tree option.  With these I could clearly see the wrong version of XText.

$ mvn -X clean install

[look for the java -classpath part of the output, you'll see the wrong jars for xtext]
[DEBUG] C:\Java\jdk1.7.0_25\jre\bin\java.exe -classpath 


$ mvn dependency-tree

[INFO] +- p2.eclipse-plugin:org.eclipse.emf.mwe.core:jar:1.2.1.v201309030422:system
[INFO] +- p2.eclipse-plugin:org.eclipse.emf.mwe.utils:jar:1.3.1.v201309030422:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtext:jar:2.4.3.v201309030823:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtext.generator:jar:2.4.3.v201309030823:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtext.common.types:jar:2.4.3.v201309030823:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtext.util:jar:2.4.3.v201309030823:system
[INFO] +- p2.eclipse-plugin:org.antlr.runtime:jar:3.2.0.v201101311130:system
[INFO] +- p2.eclipse-plugin:org.eclipse.emf.mwe2.lib:jar:2.4.1.v201309030422:system
[INFO] +- p2.eclipse-plugin:org.apache.log4j:jar:1.2.15.v201012070815:system
[INFO] +- p2.eclipse-plugin:org.eclipse.emf.mwe2.language:jar:2.4.1.v201309030840:system
[INFO] +- p2.eclipse-plugin:org.eclipse.emf.mwe2.launch:jar:2.4.1.v201309030840:system
[INFO] +- p2.eclipse-plugin:com.google.guava:jar:10.0.1.v201203051515:system
[INFO] +- p2.eclipse-plugin:org.eclipse.jdt.annotation:jar:1.0.1.v20130111-135246:system
[INFO] +- p2.eclipse-plugin:org.eclipse.jdt.debug:jar:jdi.jar:3.7.101.v20120913-153601:system
[INFO] +- p2.eclipse-plugin:org.eclipse.jdt.debug:jar:jdimodel.jar:3.7.101.v20120913-153601:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtend:jar:1.4.0.v201306110406:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xpand:jar:1.4.0.v201306110406:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtext.xbase.lib:jar:2.4.3.v201309030823:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtend.lib:jar:2.4.3.v201309030823:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtend.typesystem.emf:jar:1.4.0.v201306110406:system
[INFO] +- p2.eclipse-plugin:org.eclipse.xtext.smap:jar:2.4.3.v201309030823:system
[INFO] \- p2.eclipse-plugin:org.eclipse.xtext.ecore:jar:2.4.3.v201309030823:system


Now for the solution

Understand how the OSGi configuration in the MANIFEST.MF works.  It's pretty simple syntax, but this page helped me:  http://fusesource.com/docs/esb/4.4/esb_deploy_osgi/BestPractices-BuildBundles.html

You can also use the manifest editor included with Eclipse and just set your version ranges appropriately

xtext-maven-examples\2.3.0-example\org.xtext.example.mydsl\META-INF\MANIFEST.MF
Require-Bundle: org.eclipse.xtext;bundle-version="[2.3.0,2.3.1]";visibility:=reexport,
 org.eclipse.xtext.xbase;bundle-version="[2.3.0,2.3.1]";resolution:=optional;visibility:=reexport,
 org.apache.commons.logging;bundle-version="1.0.4";resolution:=optional,
 org.eclipse.emf.codegen.ecore;resolution:=optional,
 org.eclipse.emf.mwe.utils;resolution:=optional,
 org.eclipse.emf.mwe2.launch;resolution:=optional,
 org.eclipse.xtext.util;bundle-version="[2.3.0,2.3.1]",
 org.eclipse.emf.ecore,
 org.eclipse.emf.common,
 org.antlr.runtime,

 org.eclipse.xtext.common.types;bundle-version="[2.3.0,2.3.1]"

Sound obvious, but make sure you change the version in all your MANIFEST.MFs.  I missed the test project and scratched my head for a few minutes more than I needed to:

Require-Bundle: org.xtext.example.mydsl,
 org.xtext.example.mydsl.ui,
 org.eclipse.core.runtime,

 org.eclipse.xtext.junit4;bundle-version="[2.3.0,2.3.1]",
 org.eclipse.ui.workbench;resolution:=optional


Now the build will work and hopefully carry on working!


No comments:

Post a Comment