I have an own annotation processor (let's call it MyProcessor) and a project (let's call it MyProject) which uses the processor by
passing -pr开发者_运维知识库ocessor
to javac
.
Now I need MyProcessor to produce some output and make it available for MyProject.
I have following options (and problems):
Let MyProcessor write a file to the path, specified by the property
Problem: from the point of view of MyProcessor,user.dir
.user.dir
is always my home dir, not the path of MyProject.Pass the current directory of MyProject to MyProcessor using
Problem: It's an ugly hard-coded path:javac
's-A
option./some/path/to/MyProject/
.Let MyProcessor generate some source files, which then would be compiled by
Problem: It's too complex for such an easy (?) task.javac
together with MyProject, so that MyProject can refer to this compiled class and retrieve data from it.What other options are there?
Can someone please suggest, how to proceed?
Processor.init()
method (which you've implemented) is invoked with ProcessingEnvironment as parameter which, in turn, has a getFiler()
method returning a Filer instance.
You should be using the createResource()
method of the Filer
(assuming the output being generated is neither class nor source; otherwise use appropriate create
method for that) and write your output to either class or source locations (former is probably preferable, but it depends on what you're doing). Both are overridable via command-line switches if need be, but are well-defined as they are to be used in a build process.
精彩评论