java Maven编译错误:包不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43214715/
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
Maven compilation error: package does not exist
提问by cristianorbs
I'm trying to add maven support to an existing enterprise project. It is a multi-module project, and the first 2 modules compile and package without problems, but I'm facing the Compilation Error I try to use the same dependency in more than one module. My structure is:
我正在尝试为现有的企业项目添加 Maven 支持。这是一个多模块项目,前 2 个模块编译和打包没有问题,但我面临编译错误我尝试在多个模块中使用相同的依赖项。我的结构是:
> + parent
> - pom.xml
> - module-1
> - pom.xml
> - module-2 (Depends on module-1)
> - pom.xml
> - module-3
> - pom.xml (Depends on both modules 1 and 2)
I'm with the project opened on Eclipse and it shows no errors. When I run mvn clean install
from the parent, it succesfully installs both module 1 and 2, but fails on module 3 saying that package xxx.yyy does not exist
and Cannot find symbol XXXYYY
. The package xxx.yyy and the symbol XXXYYY are inside a jar that is listed on the dependencies of both modules 2 and 3.
我在 Eclipse 上打开了这个项目,它没有显示任何错误。当我mvn clean install
从父级运行时,它成功地安装了模块 1 和 2,但在模块 3 上失败了,说package xxx.yyy does not exist
和Cannot find symbol XXXYYY
。包 xxx.yyy 和符号 XXXYYY 位于模块 2 和模块 3 的依赖项中列出的 jar 中。
Since both modules depende on same jar, I tried to add the dependency only on module 2 and I believe that module 3 should be seeing it because of transitive dependencies, but it couldn't see the package. So I tried to add the dependency to both module 2 and 3 poms and the problem persisted.
由于两个模块都依赖于同一个 jar,我尝试仅添加对模块 2 的依赖,并且我认为模块 3 应该因为传递依赖而看到它,但它看不到包。因此,我尝试将依赖项添加到模块 2 和 3 poms,但问题仍然存在。
I've already checked/tried these:
我已经检查/尝试过这些:
- The requested jar is listed in my dependecies. I've ran the following command and after that I can see the required .jar file on the list of dependencies:
mvn dependency:copy-dependencies
- As I said above, I tried to put in my classpath through the transitive dependency and referencing it directly on my pom and both solutions didn't work
- I tried to delete my whole repository and download everything again and also didn't work
- 请求的 jar 列在我的依赖项中。我运行了以下命令,之后我可以在依赖项列表中看到所需的 .jar 文件:
mvn dependency:copy-dependencies
- 正如我上面所说,我试图通过传递依赖放入我的类路径并直接在我的 pom 上引用它,但两种解决方案都不起作用
- 我试图删除我的整个存储库并再次下载所有内容,但也没有用
The only particularity of my project is that module 3 depends on module 2 and depends on a lib that module 2 also depends.
我的项目唯一的特殊性是模块 3 依赖于模块 2 并且依赖于模块 2 也依赖的库。
Below I'm pasting the poms from both module 2 and 3. I've changed some names, because of company policy.
下面我粘贴了模块 2 和模块 3 中的 poms。由于公司政策,我更改了一些名称。
Module-2
模块 2
<project xmlns="http://maven.apache.org/POM/4.0.0" ... >
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Module2</artifactId>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>Module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>SharedArtifact</artifactId>
<version>1.1</version>
</dependency>
...
Module-3
模块 3
<project xmlns="http://maven.apache.org/POM/4.0.0" ... >
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Module3</artifactId>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>Module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--<dependency>
<groupId>com.company</groupId>
<artifactId>SharedArtifact</artifactId>
<version>1.1</version>
</dependency>-->
...
And the package that can't be seen is inside the 'SharedArtifact'. When I remove the dependency from Module2, it doesn't gives the package does not exist
error from the 'SharedArtifact' package. But then it gives the package does not exist
from the Module2 jar.
而看不到的包在'SharedArtifact'里面。当我从 Module2 中删除依赖项时,它不会给出package does not exist
来自“SharedArtifact”包的错误。但随后它给出了package does not exist
来自 Module2 jar 的。
It looks like that somehow the dependencies are getting lost since the Module3 depends on them both.
看起来依赖关系以某种方式丢失了,因为 Module3 依赖于它们。
I'm using Java 1.6.0_29and Maven 3.0.5. And I can't upgrade to Java 7, because the project needs Java 6. And can't upgrade Maven, cause upgraded Maven only works with java 7 and higher.
我正在使用Java 1.6.0_29和Maven 3.0.5。而且我无法升级到 Java 7,因为项目需要 Java 6。并且无法升级 Maven,因为升级后的 Maven 仅适用于 Java 7 及更高版本。
Update 1:
更新 1:
The error given when I compile is the following:
我编译时给出的错误如下:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] \Users\usuario\Documents\workspaces\myworkspace\qa\Module3\src\com\company\sharedartifact\package\SomeClass.java:[8,55]
package com.company.sharedartifact.package does not exist
[ERROR] \Users\usuario\Documents\workspaces\myworkspace\qa\Module3\src\com\company\sharedartifact\package\SomeClass.java:[9,55]
package com.company.sharedartifact.package does not exist
[ERROR] \Users\usuario\Documents\workspaces\myworkspace\qa\Module3\src\com\company\sharedartifact\package\SomeClass.java:[17,85]
cannot find symbol
symbol : class SomeOtherClass
I can't paste the whole -X log because of company policy, but if some part is necessary, I can change some names.
由于公司政策,我无法粘贴整个 -X 日志,但如果需要某些部分,我可以更改一些名称。
Update 2:
更新 2:
Today I just figured out that if a run mvn clean install
, it fails to compile. But if I run mvn clean
, then (in Eclipse) I run Maven Update project
and then `mvn install, it compiles!
今天我才发现,如果运行mvn clean install
,则无法编译。但是如果我运行mvn clean
,那么(在 Eclipse 中)我运行Maven Update project
然后`mvn install,它编译!
Since I found this, I've already referred to this questionand I believe that something is wrong with my classpath. As I'm working with a Weblogic Portal application, I need to keep some Shared Libraries (those that are needed both by project and runtime) on Eclipse. Possibly some library is being forgotten on my pom. The weird thing is that the package that the compilation claims that doesn't exist, DOES exist.
自从我发现这个,我已经提到了这个问题,我相信我的类路径有问题。当我使用 Weblogic Portal 应用程序时,我需要在 Eclipse 上保留一些共享库(项目和运行时都需要的那些)。可能有一些图书馆被我的 pom 遗忘了。奇怪的是,编译声称不存在的包,确实存在。
采纳答案by cristianorbs
Since I was running out of options, I followed what was suggested by @user944849 and updatedMaven to version 3.3.9 and my JDK to 1.8.0_60, but kept source and target on the compiler configuration of the POM pointing to 1.6.
由于我的选项用完了,我按照@user944849 的建议将Maven更新到 3.3.9 版,将我的 JDK更新到 1.8.0_60,但将 POM 的编译器配置中的源和目标保持指向 1.6。
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
After that, module 3 began to compile, but modules 4 and 5 broke due to dependencies on some JRockit libs. To fix that, I ended up updating the deprecated code and the project finally fully compiled.
之后,模块 3 开始编译,但模块 4 和 5 由于依赖某些 JRockit 库而崩溃。为了解决这个问题,我最终更新了弃用的代码,项目最终完全编译。
So I believe there was a problem on the Maven version I was using. Somehow it seems to get lost when 2 projects depended on the same lib and one of them depended on the other. I haven't tested other versions, but the 3.3.9 works fine for these cases.
所以我相信我使用的 Maven 版本存在问题。不知何故,当 2 个项目依赖于同一个库而其中一个依赖于另一个时,它似乎迷路了。我没有测试过其他版本,但 3.3.9 在这些情况下工作正常。