问题
I have an eclipse Java project and want to get information like project source dir, classpaths, etc. My current implementation parses the .project
file. But as I didn't find any official documentation describing the structure of the .project
file, I have some concerns for the robustness of this approach.
A more convenient and robust way would be to use JDT (headlessly) to load the project and get the relevant information from the IJavaProject
object.
Although the developer guide of JDT says
JDT Core packages give you access to the Java model objects and headless Java IDE infrastructure.
all the examples I can find opening an existing Java project get the IJavaProject
object from projects within a workspace
or use an IProject
object. But I couldn't find the way to add a project to the workspace
or to construct a IProject/IJavaProject
from a path to .project
file.
Could anyone please help?
回答1:
Yes, and can all be done through clear and stable API. org.eclipse.jdt.core
is a plug-in, just like org.eclipse.core.resources
(which is where you would get an IWorkspace
instance), and they both expect to be running within an Eclipse runtime, which can be headless if that's how you write your Eclipse Application. JDT uses the .classpath
file to record where sources, libraries, and build output are, and what abstracted references to libraries to use, while the .project
file is what records what kind of project it is in general--Java, PHP, Web, some combination of those or others--and a little more information about what builders to execute.
So make yourself a headless Eclipse Application, or package your end-goal functionality inside of one.
https://wiki.eclipse.org/FAQ_What_is_an_Eclipse_application%3F https://wiki.eclipse.org/FAQ_What_are_extensions_and_extension_points%3F http://help.eclipse.org/mars/topic/org.eclipse.platform.doc.isv/reference/extension-points/org_eclipse_core_runtime_applications.html?cp=2_1_1_27
http://help.eclipse.org/mars/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/ResourcesPlugin.html#getWorkspace-- http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/JavaCore.html#getJavaCore-- http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/IJavaProject.html#getResolvedClasspath-boolean-
回答2:
See http://www.ant4eclipse.org/ for a project which allows you to work inside Eclipse projects.
I used it for a bit some years back, and found that this is too brittle for long term usage and build scripts. I would recommend against relying on internal Eclipse things. They break! Typically when mixing releases.
Instead I would suggest you move to Maven. This is a bit more work, but allows you to use the whole maven infrastructure and API's to do things. What you want to achieve here, may be possible already using a Maven plugin.
来源:https://stackoverflow.com/questions/37776230/is-it-possible-to-load-an-eclipse-java-project-using-jdt-in-the-headless-mode