2013-09-05 4 views
1

일부 맞춤 주석이 적용된 클래스가 포함 된 패키지가 포함 된 "간단한 프로젝트"가 있습니다. 현재 주석이 달린 클래스에 대한 "간단한 프로젝트"를 검사하고 이름과 관련 패키지를 반환 할 수있는 maven 플러그인을 개발 중입니다.Maven 플러그인에서 주석이 달린 클래스를 검사합니다.

exemple 들어

: 간단한 프로젝트/SRC/메인/자바 /을 myApp/도메인/엔티티와 Entity.java는 @MyCustomAnnotation 로 주석하고 "내-받는다는 - 플러그인"프로젝트는 "간단한 프로젝트에 선언 MVN 발전기를 "나는 실행하는 경우의 pom.xml은

그래서,"정말 일을 거의 내가 인터넷에서 찾을 수있는 모든하지만 아무것도 시도 myApp.domain.Entity

: myGoal을 "은이 내용을 가진 파일을 생성한다 . 목표를 실행할 때마다 플러그인에서 자체 클래스를 검색하지만 내 간단한 프로젝트 클래스는 검색하지 않습니다.

도움을 주시면 감사하겠습니다.

감사합니다.

답변

2

해결책을 찾았습니다. 모든 파일을 찾으려면 재귀 함수를 사용하여 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); 

나는 그 파일의 경로를 얻을 후 나는 관련 FileInputStream에 구문 분석 com.google.code.javaparser를 사용 : 내 모조에서의 SourceDirectory을 얻는 방법은 다음과 이다.

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 
        } 
       } ... 
관련 문제