How to configure Java Annotation Processor(s) in Eclipse? - java

I have successfully run annotation processor for CLASS level retention annotations via command prompt compiling using Java 8.
However, when I tried to configure the annotation processor in eclipse and tried to run it with "-proc:only" option, it didn't get effective.
I have included the Jar file containing the custom annotation processor class file into the Project Properties -> Annotation Processing -> Factory Path. I have also provided the -proc:only option in Project Properties -> Annotation Processing -> Processor Options, still the annotation processor isn't getting called when a class containing my annotation is executed.
Please help me identifying the required setting or mistake or additional step for running the annotation processor via eclipse.

I was finally able to enable my own annotation processor by adding my jar file.
Right click on the project and select Properties.
Open Java Compiler -> Annotation Processing. Check "Enable annotation processing".
Open Java Compiler -> Annotation Processing -> Factory Path. Check "Enable project specific settings". Add your JAR file to the list.
Clean and build the project.
And it finally worked.

It is very much straightforward provided that whatever APT plug in you are trying to use is correctly configured.
Follow the steps mentioned in above post and the reference image to visualize it. Image showing QueryDsl APT configuration for maven project.

While compiling via command prompt or terminal, you can see all the logs in same screen after the compilation command. In eclipse, these logs can be seen at
Window->show view->General->Error Log
If you want the IDE(Eclipse) to point out the warning, error or other diagnostic message exactly at the particular element in the code editor, we have to call the printMessage method of javax.annotation.processing.Messager with 3 arguments as shown below.
messager.printMessage(Kind.ERROR, "Error Message", element);

Related

Is it possible to use an init.gradle script in IntelliJ Idea?

I'm using the Gradle wrapper in a project in Idead. I have an init.gradle file in a non-standard location and need to find a way to specify it in Idea. I thought this could be done via the Gradle VM options setting field and entered --init-script /path/to/init.gradle in it, but this throws an Unrecognized option --init-gradle /path/to/init.gralde error when trying to re-import the project.
Is there some other way to do this that I'm not aware of?
You can set Gradle commandline options/arguments from the "Run" configuration panel:
If you set this option on the "Templates>Gradle" panel it will apply to all Gradle tasks invocation ( note that you need to execute tasks with Right-click/Run from the Gradle tool window: double-click will not take the option into account..)
EDIT : another way is to add the commandline options directly in the "Run gradle task" window:

Display custom archetype properties in Intellij "create project from archetype" wizard

I have a custom maven archetype to define an internal company project structure.
This archetype has some required additional properties. E.g.,system-code
<archetype-descriptor>
<requiredProperties>
<requiredProperty key="system-code"/>
...
</requiredProperties>
...
</archetype-descriptor>
If I run the artifact through the command line:
$ mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=...
The custom property is requested so the user doesn't need to know that this property exsists before running the mvn archetype:generate plugin.
$ Define value for property 'system-code':
What I want to do is something similar when I create a new project from the archetype in Intellij-idea. Following the advices of other SO questions, I've created an artifact-catalog and added the plugin maven artifacts catalog to Idea. So far all is working fine and I can create a new project from the archetype.
However, in the archetype properties dialog, there is no trace of the custom property system-code, and in some use cases the user doesn't have to know this property is required.
If I go ahead creating the project, I expected that maybe the system-code property would be requested in the command line, but that's not the case and the property creation fails precisely because the system-code property is missing.
[ERROR] Property system-code is missing.
If in the properties dialog I manually add the system-code property, all works as expected, but as I said, I think is something not easy to know for the user.
So, the question is if anyone knows if is it posible to display the archetype custom properties somehow in the Intellij-idea new project creation wizard?
Thanks in advance
Unfortunately, IntelliJ IDEA doesn't support it at the moment, you can watch/vote the issue in YouTrack.
You can add the property manually during the creation using the wizard.
I am using IntelliJ IDEA 2019.1.3
Make sure the required property is indicated in the archetype-metadata.xml file
In the screen that lists the properties, you can hit the plus symbol to add a Maven Property, Match the name listed in the archetype-metadata.xml file and add the value of this property
Properties screen and add maven proerty popup
Clicking OK will add this property to the list and the project will build using the custom property as designed.
I hope this helps

Eclipse plugin dev: how to add default VM arguments by code?

While developing some plugins based on Eclipse, I need to add some default VM arguments when user right click-> "run as" -> "java application" or "spring boot app", is there any open api I can call or any extension point?
Just like the snapshot below, the argument "-Daaa=bbb" is added by default.
Any help is appreciated.
You need to write your own plugin and need to replicate/extend Java launch configuration
(of type Java application) from JDT and add -vm argument programmatically.
But there is one more easy way: Try LcDsl.
LcDsl provides a way of defining Eclipse launch configurations in a textual way. The file extension used is '.lc' It provides some obvious and some non-obvious advantages over the Eclipse launch configuration solution.
For more info look here and check demo videos. Also there was a discussion going on to include this plugin in JDT itself, see this bug entry.
A typical Java launch configuration looks like this:
java configuration LcJavaMain {
project com.wamas.test;
main-class com.wamas.test.JavaMain;
memory min=64m max=256m;
vm-argument '-Dmy.arg=value';
argument 'cmdArg';
}

eclipse annotation processor not working. Where are errors shown?

Although there are other answers that cover this in some way, I want to have it for someone else that has similar problem and does notice the solution.
After setting up my own annotation processor and it properly working via maven, I got annoyed by being forced to rebuild source with maven on each change that needs the processor to do some magic.
Setting up eclipse to use my annotation processor required me to close annotation processor project so m2e-apt can put processor jars into .factorypath .
But then it stopped working for some reason, and I could not find the reason,
The solution to see the problem was to open:
Window->show view->General->Error Log
After seeing exceptions I was able to fix problems and get it working.
This is also the place where you'll see mesages from your processor
The answer above is correct, but I'd like to put here more details about how to work logging in Eclipse + APT.
Eclipse takes into account only messages send through processingEnv.getMessager().printMessage. If you use printMessage without reference to an element the Eclipse will route your log message to the Workspace Log (Window -> Show view -> General -> Error Log). If you use the printMessage with element reference the Eclipse will route your message to the Problems view (Window -> Show view -> General -> Problems).
If there is some exception during APT rounds Eclipse will log it into the Workspace Log (Window -> Show view -> General -> Error Log)
Eclipse will ignore any other logging methods (e.g. log4j, slf4j, etc).

configure velocity as annotations' processor dependency on eclipse

I try to follow this tutorial on java code generation utilizing annotations and Velocity template engine. (I'm using eclipse ee mars.)
On the annotated project, annotation processing is enabled and the processor (as an exported jar) is included in Factory Path. the processor has dependencies which are velocity and velocity-tools so I included them too in Factory path (velocity-dep is used which contains all velocity's dependencies).
problem is: when I include velocity-dep-1.4.jar everything works fine and code is generated but when I switch to
any other version (1.5 or 1.7) I get error The specified class for ResourceManager (org.apache.velocity.runtime.resource.ResourceManagerImpl) does not implement org.apache.velocity.runtime.resource.ResourceManager; Velocity is not initialized correctly.
on error log.
I need to utilize velocity 1.7 to proceed with my project.
Tried same things on three different pc's with same results.
I have ran out of ideas, any help would be really appreciated.
Update1 (added steps to reproduce in Eclipse):
To generate processor's jar:
clone this github repo:
https://github.com/halx4/question.StackOverflow.velocity-template-engine-configuration-on-eclipse/tree/master
It contains 2 eclipse projects, a processor and an annotated project(client). I have included all dependencies and made them non-maven projects just to make sure the problem is not maven-related.
make sure that:
processor project properties > java compiler > annotations processing > enable project specific settings is Enabled
processor project properties > java compiler > annotations processing > enable annotation processing is Enabled
processor project properties > java compiler > annotations processing > enable processing in Editor is Enabled
processor project properties > java compiler > annotations processing > Factory Path > Enable project specific settings is Enabled
processor project properties > java compiler > annotations processing > Factory Path > metainf-services-1.1.jar is Enabled as annotation processor containing JAR.
Now it should be possible to export the processor as a (non-executable)JAR. Make sure that beaninfo.vm and velocity.properties files have been included in the exported JAR. Also make sure the file META-INF/services/javax.annotation.processing.Processor has also been included in the JAR by selecting Export all output folders fro checked projects on the JAR export menu.
The exported jar may be placed in NoMavenClient project's folder. for convenience the processor's JAR(theProcessor.jar) has already been placed in this folder.
To apply annotation processing:
open eclipse error log (window > show > error log) to see messages from the processor
make sure that:
client project properties > java compiler > annotations processing > enable project specific settings is Enabled
client project properties > java compiler > annotations processing > enable annotation processing is Enabled
client project properties > java compiler > annotations processing > enable processing in Editor is Enabled
client project properties > java compiler > annotations processing > Factory Path > Enable project specific settings is Enabled
client project properties > java compiler > annotations processing > Factory Path > theProcessor.jar , velocity-tools-2.0.jar ,velocity-1.7-dep.jar are Enabled as annotation processor containing JARs.
On this stage, hitting Apply will invoke the processor. On the error log view I see "The specified class for ResourceManager..." error message.
*BUT if I include in factory path the files theProcessor.jar , velocity-tools-2.0.jar ,velocity-dep-1.4.jar and hit Apply, I get on error log "creating source file ..." which means success.
see image for better understanding: configurations
Update2
continuing my investigation on the subject, I tried to reproduce the problem in intelliJ IDEA and I simply could not. Everything works fine with velocity 1.4 or 1.7 and code files are generated as expected.
One important (maybe) difference between eclipse's and IDEA's configuration procedure is the place where the annotations processor's dependencies should be declared.
On IDEA, the processors' dependencies are declared as ordinary module's(same as eclipse's ) dependencies
but on eclipse if they are being declared as simple project dependencies in the build path, are not visible by the processor. Instead, they should be declared in Factory Path which is the same place where the annotation processors are declared. Still though, the question is not considered answered as I'd better find a solution in eclipse rather than developing the whole project in a new for me IDE.
Also configuring annotation processing through maven seems to result in different behaviour between the two IDEs. On the aforementioned tutorial's page, the author has uploaded (see his comments) a a git repo with maven projects demonstrating what he discusses. cloning this repo on IDEA and compiling, made the annotation processing work without touch any setting as all was configured with maven. I did not even had to package the processor in a jar..! Eclipse did not make any annotation processing out of the box. (and I do have m2e-apt installed; just to clarify)
If anybody tries any of the procedures I try to explicate, and has different results, please let me know.
Still though, the question is not considered answered as I'd better find a solution in eclipse rather than developing the whole project in a new for me IDE.
I'm not an eclipse user. But when I see this message:
The specified class for ResourceManager(org.apache.velocity.runtime.resource.ResourceManagerImpl) does not implement org.apache.velocity.runtime.resource.ResourceManager; Velocity is not initialized correctly.
I immediately think about a version mismatch. There must still be some 1.4 class or jar files somewhere when you switch to 1.5 ou 1.7.
Please make sure you clean the whole project and remove any 1.4 dependencies when you upgrade.

Categories