Reputation: 3095
I want to list out all files inside a java project using jdt, including jsps, xml files, etc.
I have tried the below code, but it returns java resources only (including class files, which I don't require).
List<String> resourceNames = new ArrayList<String>();
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (IProject project : projects) {
if (project.isOpen() && project.isNatureEnabled(JavaCore.NATURE_ID)) {
for (IResource resource : project.members()) {
resourceNames.add(resource.getName() + "-" + resource.getFullPath().toString());
}
}
}
Can anyone please point me in the right direction.
Upvotes: 2
Views: 3374
Reputation: 111142
'members()' does return everything which is directly in the current container. To see all files you need to look in the folders in each container as well.
Something like:
void processContainer(IContainer container)
{
IResource [] members = container.members();
for (IResource member : members)
{
if (member instanceof IContainer)
{
processContainer((IContainer)member);
}
else if (member instanceof IFile)
{
... deal with the file
}
}
}
Since IProject
extends IContainer
just call
processContainer(project);
to look at everything.
IContainer
(actually IResource
) also has several accept
methods to traverse the container contents calling a callback.
Upvotes: 8