I am building an annotation processor plugin for eclipse,
what i would like to do is to examine several files inside the project folder during the processing.
I would like to know how can I get the project path from within my processor.
I believe this can be done because the project source path is provided to the processor - but I cannot find a way to reach it.
I tried looking at the System.properties and at the processingEnv.getOptions() but there is no useful information there..
eventually I would like to use this annotation processor on Netbeans too so if there is a public API that can provide this information it will be the best - but any help will be appreciated..
The processing environment provides you with a
Filer that can be used to load (known) resources. If you need absolute paths to discover files or directories, you can use a JavaFileManager and a
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); Iterable<? extends File> locations = fm.getLocation(StandardLocation.SOURCE_PATH);
If you are using Eclipse, you need to configure it to use the JDK as runtime as bennyl pointed out in the comments.
It seems that there is no API that is obligated to return the source location, so the solution above won't work reliably and only with some environments. The Filer for example is only required to support
The easiest workaround is probably to assume/require a specific project structure, where the compiled classes are in a direct subdirectory of the project (which is default for most IDEs
bin or Mavens
target directory). If you do that, then you can create a temporary resource file with the Filer at the
SOURCE_OUTPUT location as and get the path from that file.
Filer filer = processingEnv.getFiler(); FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "tmp", (Element) null); Path projectPath = Paths.get(resource.toUri()).getParent().getParent(); resource.delete();