maven - Java Annotation processor for remote JAR -
general question
i have 2 projects , b; b has dependency on a. want generate code in b annotation processor, based on annotations on objects in a. when run compilation correct processor implementation, annotated objects b picked up.
i understand scanning other jars must disabled default, because don't want annotation scan dependencies. understand may impossible want because of compiler magic - don't know lot - i'm hoping it's not.
specific case
my projects called db , web. web depends on db jpa access; configured in maven. due number of architectural choices, db must remain separate jar. db doesn't use spring except annotations consumed web; web uses spring mvc.
i'm trying generate crudrepository
interfaces jpa entities annotation processor. @repository
objects supposed go in repo
package in web project, can used @autowired
wherever in web application. annotation i'm performing scan @javax.persistence.entity
, i've tried custom annotation, same results.
@supportedannotationtypes("javax.persistence.entity") @supportedsourceversion(sourceversion.release_8) public class repositoryfactory extends abstractprocessor { @override public boolean process(set<? extends typeelement> annotations, roundenvironment roundenv) { (element e : roundenv.getelementsannotatedwith(entity.class)) { if (e.getkind() != elementkind.class) { continue; } // todo: implement logic skip manually implemented repos try { string name = e.getsimplename().tostring(); typeelement clazz = (typeelement) e; javafileobject f = processingenv.getfiler(). createsourcefile("blagae.web.repo." + name + "repo"); try (writer w = f.openwriter()) { printwriter pw = new printwriter(w); pw.println("package blagae.web.repo;"); pw.println("import org.springframework.data.repository.crudrepository;"); pw.printf("import %s;\n", clazz.tostring()); pw.println("import org.springframework.stereotype.repository;"); pw.println("@repository"); pw.printf("public interface %srepo extends crudrepository<%s, long> {}\n", name, name); pw.flush(); } } catch (ioexception ex) { logger.getlogger(repositoryfactory.class.getname()).log(level.severe, null, ex); } } return false; } }
ideally, i'd love tell me annotation simple as
@componentscan(basepackages = "blagae.db.*")
but of course, i'm not counting on because documented somewhere. workaround, add spring dependency db , generate classes there, serve purpose in spring mvc app. i'm wary of config might take make work.
update
some info: i'm using maven-processor-plugin, i've verified work in web project classes defined there. however, want access classes annotated in dependency project db. have looked method abstractprocessor::getsupportedoptions
it's unclear me there.
maven config:
<plugin> <groupid>org.bsc.maven</groupid> <artifactid>maven-processor-plugin</artifactid> <version>2.2.4</version> <configuration> <processors> <processor>blagae.utils.repositoryfactory</processor> </processors> </configuration> <executions> <execution> <id>process</id> <goals> <goal>process</goal> </goals> <phase>generate-sources</phase> </execution> </executions> </plugin>
suggestion
another random thought had run javacompiler
process db project in web, how inject processor
?
annotation processor works on compilation phase of project (web in case) , compiler compiles project. dependencies of current project compiled , compiler (and result annotation processor) don't touch (or have no access) third party libraries (db).
you can try extract annotation processor separate project/jar , use in web , db projects. in case annotation processor create crudrepository
on compilation phase of concrete project. , generated classes in db project available in web.
Comments
Post a Comment