Processing Annotations Maven - java

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.

Solution to your question
Please use maven processor plugin for processing your custom annotations.
you can get maven processor plugin from this maven repository
Also, this example will help you a lot.
Once your maven is complete/perfect in all aspects, you can launch in Jenkins by setting your build goals.
Alternatively you can use mojo plugin to process/execute your custom annotations. please check this link if it of any help to you.

Related

How to add additional chrome options in bobcat automation framework

I have a simple maven bobcat project with YAML configuration. I am now truing to add additional property implementing WebDriverCreator interface. I have add necessary methods (create(), getId() and getOptions()) in the implemented class "CustomChromeCreator".
However when I have tried to execute it I am getting error "No WebDriverCreator registered for the provided type: customChrome".
Can anyone provide me a sample codebase how to do it.
NOTE: I am using bb-core version 2.2.0

How to inject Beans inside a test class properly with spring boot and annotations

I have a class I want to test if it uses the right Beans when a certain profile is active. Therefore I have written a test class, with the profile active, for the DomesticService (which in turn uses the GardeningService and CleaningService, all of it is autowired).
#Component
public class HumanDomesticService implements DomesticService {
private CleaningService cleaningService;
private GardeningService gardeningService;
private Logger logger;
HumanDomesticService() {
}
#Autowired
public HumanDomesticService(CleaningService cleaningService, GardeningService gardeningService, Logger logger) {
setCleaningService(cleaningService);
setGardeningService(gardeningService);
setLogger(logger);
}
I made a test configuration class, which should scan the whole project for Beans, since the SpringBootApplication annotation includes the ComponentScan annotation.
#SpringBootApplication
public class ActiveProfileConfig {
}
Yet my test class can't seem to find the right Beans to complete the test.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'be.mycompany.springlessons.housekeeping.domestic.service.ActiveProfileSmallHouseTest': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'be.mycompany.springlessons.housekeeping.domestic.service.DomesticService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
After that I tried to make Beans inside my configuration class, which makes my tests succeed, but then Maven complains about finding 2 Beans which could be injected there. Maven seems to be able to see all the beans through the ComponentScan.
Finally I ended importing the necessary classes, which works for both my tests and Maven, but it just doesn't seem to be the right and best solution.
#SpringBootTest(classes = ActiveProfileConfig.class)
#ActiveProfiles(profiles = "smallHouse")
#Import({HumanDomesticService.class, HumanCleaningService.class, RobotCleaningService.class, HumanGardeningService.class, HedgeTrimmerFactory.class, Broom.class, VacuumCleaner.class,
Sponge.class, DisposableDuster.class, LawnMower.class, LoggerFactory.class})
public class ActiveProfileSmallHouseTest {
#Autowired
private DomesticService service;
I have tried to search on the internet for another solution and saw I wasn't the only one with the problem, but no other solution seemed yet to have worked.
What is the reason ComponentScan doesn't seem to work in a test class and how best to solve this?
I wanted to share our final solution on this.
We both use IntelliJ IDEA as IDE. In this IDE we didn't specify that the tests shouldn't use the module path. IntelliJ now has a setting that lets you enable/disable the usage of the module path for Unit tests.
In maven we already disabled this by configuring the main surefire plugin to not use the module path.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
With this 'new' feature in our IDEA, we also have to do disable this so our tests don't use the class path.
We figured it out because our Maven phase : mvn test
Did run the tests.
Now all the tests run smoothly with just the #SpringBootTest annotation.
To set this in IntelliJ :
in the menu bar : Run > Edit Configurations
in the left menu open the sub menu of Templates > JUnit
uncheck the checkbox next to Use module path
click apply > OK
From now on, all the tests you will run will not use this module path.
If you already have existing tests. Select them win the menu on the left, and uncheck the Use module path option for all of them.
I have had the exact same problem for a long time. I was able to solve this in Spring Boot by adding the main configuration I wanted to use. and all the needed dependencies my test had (All AutoWires) to the classes field of the #SpringBootTest annotation.
#SpringBootTest(classes = {ActiveProfile.class, HumanDomesticService.class, HumanCleaningService.class, RobotCleaningService.class, HumanGardeningService.class, HedgeTrimmerFactory.class, Broom.class, VacuumCleaner.class,
Sponge.class, DisposableDuster.class, LawnMower.class, LoggerFactory.class})
#ActiveProfiles({"smallhouse"})
public class ActiveProfileSmallHouseTest {
#Autowired
private DomesticService service;
...
}
If you weren't using Spring Boot. You could solve this with the #ContextConfiguration annotation. Which works in the same way and is a part of the #SpringBootTest annotation as well.
This is also how it is being done in my main learning source: Pro Spring 5 - Iuliana Cosmina et al - Apress (page 631)
I still don't know why the ComponentScan or automatic loading of the context doesn't work. But at least I know that this way my tests will always run. Also, you are able to run only the classes you need to get this test succeeding, and don't need to fire up the entire context for the test.

#CustomFormAuthenticationMechanismDefinition annotation missing

I'm trying to implement a custom authentication module in jee using java 11 but when I add the annotation #CustomFormAuthenticationMechanismDefinition to configuration class Eclipse complains that the annotation doesn't exists.
Do somebody already had this problem?
Yes I had the same problem. Reason was I did not have the jar file containing the annotation on my IDE (design time) classpath.

Spring dependency injection from webapp to external jars

I am integrating Java Plugin Framework within a Spring based web application (XML-free).
Everything is fine, except for the dependency injection in plugin context
For instance I have a data source I would like to use in a plugin without having to go back to property files by using #Autowired like for the rest of the application
I cannot find a way to do this except by using getBean, which I read was not the best practise on this subject.
I also had a look at LogicalDoc but this project resorts to properties reloading which is not the correct solution for me as I want beans attributes modifications to be available without further glue.
Does anyone know of an existing open source project where both these environment are used ?
Not being able to inject dependencies in plugins, I finally added the following methods to mother class of all plugins
public void setContext(ApplicationContextProvider a_ctx) {
m_theContext = a_ctx;
}
public ApplicationContext getApplicationContext() {
return m_theContext.getApplicationContext();
}
And retreive the beans from within the plugin by
getApplicationContext().getBean(*ClassType*)

Do plexus annotations work in a maven plugin?

I'm working on a custom maven plugin and I'm trying to use the Plexus annotations for dependency injection as shown on the Sonotype blog.
I have a field defined in my mojo:
/**
*/
#Requirement(hint = "rhino")
private RhinoRunner rhinoRunner;
And the class defined with the #Component annotation:
/**
*/
#Component(role = RhinoRunner.class, hint = "rhino")
public class RhinoRunnerImpl implements RhinoRunner {
I then added the configuration to the components.xml. When I use the plugin the rhinoRunner field is null. If I change to use the old javadoc taglet on the dependency, like follows, it works correctly:
/**
* #component
*/
private RhinoRunner rhinoRunner;
Is it possible to make the Java5 annotations work?
The plugin annotations differ from the plexus annotations (though the plugin annotations happen to use the same mechanism to fulfil a requirement).
You should continue to use the plugin annotations in a Mojo, but feel free to use the other annotation in pure components (in the plugin or any supporting libraries).

Categories