Java Maven 全新安装:找不到符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19167545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:41:04  来源:igfitidea点击:

Maven clean install: cannot find symbol

javamavenbuildannotations

提问by Jason

My Maven clean install is failing. Below is the error message.

我的 Maven 全新安装失败。下面是错误信息。

[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile
(default-compile) on project MyProject: Compilation failure:
Compilation failure:

[ERROR] C:\..\MyClass.java:[13,2] cannot find symbol

[ERROR] symbol  : class MyAnnotation

[ERROR] location: class mypackage.MyClass

MyClass.java

我的类

public class MyClass{

    @MyAnnotation
    public static class MyAnnotation{
        //some static nested class code here...
    }

MyAnnotation.java

我的注解

@Retention (RetentionPolicy.RUNTIME)
public @interface MyAnnotation{
} 

I have no clue why this would present problems, can anyone please give me some ideas?

我不知道为什么这会出现问题,任何人都可以给我一些想法吗?

采纳答案by Jason

Found the problem...

发现问题了...

I apologize as I didn't include enough code for anyone to determine the root cause of the issue, normally I don't include import statements in my posts but this time I should have. The below class is a more full example. As we can see, the below class declares a static import to an object that resides in the static nested class (within the same .java file). While this is not illegal from a compilation standpoint, it was causing issues in my Maven clean install. I'm still not %100 sure why maven does not like this, but this fashion of static importing doesn't really make sense to begin with. To fix this, I removed the static import and substituded a normal static call (MyAnnoation.someObject) wherever the static import was being used.

我很抱歉,因为我没有为任何人包含足够的代码来确定问题的根本原因,通常我不会在我的帖子中包含 import 语句,但这次我应该有。下面的课程是一个更完整的例子。正如我们所见,下面的类声明了对驻留在静态嵌套类中的对象的静态导入(在同一个 .java 文件中)。虽然从编译的角度来看这并不违法,但它在我的 Maven 全新安装中引起了问题。我仍然不确定 %100 为什么 maven 不喜欢这个,但是这种静态导入的方式一开始就没有意义。为了解决这个问题,我删除了静态导入,并在使用静态导入的地方替换了一个普通的静态调用 (MyAnnoation.someObject)。

package com.classes;

import static com.classes.MyClass.MyAnnotation.someObject;

public class MyClass{

     @MyAnnotation
     public static class MyAnnotation{

     public static final Object someObject = new Object();

}

Again, my apologies for not providing the static import details in my original post, hopefully someone finds this helpful.

再次,我很抱歉没有在我的原始帖子中提供静态导入详细信息,希望有人觉得这有帮助。

回答by Vinay Lodha

Did your maven-compiler-pluginis using 1.5. By default it used 1.4 and annotations are introduced in 1.5

你用的maven-compiler-plugin是 1.5. 默认情况下,它使用 1.4 并在 1.5 中引入注释

回答by carlspring

Try the following:

请尝试以下操作:

  • Import just one of the classes.

  • Always use the FQN (fully-qualified name) for the other case, let's assume that's the annotation (@com.foo.MyAnnotation).

  • Also, as Vinay suggested, set the sourceand targetversions in your maven-compiler-plugin's <configuration/>.

  • 只导入其中一个类。

  • 对于另一种情况,始终使用 FQN(完全限定名称),让我们假设这是注释 ( @com.foo.MyAnnotation)。

  • 此外,作为维奈建议,设置sourcetarget版本在你maven-compiler-plugin<configuration/>