views:

64

answers:

2

Hello,

I would like to write a simple Ant task that would interact with an Eclipse workspace to get some information from it. I would like to be able to use the various Eclipse API's (for example, IWorkspace).

My question is what would be the simplest way to go about doing this.

I have excellent knowledge of the Eclipse platform as a user of it - but none with development for Eclipse. I understand that, in runtime, my Ant task will have to be invoked under the same JRE as the workspace - that's a restriction I'm willing to be bound to.

I started by creating a Java project to hold my Ant task. I believe that the only thing in my way now is how to define the JAR dependency on Eclipse's JARs. I obviously don't want to depend on one specific Eclipse JAR as these are versioned (for example, Eclipse's "Resources" plugin). Is it possible to have my Java project buildtime-depend on "whatever the current JAR is for the Resources plugin"?

Thanks, Isaac

A: 

You should probably say more about what you want to do, because there are several routes you can take.

  1. Eclipse provides some Ant tasks that you can use in your build scripts. Here are a few. There are more, so search for "ant tasks" in the Eclipse docs; they're scattered throughout different pages. Eclipse Preferences (Window..Preferences, then select Ant/Runtime, and look at the "Contributed Entries" in the Classpath tab) shows you a list of Ant tasks that Eclipse contributes to the runtime whenever Ant is invoked from Eclipse; you can invoke any of these tasks yourself as long as you include the right jar.
  2. It's possible to start up Eclipse from within an Ant task, because it's possible to start Eclipse from Java. You need to include the right jars and make the right calls; you also need to configure Eclipse correctly. "org.eclipse.core.runtime.adaptor.EclipseStarter" should give you some detail.
  3. Perhaps you can use one or more of the variables Eclipse defines for use when launching Ant (or any other program). Try creating an External Tool Configuration (at the bottom of the Run menu) - select an Ant Build and try customizing it with arguments (from the Main tab) or environment variables (from the Environment) tab. Both give you access to Eclipse variables. But of course these are just values you can pass into your Ant script, not handles to anything you can invoke a method on.

What you can't do: I'm pretty sure that when Eclipse launches Ant, it is always in a separate VM, with no way to call back into Eclipse.

So perhaps you should say more about what you want to do.

Ladlestein
Hi Ladlestein, yes I am aware of the provided tasks as you mentioned in (1) above. What I want is really to learn how to go about writing Ant tasks, not how to implement a particular functionality (as I know there's a lot of functionality provided built-in by Eclipse). So, lets consider the case of retrieving, into an Ant property, a comma-separated list of all projects in the workspace (yes, I know Ant4Eclipse does that but I'm looking for an Eclipse-API-based approach to achieve this).
Isaac
@Isaac, that clarifies what you're looking for. I think you'll want to look at the EclipseStarter class I described above. Docs start at http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime/adaptor/EclipseStarter.html.
Ladlestein
@Ladlestein, thanks however I think I was a bit unclear. The EclipseStarter class would help me fire-up a workbench, do my thing and get out. That would be option (2) in your answer above. What I really want is to learn how to write tasks that can run under a *running* Eclipse instance - those that fall under category (1) in your answer above.
Isaac
To elaborate even further: I would like to create an Ant task that runs within Eclipse, in the same manner that, say, `eclipse.refreshLocal` is running. Clearly, `eclipse.refreshLocal` and other related tasks make use of the Eclipse API; that's exactly what I want to do but I don't know how to "link" my Java project to the appropriate Eclipse JAR files. The only way I know I can do it is by actually writing an Eclipse plug-in, and have the plug-in installed on RAD - thereby "contributing" Ant tasks to the workbench; however that sounds like an overkill.
Isaac
I'm going to add another answer.
Ladlestein
+1  A: 

So, you want an Ant task that runs within Eclipse. And as you say, it's clear that those tasks are using the Eclipse API. As of this moment, I don't really understand how they're doing it. I've looked at the source of a couple of them and I still have questions.

To find the locations of all the Ant tasks contributed by Eclipse, do a Plug-in Search for org.eclipse.ant.core.antTasks. When I do that, I see twenty or so extensions, many of which define multiple tasks.

If you get the source bundle for a plugin that contributes one of these tasks, you can look at the source for it. RefreshLocalTask is in org.eclipse.core.resources; unfortunately, when I import this bundle into my workspace as a source project, the source for the Ant tasks doesn't get linked correctly. There is a separate jar (inside the bundle) for them, and, while the source is in the bundle, it's not clear how the jar is compiled. The upshot is that I don't have the Ant task source compiling in my workspace.

You can also Google for the Ant task source; here's the 3.6.0 source for RefreshLocalTask.

Anyway, in the source you can see calls to org.eclipse.core.resources.ResourcePlugin that are illustrative of what you probably need.

Ah, I see in the extension point description - right-click on one of those search results and choose "Show Description", or go there from the Manifest editor - that there is a flag you can set, "eclipseRuntime"; the text implies that if it is set, Eclipse will launch the task in the same VM.

Ladlestein
This sounds on the right track to me. Create a new Eclipse plug-in (so create a new Plug-In Project, not just a Java Project). Add dependencies (open up the `MANIFEST.MF`) on the (existing) Eclipse plug-ins you want to call. You can edit the dependency declarations to give you "wiggle room" in versions, if you want. (Note that Eclipse is well-separated into 'core' vs. 'ui', so you should be able to do whatever you want without having to bring up or interact with any UI.) Have your new plug-in contribute an extension to the extension point Ladlestein identified. Good luck!
Woody Zenfell III
I just reached the conclusion, a few minutes ago, that what I want to achieve really can't be achieved by just dropping a simple JAR; whatever works under Eclipse or with Eclipse has to correspond to OSGi and how plugins/features are designed - so I should be no exception. Thank you both!
Isaac