Writing an Eclipse-aware Ant task - eclipse

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"?

I think this article quite much addresses your problem: http://www.ibm.com/developerworks/rational/library/09/eclipsecustomanttasks/
It is targeted to the IBM Rational Application Developer, but that one is based on Eclipse and AFAIS there is not overly much non-eclipse-specific happening here, at least not regarding the specifics about how to make the task available in Eclipse (writing the plugin definition etc).

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.

You should probably say more about what you want to do, because there are several routes you can take.
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.
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.
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.

Related

How to Create a Spring+Primefaces+Hibernate (no maven) project in eclipse?

I am new to J2EE. I would like to create a Spring+Primefaces+Hibernate project.
I googled for it.
But I found all projects examples show in internet contains maven. My questions are
Is it possible to create a spring+primefaces+hibernate project in eclipse without Maven? If no, what is need of maven?
How to add the jar file of primefaces and spring and hibernate in eclipse?
Will the spring controller xml file (spring context or dispatcher servlet) be created automatically or manually?I mean Spring MVC.
Will the hibernate file (mapping file) also be created automatically or manually?
If possible, can anyone guide me to tutorial (preferably video) to implement the same?
I am using tomcat 7 and Eclipse - kepler.
Any help is appreciated.
If this is downvoted , do specify the reason also.
Although it's not a 'must' to use Maven or any other build tool, you should strongly consider using one.Eclipse Kepler has by default maven support but feel free to use other build tools(Gradle, Ant) or none(see 2.).Maven and the other build tools remove the headache of scaffolding, searching for dependencies(external jars like spring-mvc, hibernate, some db drivers), even deploying applications in a server.
If you chose not to use a build tool you have to manually get your project dependencies and enter them
into your project's buildpath(Right Click -> Build Path then enter their location).As you have noticed this step can be really really time consuming...
No, you have to manually create the configuration unless you use another project that already has what you need, again this might get easier with a build tool(maven archetypes for example)
The same as 3.
You won't have a hard time finding resources about these technologies, they are being used practically everywhere, and I think the Spring team has some videos in their YouTube channel.
Hope that helps a little!
1:* The fundamental difference between Maven and Ant is that Maven's design regards all projects as having a certain structure and a set of supported task work-flows (e.g., getting resources from source control, compiling the project, unit testing, etc.). While most software projects in effect support these operations and actually do have a well-defined structure, Maven requires that this structure and the operation implementation details be defined in the POM file. Thus, Maven relies on a convention on how to define projects and on the list of work-flows that are generally supported in all projects.
This design constraint resembles the way that an IDE handles a project, and it provides many benefits, such as a succinct project definition, and the possibility of automatic integration of a Maven project with other development tools such as IDEs, build servers, etc.
But one drawback to this approach is that Maven requires a user to first understand what a project is from the Maven point of view, and how Maven works with projects, because what happens when one executes a phase in Maven is not immediately obvious just from examining the Maven project file. In many cases, this required structure is also a significant hurdle in migrating a mature project to Maven, because it is usually hard to adapt from other approaches.
In Ant, projects do not really exist from the tool's technical perspective. Ant works with XML build scripts defined in one or more files. It processes targets from these files and each target executes tasks. Each task performs a technical operation such as running a compiler or copying files around. Targets are executed primarily in the order given by their defined dependency on other targets. Thus, Ant is a tool that chains together targets and executes them based on inter-dependencies and other Boolean conditions.
The benefits provided by Ant are also numerous. It has an XML language optimized for clearer definition of what each task does and on what it depends. Also, all the information about what will be executed by an Ant target can be found in the Ant script.
A developer not familiar with Ant would normally be able to determine what a simple Ant script does just by examining the script. This is not usually true for Maven.
However, even an experienced developer who is new to a project using Ant cannot infer what the higher level structure of an Ant script is and what it does without examining the script in detail. Depending on the script's complexity, this can quickly become a daunting challenge. With Maven, a developer who previously worked with other Maven projects can quickly examine the structure of a never-before-seen Maven project and execute the standard Maven work-flows against it while already knowing what to expect as an outcome.
It is possible to use Ant scripts that are defined and behave in a uniform manner for all projects in a working group or an organization. However, when the number and complexity of projects rises, it is also very easy to stray from the initially desired uniformity. With Maven this is less of a problem because the tool always imposes a certain way of doing thi
2:* You have to download all required jars file for hibernate/spring/primefaces from internet and place them in your project build path or in lib folder.
3:* Spring configuration files need to be created by you so that you can get the concept.
4:* Hibernate mapping files can be created by using reverse Engineering techniques for hibernate from where you can generates hbm files or you can use annotations if you dont want xml.
I suggest you to first create a sample java project in eclipse then download all required jars and place them in lib folder. Then configure hibernate in projects and spring integration.

How to get Eclipse to create bin/main and bin/test

I want my Ant build to take all Java sources from src/main/*, compile them, and place them inside bin/main/. I also want it to compile src/test/* sources to bin/test/. I wan this behavior because I want to package the binaries into different JARs, and if they all just go to a single bin/ directory it will be impossible* (extremely difficult!) to know which class files belong where.
When I go to configure my build path and then click the Source tab I see an area toward the bottom where it reads Default output folder: and then allows you to browser for its location.
I'm wondering how to create bin/main and bin/test in an existing project without "breaking" Eclipse (it happens). I'm also wondering that if I just have my Ant build make and delete those directories during the clean-n-build process, that Eclipse might not care what the default output is set to. But I can't find any documentation either way.
Thanks in advance for any help here.
In Eclipse, you can only have one output folder per project for your compiled Java files. So you cannot get Eclipse to do the split you want (i.e. compile src/main to bin/main and src/test to bin/test).
You can, if you want, create two Eclipse projects, one main project and one test project, where the test project depends on (and tests) the main project. However, in that case, each project should be in its own directory structure, which is not what you are asking for. But this is a common approach.
Another way, which I would recommend, would be to not mix Ant compilation and Eclipse's compilation. Make the Ant script the way you describe (i. e. compile the main and test directories separately and create two separate jar files). Change the Eclipse compile directory to something different, for instance bin/eclipse. Use the Ant script when making official builds or building for release. Use Eclipse's building only for development/debugging. This way, your two build systems will not get in each other's way and confuse each other.
Hope this answers your question and I understood it correctly. Good luck!

Compare to the IDE for Java,do we still need Ant?

Not long before I had digged into Ant and use it doing simple practice(compile,unit test,package).Now my usual work is under Eclipse for java development,the usual work can all be simply done through Eclipse' function:in-time compiling、click button to deploy、various wizard..
So I am wondering under Eclipse do we have a need for Ant.
And if it is, at which aspect should we use Ant along with Eclipse.
Building applications is not about just building them in your IDE. You need to setup an automated build and deploy method that can be run for, say, continuous integration and deployment. The built package has to be deloyed to various environments and finally production. You cannot build them out of your own box. You cannot build it out of your IDE. You need separate build system. That is where build tools like ANT come in.
Also note that the capabilities of IDEs for doing build, deploy is limited and that is not their strength. It is, however, possible to make use of ANT and its build scripts and run them out of eclipse. This way, you have a single set of scripts for both your local ( developer ) build and the main build process. With ANT being JAVA based, you can also suit your build tool to your needs by writing custom tasks etc.
Also, when you make use of something as standard as ANT for your build system, you can move to a different IDE like Intellij IDEA and still make use of the build system that you know.
Do you need Ant if you can build through the IDE? Of course not! You can see that yourself.
Should you still have a IDE independent build with standard tools? Of course!
Note I didn't say Ant. There are a number of build tools such as Maven or Gradle that are made to build Java programs. However, there are reasons why you want an IDE independent build system:
You have a Continuous Integration Server: You do have one. Right? If you don't get one. A Continuous Integration Server automatically builds your project (and runs every test) each time a change is committed to the version control system. It catches mistakes. It also provides an OFFICIAL BUILD. No more It worked on my machine! excuses. If the Continuous Integration Server can't build it and successfully run the tests, the software isn't good.
You have others who need to work with your project on a different computer in a different environment: The problem with IDEs is that its specific to your system. Someone else checks out your project, and they may be using a different IDE. They maybe have a slightly different setup. An independent build scripting language like Ant guarantees that they can rebuild your project as intended.
If you share a project with someone else, you need an independent standard method way of being able to build that project and to know exactly what is required for that build to work. IDEs can hide that information.
I am a benchmark tester using Ant everyday. Our environment has 7 test machines and 1 console machine. For each test run, the ant script updates the working copy, restarts the database, build the product and the Grinder with proper properties and deploy them to the machines, and starts them and the monitoring softwares. With the 1000-line-buildfile, I can just type in an "ant A B C D E"-like line in the terminal on the console machine. I don't think it's a good idea to do this in a IDE like eclipse or NetBeans or anything else.
Also, in my company's product, ant is widely used. We use ant to build sample data for the benchmark test; we use ant to update Tomcat which is going to be bundled with our product; we also use ant to build and run selenium tests.
What's more, when you want to release the source code, including the IDE project files is not a good idea because there are a lot of IDEs. And what if the user just want to get a fresh build from your latest code (if you don't provide nightly build)? Providing a set of ant (or other build tool) buildfiles is a good idea. With this, you can manage your classpath for the build.
The shortest answer here wold be flexibility. The IDEs may came with pre-defined functionalities and may even allow you to choose some options using wizards. It is like generating html with Dreamweaver or such tools. It will never be as flexible as the pure thing. For example NetBeans uses Ant underneath.
Quoted from: Netbeans: Build Tools
The default NetBeans IDE project system is built directly on top of
the Ant build system. All of the project commands, such as "Build
Project" and "Run File in Debugger", call targets in the project's Ant
script. You can therefore build and run your project outside the IDE
exactly as it is built and run inside the IDE.
Even after you use tools for that purpose you are most likely still to brush up. So, what I am saying is, what the IDEs do for you is only a subset of what you can do with the pure ant or maven( if you use it). For simple tasks such as compile and run, IDEs do pretty much a good job! But if you have a use case as complex as deploying on CI IDEs won't help you. You need the whole flexibility of your build tools.

How to share a common customBuildCallbacks script between multiple Eclipse plug-ins

I am trying to avoid duplicating customBuildCallbacks.xml for all my plug-ins, when called from either PDE's headless build or the Eclipse GUI. I have in customBuildCallbacks.xml steps to generate code or modify the plug-in packaging that I:
obviously want to run with the headless build
would also like to run either when I export this plug-in from the GUI or from an external builder watching specific files in my plug-in
The headless build runs in a well defined environment, so I can set customBuildCallbacks.buildpath or even use a relative path in each plug-in's build.properties to point at my common customBuildCallbacks.xml. However, this is trickier from the GUI: that path is different for every developers since we do not have sources in the Eclipse workspace (we import projects from various locations due to our internal build process). I was hoping for every project to have an associated ant property holding its path on disk, similar to the ${workspace} variables exposed in various dialogs. I couldn't find anything useful though.
Does anyone have any experience doing this kind of things?
Thanks,
Romain

Automation in Eclipse

I am using Eclipse and have a few simple tasks(such as uploading files to a server) I would like to automate. Where should I be looking, is something like ANT applicable here?
Any pointers appreciated.
You can use the external tools of eclipse (see eclipse help).
As stated in "FAQ How do I add my own external tools?":
External tools are applications or scripts that typically act as extensions to your development environment. For example, they may be used to execute scripts to package and deploy your application or to run an external compiler on your source files.
External tools allow an end user to achieve a basic level of integration for a non-Eclipse-aware tool without writing a plug-in.
External tools are created and configured via Run > External Tools > External Tools or from the drop-down menu on the Run button with the toolbox overlay.
That way, your script (.bat, ant, maven, whatever you want) can be run with a simple click on the external tools menu.
You can indeed associate an external tool to ant, since:
The Ant UI as provided in Eclipse comes with a first-class Ant build-file editor, including syntax highlighting, Content Assist, templates, and content formatting.
This editor is automatically associated with files named build.xml and can be associated with other file names from the Workbench > File Associations preference page.
The Ant UI also has a wizard for running Ant scripts and a toolbar button for repeating previous builds.
(source: eclipse.org)
Maven is another solution. There are many mojos (custom plugins) for existing tasks that are highly configurable, or if you get stuck you can write your own.
The trade off is the setup and learning curve can be fairly steep, so I guess it boils down to what you need it for, if you'll reuse it, etc, etc.
Eclipse Monkey is an atempt to make Eclipse scriptable to automate tedious tasks.