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.

Comments on Maven JPA integration: processor not found

Parent

Maven JPA integration: processor not found

+2
−0

My annotation processor "cannot be found" but everything ~looks okay.

In Eclipse IDE, I created a new Maven project with the "quickstart-architype" resulting in a project with this structure:

____pom.xml
____src/main/java
________com.folder.Project
____________App.java

There isn't a META-INF directory in the Project hierarchy; it does not get generated when I create Maven projects, but it should not be the issue.

I also added some files/directories to com.folder.Project, resulting in:

com.folder.Project
____App.java
____archive
________Annotation.java
____processors
________AnnotationProcessor.java

This is a new project, and there are not any added files outside of what I have mentioned. These added files even have the same name in my setup; I've set it up like this, purely to troubleshoot and learn how to create annotation processors in Java.

In the terminal, when I run mvn compile -X or other similar commands, I get an error:

diagnostic: error: Annotation processor 'com.folder.Project.processors.AnnotationProcessor' not found.

pom.xml:

<groupid>com.folder</groupid>
<artifactid>Project</artifactid>
<version>2024</version>
<packaging>jar</packaging>
<name>Project</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupid>junit</groupid>
        <artifactid>junit</artifactid>
        <version>3.8.1</version>
        <scope>test
    </dependency>
    <dependency>
         <groupid>org.bsc.maven</groupid>
         <artifactid>maven-processor-compiler</artifactid>
         <version>5.0-jdk8</version>
     </dependency>
     <dependency>
         <groupid>org.bsc.maven</groupid>
         <artifactid>maven-processor-plugin</artifactid>
         <version>5.0-jdk8-rc3</version>
     </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupid>org.apache.maven.plugins</groupid>
            <artifactid>maven-compiler-plugin</artifactid>
            <version>3.11.0</version>
        </plugin>
        <plugin>
            <groupid>org.bsc.maven</groupid>
            <artifactid>maven-processor-plugin</artifactid>
            <version>5.0-jdk8-rc3</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process
                    </goals>
                    <phase>generate-sources</phase>
                        <configuration>
                            <processors>
                                <processor>com.open.Engine.processors.AnnotationProcessor</processor>
                            </processors>
                        </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

The mistake is probably in my POM file. In the XML view in Eclipse, each tag gives a meaningful description (if a tag was spelled incorrectly or misplaced, it might not).

Here are the simple classes I made for testing:

Annotation.java:

package com.folder.Project.archive;

// import from java.lang.annotation.Documented;
@Documented
public @interface Annotation { /*does nothing*/ }

AnnotationProcessor.java:

package com.folder.Project.processors;

// imports from javax, and java.util.Set;
@SupportedAnnotationTypes("com.folder.Project.archive.Annotation") // I'm not sure if this matches
public class AnnotationProcessor extends AbstractProcessor {
    @Override public boolean process( ... ) // Set and RoundElement inputs
    {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error");
        return false; // so it should fail every time (which lets me know if it works)
    }
}

App.java:

import com.folder.Project.archive.Annotation;

... main(String[] args) {
    @Annotation int x = 1;
    System.out.println("finished main");
}

The code is written so I can verify that an annotation processor is executed at all.


A few notes:

  • diagnostic: error: Annotation processor 'com.folder.Project.processors.AnnotationProcessor' not found; is the 1st error to appear after running mvn compile.

  • mvn clean compile -X doesn't elaborate on the error: Build Fail / could Not Execute / could not achieve goal.

  • I run "Build Project", "Build All", and "Run" after every significant change to pom.xml.

  • <annotationprocessingpaths>, from an alternative solution, was not a recognized tag in my pom dependency hierarchy (alternative solutions failed too).


What steps should I take to get everything running normally, for annotation processing? What errors have I made?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+3
−0

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:

/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.


/pom.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>

/Preprocessor/pom.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>

/Program/pom.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>

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

package com.folder.project.archive;

public @interface Annotation {
    String value();
}

/Preprocessor/src/main/java/com/folder/project/preprocessors/AnnotationProcessor.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;
    }
}

/Program/src/main/java/com/folder/project/program/App.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" );
    }
}

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

It works (2 comments)
It works
tylerbakeman‭ wrote 8 months ago

It works. It took a little bit of time to implement.

I updated my project setup and run comfigurations (mvn exec:java doesn’t configure to App.java in the project Program module, which Ill figure out next)

This is a touch subject, so im really grateful for your time

Thank you

Zer0‭ wrote 8 months ago

You are right, there is no configuration for mvn exec:java in the example, but you can run it with mvn package and then java -cp Program\target\Program-1.0.jar com.folder.project.program.App to see the ouput. (To demonstrate the annotation processor, a mvn compile is sufficient)