Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Maven JPA integration: processor not found

The annotation processor must be compiled before it can be used. This is not a problem if you're using it as a dependency from Maven Central, for example. If you want to use a custom processor, you...

posted 7mo ago by Zer0‭

Answer
#1: Initial revision by user avatar Zer0‭ · 2024-03-27T20:49:07Z (7 months ago)
The annotation processor must be compiled before it can be used. This is not a problem if you're using it as a dependency from Maven Central, for example. If you want to use a custom processor, you usually split your project into two parts.

Your project structure could look as follows:
```txt
/Project
  /Preprocessor
    /src/main/java/com/folder/project
      /archive
        Annotation.java
      /preprocessors
        AnnotationProcessor.java
    pom.xml
  /Program
    /src/main/java/com/folder/project/program
      App.java
    pom.xml
  pom.xml
```

There are three pom.xml files. One root file and two for the `Preprocessor` and `Program` modules. `Preprocessor` is a dependency for `Program`, so it can be compiled and used before program is compiled.

`Preprocessor` contains the annotation and the annotation preprocessor. `Program` is the example module.

<br />


`/pom.xml`

```xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.folder.project</groupId>
  <artifactId>Project</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>

  <name>Project</name>
  <url>http://maven.apache.org</url>
  <modules>
    <module>Preprocessor</module>
    <module>Program</module>
  </modules>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>8</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.11.0</version>
    </dependency>
  </dependencies>
</project>
```

<br />

`/Preprocessor/pom.xml`

```xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.folder.project</groupId>
        <artifactId>Project</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>Preprocessor</artifactId>
    <packaging>jar</packaging>
</project>
```

<br />

`/Program/pom.xml`

```xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.folder.project</groupId>
        <artifactId>Project</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>Program</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.folder.project</groupId>
            <artifactId>Preprocessor</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
            <type>jar</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <annotationProcessors>
                        <annotationProcessor>
                            com.folder.project.preprocessors.AnnotationProcessor
                        </annotationProcessor>
                    </annotationProcessors>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
```

<br />

`/Preprocessor/src/main/java/com/folder/project/archive/Annotation.java`

```java
package com.folder.project.archive;

public @interface Annotation {
    String value();
}
```

<br />

`/Preprocessor/src/main/java/com/folder/project/preprocessors/AnnotationProcessor.java`

```java
package com.folder.project.preprocessors;

import com.folder.project.archive.Annotation;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.Set;

@SupportedAnnotationTypes("com.folder.project.archive.Annotation")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class AnnotationProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (Element e: roundEnv.getElementsAnnotatedWith(Annotation.class)){
            Annotation a = e.getAnnotation(Annotation.class);
            processingEnv.getMessager().printMessage( Diagnostic.Kind.WARNING, "Annotation found. Type: "+a.value() );
        }
        return false;
    }
}
```

<br />

`/Program/src/main/java/com/folder/project/program/App.java`

```java
package com.folder.project.program;

import com.folder.project.archive.Annotation;

@Annotation("class")
public class App
{
    @Annotation("method")
    public static void main( String[] args )
    {
        @Annotation("local variable") int x = 1;
        System.out.println( "finished main" );
    }
}
```

<br />

You will see a warning with the text `Annotation found` when you compile the project. You can do this from root, which will automatically build the preprocessor module and then compile the program.
There are three annotations, but only two `Annotation found` warnings will appear. That's because local variable annotations are ignored.