0

I have a maven project and I want to create custom annotations to use in it.

I need that those annotations only be processed when some parameter is present, because I want to launch it a jenkins task.

How can I achieve it? Googling, I thought about creating a custom maven plugin for it, but I don't know how process the annotations in it.

This it is totally new for me and I would like your advice.

EDIT: I've tried creating a new maven plugin and with the reflections library find all classes with the annotation, but it isn't finding anything.

EDIT2: Following steps @Praveen Kumar, I was able to create a Custom Annotation Processor which process my method annotations. But I don't know how to execute the annotated method(I don't know either if this can be done)

@SupportedAnnotationTypes({"*"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class MyProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement annotations, RoundEnvironment roundEnv) {
        for(Element el : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
            // Execute annotated method???
        }
    }
}

PS. Sorry for my english.

Diego.

niegus
  • 1,698
  • 4
  • 22
  • 34
  • maybe [here](http://hannesdorfmann.com/annotation-processing/annotationprocessing101/) is some help – Hendrik Jander Feb 03 '16 at 08:13
  • Please post some examples of what you are trying to do , what you have tried so far. SO is not a coding service – coder3521 Feb 03 '16 at 08:13
  • It highly depends on how you want to process the annotation, and which version of Java you are using. If you want to do annotation processing against source code (usually for code generation), then in older version of JDK you need to use apt and the annotation processing API changed from JDK6 (or 7?). THat's probably why reflection will not work because it is still in source processing phase. If you need to process annotation for other purpose, then you will need to use other technique (e.g. reflection, but in later build phase in Maven) – Adrian Shum Feb 03 '16 at 10:17
  • Tell us precisely by example what do you want to do with annotation, so people can give you proper suggestion – Adrian Shum Feb 03 '16 at 10:18

1 Answers1

0

Solution to your question

  1. Please use maven processor plugin for processing your custom annotations.
  2. you can get maven processor plugin from this maven repository
  3. Also, this example will help you a lot.
  4. Once your maven is complete/perfect in all aspects, you can launch in Jenkins by setting your build goals.
  5. Alternatively you can use mojo plugin to process/execute your custom annotations. please check this link if it of any help to you.
Community
  • 1
  • 1
Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31