2016-03-27 1 views
1

Ich schreibe eine einfache benutzerdefinierte Annotation in Java und läuft damit auf ein Problem damit. Hier sind die wichtigsten Teile meines Codes.Schlechte Service-Konfigurationsdatei oder Ausnahme beim Konstruieren Prozessorobjekt

LogMeCustomAnnotation.java

package fun.n.learn.annotation; 

import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 

// We need this annotation only till before compilation. 
@Retention(RetentionPolicy.SOURCE) 
// This is a simple custom annotation. 
public @interface LogMeCustomAnnotation { 

} 

LogMeCustomAnnotationProcessor.java

package fun.n.learn.annotation; 

import java.util.Set; 

import javax.annotation.processing.AbstractProcessor; 
import javax.annotation.processing.Messager; 
import javax.annotation.processing.RoundEnvironment; 
import javax.annotation.processing.SupportedAnnotationTypes; 
import javax.lang.model.element.TypeElement; 
import javax.tools.Diagnostic; 

// List the custom annotations that are supported. 
@SupportedAnnotationTypes({ "fun.n.learn.annotation.LogMeCustomAnnotation" }) 
// Extend AbstractProcessor. This will let you process. 
public class LogMeCustomAnnotationProcessor extends AbstractProcessor { 

    @Override 
    public boolean process(Set<? extends TypeElement> annotations, 
      RoundEnvironment roundEnv) { 

     Messager messager = processingEnv.getMessager(); 
     messager.printMessage(Diagnostic.Kind.NOTE, "I was here."); 

     // TODO: Put some meaningful code here. Right now just get it to work. 

     // return false; 
     // We have already handled these annotations. No more. So return true. 
     return true; 
    } 

} 

/src/main/resources/META-INF/services/javax.annotation.processing.Processor

fun.n.learn.annotation.LogMeCustomAnnotationProcessor 
Jetzt

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>fun.n.learn</groupId> 
    <artifactId>javaCustomAnnotation</artifactId> 
    <version>0.1.0</version> 

    <build> 
     <plugins> 
      <plugin> 
       <!-- Configure the project to use java 8 version. --> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
        <!-- Disable annotation processing for ourselves. --> 
        <!-- <compilerArgument>-proc:none</compilerArgument> --> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 


</project> 

wenn ich mvn -e clean install laufen bekomme ich folgendes Problem

[ERROR] COMPILATION ERROR : 
[INFO] ------------------------------------------------------------- 
[ERROR] Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider fun.n.learn.annotation.LogMeCustomAnnotationProcessor not found 
[INFO] 1 error 

Ich muß hier einen einfachen Trick fehlen. Irgendeine Hilfe?

+0

Könnten Sie Ihre ganze POM posten? – Tunaki

Antwort

4

Ok. Ich habe das Problem gefunden. Früher hatte meine pom.xml die proc:none Zeile auskommentiert. Jetzt, wo ich es in Aktion habe, ist es in Ordnung. Ich muss genau herausfinden, was diese Zeile macht, aber die Antwort auf meine Frage ist einfach die proc:none zurück ins Spiel zu setzen. So sieht der Build-Bereich meiner pom.xml jetzt aus.

<build> 
    <plugins> 
     <plugin> 
      <!-- Configure the project to use java 8 version. --> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.5.1</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
       <!-- Disable annotation processing for ourselves. --> 
       <compilerArgument>-proc:none</compilerArgument> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 
+1

' -proc: none' deaktiviert die Annotationsverarbeitung. So wird 'LogMeCustomAnnotation 'nie verarbeitet.Wahrscheinlich ist es nicht das, was Sie in Ihrer ursprünglichen Frage erwartet haben. – Evgeny

6

Der Standard Maven Lifecycle läuft javac mit javax.annotation.processing.Processor Datei als Teil des Classpath. Dies führt dazu, dass der Compiler eine kompilierte Instanz der in den Dateien aufgeführten Annotation-Prozessoren erwartet. Aber LogMeCustomAnnotationProcessor wird in diesem Moment nicht kompiliert, daher löst der Compiler den Fehler "Fehlerhafte Konfigurationsdatei ..." aus. Siehe bug report.

Um dieses Problem zu lösen, kann die Maven-Kompilierungsphase getrennt werden, um den Annotationsprozessor an erster Stelle zu kompilieren und dann das gesamte Projekt zu kompilieren.

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.5.1</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
      <executions> 
       <execution> 
        <id>default-compile</id> 
        <configuration> 
         <compilerArgument>-proc:none</compilerArgument> 
         <includes> 
          <include>fun/n/learn/annotation/LogMeCustomAnnotationProcessor.java</include> 
          <!--include dependencies required for LogMeCustomAnnotationProcessor --> 
         </includes> 
        </configuration> 
       </execution> 
       <execution> 
        <id>compile-project</id> 
        <phase>compile</phase> 
        <goals> 
         <goal>compile</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

default-compile Ausführung kompiliert LogMeCustomAnnotationProcessor mit behinderter Annotation Verarbeitung, um erfolgreich Kompilierung zu haben.
compile-project kompiliert ganzes Projekt mit Annotaton-Verarbeitung.

+0

Wie führe ich einen bestimmten Ausführungstyp aus? – EpicPandaForce

+1

@EpicPandaForce Execution-ID-Spezifikation ist seit Maven 3.3.1 [MNG-5768] (https://issues.apache.org/jira/browse/MNG-5768) verfügbar. 'mvn compiler: kompiliere @ compile-project' – Evgeny

0

Befolgen Sie die folgenden Schritte, um dies zu beheben:

  • bearbeiten nbproject/project.properties Datei
  • Suche nach javac.processorpath, und ändern Sie es zu:

javac.processorpath=\ ${javac.classpath}:\ ${libs.eclipselink.classpath}

Verwandte Themen