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.

Maven JPA integration: processor not found

+1
−0

Personal Project: I am new to Maven and have spent a few weeks troubleshooting pom errors with chatGPT. Tutorials are hard to come by online, because annotation-processing in Java is more complicated than the sources I've found. Issue in summary: my annotation processor "cannot be found" but everything ~looks okay. I need help spotting the issue in my Pom; Thank you in advance!

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

Project:
____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 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 + learn how to create annotation processors in java.

Issue: 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:

<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.
I had to retype 's and 's, so ignore spelling errors related to those; in the XML view on Eclipse, each tag gives a meaningful description (versus, if a tag was spelled incorrectly or misplaced, it might not).

Here are the simple classes I made for testing:

Annotation.java:

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

AnnotationProcessor.java:

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

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

The code is written so I can verify 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).

  • I have confirmed the spelling is correct in the files, with the correct filenames as well. If you see any spelling errors above, it's probably because I mistyped them into the question-forum text editor.


I'm trying to build annotation processors in Java; I think it has potential for lots of future projects. I understand it's a little niche, but also think it is probably important to know / be able to use effectively.

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

Thank you for your time

  • Tyler Bakeman
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

+1
−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 moderator attention?
You might want to add some details to your flag.

1 comment thread

It works (2 comments)
+0
−0

You say this is a personal project. I am sure you were free to chose the technology.

I ask, because Java, annotations and Mvn are good tools for some requirements, but not necessarily the most easy, elegant or efficient ones for everything... it depends.

What exactly is the use of your annotation processors in Java? Is it learning Java annotations (then you are doomed), or building something like... a text editor, a compiler, a game etc.?

Can you avoid annotations altogether? They are again good for certain use cases, but bring a heavy debt: the code is much less readable, as the implementation of the annotation is... somewhere else. They are not good for themselves; at most a necessary evil: avoid if you can.

Same goes for Maven: the version management is fantastic; but if you don't need that, you'll be better off with a more transparent build system, where YOU understand exactly what it does.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Works for me (1 comment)

Sign up to answer this question »