2013-09-05 161 views
1

我有一個「簡單的maven項目」,其中包含一些包含一些自定義註釋類的包。 我目前正在開發一個maven插件,可以掃描我的「簡單項目」爲那些註釋類,然後返回他們的名字和相關的包。從maven插件掃描自定義註釋類

對於爲例: 簡單的項目/ src目錄/主/ JAVA /對myApp /域/ Entity.java與實體標註有@MyCustomAnnotation 和「我 - Maven的插件」項目是「簡單的項目申報「pom.xml中

所以,如果我跑‘MVN發生器:myGoal’它應該產生與此內容的文件:myApp.domain.Entity

我幾乎嘗試了所有我在互聯網上找到,但沒有什麼工作。 每當我運行我的目標,我的插件掃描它自己的類,但不是我的簡單項目類。

任何幫助,將不勝感激。

謝謝。

回答

2

我找到了解決方案。 我使用遞歸函數在sourceDirectory上循環查找所有文件。 這裏是我從我的魔力得到sourceDirectory:

/** 
    * Project's source directory as specified in the POM. 
    * 
    * @parameter expression="${project.build.sourceDirectory}" 
    * @readonly 
    * @required 
    */ 
    private final File sourceDirectory = new File(""); 
    fillListWithAllFilesRecursiveTask(sourceDirectory); 

我獲得這些文件的路徑,然後我用com.google.code.javaparser解析相關的FileInputStream。

public void fillListWithAllFilesRecursiveTask(final File root) { 
        CompilationUnit cu; 
        FileInputStream in; 
        try { 
         // we looped through root and we found a file (not a directory) 
         in = new FileInputStream(file); 
         cu = JavaParser.parse(in); 
         new MethodVisitor().visit(cu, file); 

最後,我延長VoidVisitorAdapter獲得批註和會員...等

private static class MethodVisitor extends VoidVisitorAdapter<File> { 

     @Override 
     public void visit(ClassOrInterfaceDeclaration n, File file) { 
      if (n.getAnnotations() != null) { 
       // store classes that are annotated 
       for (AnnotationExpr annotation : n.getAnnotations()) { 
        //here some logic 
        } 
       } ...