简体   繁体   English

使用带有java 1.8和netbeans的maven-compiler-plugin 2.5.1在Java中进行注释的编译错误

[英]Compilation error for annotations in Java using maven-compiler-plugin 2.5.1 with java 1.8 and netbeans

I'm newborn in writing anotations in Java. 我刚开始用Java编写anotations。 I was trying to write my own following this tutorial: Playing with Java annotation processing 我试着按照本教程编写自己的文章: 使用Java注释处理

I wrote everything like it is there, but during compilation I'm getting an error: 我写了一切就像它在那里,但在编译期间我收到一个错误:

Bad service configuration file javax.annotation.processing.Processor Provider <my class> not found.

I'm using netbeans and maven with plugin maven-compiler-plugin v. 2.5.1. 我正在使用netbeans和maven与插件maven-compiler-plugin v.2.5.1。 and java sources v.1.8. 和java源码v.1.8。

In my pom.xml file I have (like suggested in page) following code: 在我的pom.xml文件中,我有以下代码(如页面中所示):

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.5.1</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <!-- Disable annotation processing for ourselves. -->
    <compilerArgument>-proc:none</compilerArgument>
  </configuration>
</plugin>

My OS is Linux (leatest ubuntu) and maven is that one integrated in Netbeans. 我的操作系统是Linux(最好的ubuntu),maven是集成在Netbeans中的。

I was trying to google it, but nothing helped me. 我试图谷歌它,但没有任何帮助我。 All tutorials were for older release of plugin and Java. 所有教程都是针对旧版本的插件和Java。 I tried older release of maven-compiler-plugin but with no effect. 我尝试了maven-compiler-plugin的旧版本但没有效果。 I cannot switch to older version of java because of new features introduced in Java 8. 由于Java 8中引入了新功能,我无法切换到旧版本的Java。

Thanks a lot for any pointing me how to fix it. 非常感谢任何指点我如何解决它。

Edit: 编辑:

Here is full list of my sources: 以下是我的来源的完整列表:

Config.java Config.java

@Retention(RetentionPolicy.SOURCE)
@Target(value = {ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.PARAMETER})
public @interface Config {
    String name();
    String type();
    String defaultValue();
}

ConfigAnnotationProcessor.java ConfigAnnotationProcessor.java

@SupportedAnnotationTypes(
        {"sk.lieskove301.jianghongtiao.motionanalyser.config.ConfigAnnotations"}
)
public class ConfigAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
        Messager messager = processingEnv.getMessager();
        annotations.stream().forEach((te) -> {
            env.getElementsAnnotatedWith(te).stream().forEach((e) -> {
                messager.printMessage(Diagnostic.Kind.NOTE,
                        "Printing: " + e.toString());
            });
        });
        return true;
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }
}

META-INF/services/javax.annotation.processing.Processor META-INF /服务/ javax.annotation.processing.Processor

sk.lieskove301.jianghongtiao.motionanalyser.config.ConfigAnnotationProcessor

pom.xml 的pom.xml

<?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>sk.lieskove301.jianghongtiao</groupId>
    <artifactId>MotionAnalyser</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        .... some dependencies ...
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <profiles>
        <profile>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.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>
        </profile>
    </profiles>
</project>

And this is log of my compiler : 这是我的编译器的日志

cd /home/xjuraj/Dropbox/Work/MotionAnalyser; JAVA_HOME=/usr/lib/jvm/java-8-oracle /usr/local/netbeans-8.0/java/maven/bin/mvn clean install
Scanning for projects...

------------------------------------------------------------------------
Building MotionAnalyser 1.0-SNAPSHOT
------------------------------------------------------------------------

--- maven-clean-plugin:2.4.1:clean (default-clean) @ MotionAnalyser ---
Deleting /home/xjuraj/Dropbox/Work/MotionAnalyser/target

--- maven-resources-plugin:2.5:resources (default-resources) @ MotionAnalyser ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
Copying 6 resources

--- maven-compiler-plugin:2.3.2:compile (default-compile) @ MotionAnalyser ---
Compiling 27 source files to /home/xjuraj/Dropbox/Work/MotionAnalyser/target/classes
-------------------------------------------------------------
COMPILATION ERROR : 
-------------------------------------------------------------
error: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider sk.lieskove301.jianghongtiao.motionanalyser.config.ConfigAnnotationProcessor not found
1 error
-------------------------------------------------------------
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 1.661s
Finished at: Mon Aug 11 19:56:16 CEST 2014
Final Memory: 12M/180M
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project MotionAnalyser: Compilation failure
error: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider sk.lieskove301.jianghongtiao.motionanalyser.config.ConfigAnnotationProcessor not found
-> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Edit 2 and so on... : 编辑2,依此类推......

Screenshot of my environment is here 我的环境截图在这里

Screenshot of my folder structure 我的文件夹结构截图

Output of non-integrated maven 输出非综合maven

xjuraj@xjuraj-pc:~/Dropbox/Work/MotionAnalyser$ mvn -e package
+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - sk.lieskove301.jianghongtiao:MotionAnalyser:jar:1.0-SNAPSHOT
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 6 resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 27 source files to /home/xjuraj/Dropbox/Work/MotionAnalyser/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] error: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider sk.lieskove301.jianghongtiao.motionanalyser.config.ConfigAnnotationProcessor not found
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
error: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider sk.lieskove301.jianghongtiao.motionanalyser.config.ConfigAnnotationProcessor not found

[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.BuildFailureException: Compilation failure
error: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider sk.lieskove301.jianghongtiao.motionanalyser.config.ConfigAnnotationProcessor not found

    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:715)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
error: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider sk.lieskove301.jianghongtiao.motionanalyser.config.ConfigAnnotationProcessor not found

    at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:729)
    at org.apache.maven.plugin.CompilerMojo.execute(CompilerMojo.java:128)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
    ... 17 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Aug 14 15:10:26 CEST 2014
[INFO] Final Memory: 19M/187M
[INFO] ------------------------------------------------------------------------

Cheers, juraj 干杯,juraj

Thanks a lot bigdestroyer for your time and help. 非常感谢你的时间和帮助。 I already found where the bug was. 我已经找到了bug的位置。 It was in pom.xml file. 它在pom.xml文件中。 My structure was: 我的结构是:

<project>
    [...]
    <dependencies>.... some dependencies ...</dependencies>
    <properties>.... some properties ...</properties>
    <profiles>
        <profile>
            <build>
                <plugins>
                    <plugin>
                        ... plugins & settings ...

But there were problem with tags <profiles> and <profile> . 但是标签<profiles><profile>存在问题。 I removed them and it works like a charm now. 我删除了它们,它现在就像一个魅力。

So my working pom.xml now looks like: 所以我的工作pom.xml现在看起来像:

<?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>sk.lieskove301.jianghongtiao</groupId>
    <artifactId>MotionAnalyser</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        ...dependencies...
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.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>

The problem seems to be in this instruction: 问题似乎出现在这条指令中:

<compilerArgument>-proc:none</compilerArgument>

OP's answer has that line in Maven configuration, but he doesn't mention its importance. OP的答案在Maven配置中有这一点,但他没有提到它的重要性。

I, too, tried to write an annotation processor and compile it with Maven. 我也试图编写一个注释处理器并用Maven编译它。 Addinng the line did the trick. Addinng这条线就行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven编译器插件错误(maven-compiler-plugin:2.5.1:testCompile) - Maven Compiler Plugin Error (maven-compiler-plugin:2.5.1:testCompile) 无法使用Java高于1.8的maven-compiler-plugin进行编译 - Impossible to compile using maven-compiler-plugin with Java higher than 1.8 Maven Java 构建编译时出错:maven-compiler-plugin - Maven Java error compiling on build: maven-compiler-plugin NetBeans项目中的exec-maven-plugin Java版本不同于maven-compiler-plugin Java版本 - exec-maven-plugin Java version different from maven-compiler-plugin Java version in NetBeans project java,为什么会出现这些错误? --maven-编译器插件:3.7.0 - java, why these error? --maven-compiler-plugin:3.7.0 使用不带maven-compiler-plugin的Java 1.7 - Using Java 1.7 without maven-compiler-plugin 编译错误:无法执行 maven-compiler-plugin:2.3.2:compile - COMPILATION ERROR : Failed to execute maven-compiler-plugin:2.3.2:compile maven-compiler-plugin 编译失败:3.6.1 - Compilation failure for maven-compiler-plugin: 3.6.1 使用 maven-compiler-plugin 从编译中排除 src/main/java - exclude src/main/java from compiling with maven-compiler-plugin 无法执行maven-compiler-plugin:3.6.1:使用java 9时的testCompile - Failed to execute maven-compiler-plugin:3.6.1:testCompile when using java 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM