0

I have a XText project within Eclipse, with my own grammar file implemented. Using the doGenerate function I generate several .java files. So far everything works like a charm.

My problem is that every time I generate my files I need to do the following:

  1. Copy the generated files to my Eclipse projects directory
  2. Create a new Java project in Eclipse
  3. Import the generated files to the project

I would like to be able to make any or all of these steps automatic every time the doGenerate function is called, so is there a way to do just that with xtend/java code? I should mention that my grammar requires the user to specify the project directory + name, so that info is available at runtime from within the doGenerate function.

The code should preferably be able to detect if there is already a project with the same name, and update it's contents within the open Eclipse instance at runtime if such a thing is possible.

Alternatively, if this cannot be done with xtend/java, can this be done with a plugin?

Glugstar
  • 45
  • 1
  • 3
  • Creating Eclipse projects has to be done in a plugin. There are already several questions about that such as [this one](http://stackoverflow.com/q/251807/2670892) – greg-449 Apr 26 '15 at 14:56

1 Answers1

1

Creating new projects has to be done through the resources API of eclipse. If you are ok, with having that dependency in your code generator, you can simply add a dependency to org.eclipse.core.resources and do something like

ResourcesPlugin.getWorkspace().getRoot().getProject("myProject").create(null)

If you don't want that dependency, because you run your code generator from other environments (e.g. Maven), you need to abstract the project creation into its own class, inject it and have individual implementations for Eclipse and non-Eclipse.

The code generator API of Xtext (IFileSystemAccess) will use the Eclipse resources API under the hood, to make sure all notifications etc. work as expected and you don't need to manually refresh. So no need for different implementations here, as Xtext takes care of that.

Sven Efftinge
  • 3,065
  • 17
  • 17
  • Yes, your solution is exactly what I was looking for. Problem is for some reason I can't get **import org.eclipse** to work, which is strange and I can't figure out what I'm doing wrong. I suppose I need a specific plugin for that, but I have no idea which is it. – Glugstar Apr 29 '15 at 09:37
  • I would like to ask for further information about the same issue. IFileSystemAccess works great, when it comes to staying in the same project as where the code generators reside. I also can create simple folders in my file system (in the current workspace folder) for the generated files. But these are not recognized as new projects in my Eclipse workspace. Is Xtend framework actually capable of achieving creating executable projects? I believe to achieve that I would need the support of a build process such as Maven, right? Also for integrating the libraries or dependencies of the project. – DanglingElse Jul 20 '15 at 10:28