0

It so complicated for me, so i probably will can't to ask normally question, but i will try...So, i want to compile the .html file using command "mvn clean compile". I also have two custom quite similar annotations (@HtmlForm (for classes) and @HtmlInput (for fields)) looks like: @HtmlForm:

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface HtmlForm {
    String fileName();
    String action();
    String method();
}

And one annotated class:

@HtmlForm(fileName = "user_form.html", action = "/users", method = "post")
public class UserForm {
    @HtmlInput(type = "text", name = "first_name", placeholder = "Enter First Name")
    private String firstName;

    @HtmlInput(type = "text", name = "last_name", placeholder = "Enter Last Name")
    private String lastName;

    @HtmlInput(type = "password", name = "password", placeholder = "Enter Password")
    private String password;
}

my .pom file included dependencies on auto-service (seems to declare processor), on compile-plugin and reflection-util. I tried to write some custom annotation processor, but here few myknowleges are ended, and this is all, that I could to do:

@SupportedAnnotationTypes({"HtmlForm", "HtmlInput"})
@AutoService(Processor.class)

    public class HtmlProcesser extends AbstractProcessor{
        @Override
        public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv) {
    
            Set<? extends Element> annotatedElements = null;
            for (TypeElement annotation : set) {
                annotatedElements = roundEnv.getElementsAnnotatedWith(HtmlForm.class);
                if (!annotatedElements.isEmpty()) {
                    try {
                        FileObject file = processingEnv.getFiler()
                                .createResource(StandardLocation.CLASS_OUTPUT,  "","user_form.html");
                        PrintWriter out = new PrintWriter(file.openWriter());
                        out.print("sometext!!!!!");
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
    
                }
            }
            return true;
        }
    }

At first, I tried to create "user_form.html" file and write "sometext!!!" into it, but this file is not created. I also includes "user-form.html" into mvn-compile-plugin and it's does not help. What should I do?

Updade (my .pom-file):

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>Annotation</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.auto.service</groupId>
            <artifactId>auto-service</artifactId>
            <version>1.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
        </dependency>

        <dependency>
            <groupId>de.cronn</groupId>
            <artifactId>reflection-util</artifactId>
            <version>2.14.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
ldoctori
  • 1
  • 1
  • How does your pom.xml look like? – SpaceTrucker Jul 28 '22 at 06:09
  • I added the pom-file into main field – ldoctori Jul 28 '22 at 06:54
  • This could help: https://stackoverflow.com/a/41777832/1466267 – SpaceTrucker Jul 28 '22 at 07:46
  • A maven plugin as a dependency is simply wrong. Furthermore using a plugin maven-compiler-plugin version:2.0.2 (which is of 2007) I strongly recommend to use most recent versions of plugins... – khmarbaise Jul 28 '22 at 08:04
  • I deleted compiler-plugin dependency and try too use newest version of it. This does not help. I also tried to use hibernate as annotation processor, it does not work too. But, honestly, it is not clearly for me, I want to use my own annotation processor. How hibernate can help me? Or I must set to my custom processor (as src.main.java.MyCustomProcessor). I tried this and here i am... – ldoctori Jul 28 '22 at 10:48
  • To confirm, you have at least two pom files, right? One for the processor, and one that contains `UserForm.java`, `user_form.html`, and depends on the processor jar? – Colin Alworth Jul 29 '22 at 12:16

0 Answers0