简体   繁体   English

Maven 编译错误:package 不存在

[英]Maven compilation error: package does not exist

I'm trying to add maven support to an existing enterprise project.我正在尝试将 maven 支持添加到现有企业项目中。 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.这是一个多模块项目,前 2 个模块编译和 package 没有问题,但我面临编译错误我尝试在多个模块中使用相同的依赖项。 My structure is:我的结构是:

> + 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.我在 Eclipse 上打开了项目,它没有显示任何错误。 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 .当我从父级运行mvn clean install时,它成功安装了模块 1 和 2,但在模块 3 上失败,说package xxx.yyy does not exist并且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. package 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.由于两个模块都依赖于相同的 jar,因此我尝试仅添加对模块 2 的依赖项,并且我相信模块 3 由于传递依赖关系而应该看到它,但它看不到 package。 So I tried to add the dependency to both module 2 and 3 poms and the problem persisted.所以我尝试将依赖项添加到模块 2 和 3 pom 中,但问题仍然存在。

I've already checked/tried these:我已经检查/尝试过这些:

  • The requested jar is listed in my dependecies.请求的 jar 列在我的依赖项中。 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我运行了以下命令,之后我可以在依赖项列表中看到 required.jar 文件: 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正如我上面所说,我试图通过传递依赖项放入我的类路径并直接在我的 pom 上引用它,但这两种解决方案都不起作用
  • I tried to delete my whole repository and download everything again and also didn't work我试图删除我的整个存储库并再次下载所有内容,但也没有用

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 的 pom。由于公司政策,我更改了一些名称。

Module-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'.而看不到的package在“SharedArtifact”里面。 When I remove the dependency from Module2, it doesn't gives the package does not exist error from the 'SharedArtifact' package.当我从 Module2 中删除依赖项时,它不会给出来自“SharedArtifact”package 的package does not exist错误。 But then it gives the package does not exist from the Module2 jar.但随后它给出了package does not exist的 package。

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_29 and Maven 3.0.5 .我正在使用Java 1.6.0_29Maven 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. 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.

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.今天我刚刚发现如果运行mvn clean install ,它将无法编译。 But if I run mvn clean , then (in Eclipse) I run Maven Update project and then `mvn install, it compiles!但是如果我运行mvn clean ,那么(在 Eclipse 中)我运行Maven Update project ,然后 `mvn install,它编译!

Since I found this, I've already referred to this question and 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.当我使用 Weblogic Portal 应用程序时,我需要在 Eclipse 上保留一些共享库(项目和运行时都需要的那些)。 Possibly some library is being forgotten on my pom.可能在我的 pom 上忘记了一些图书馆。 The weird thing is that the package that the compilation claims that doesn't exist, DOES exist.奇怪的是,编译声称不存在的 package 确实存在。

Since I was running out of options, I followed what was suggested by @user944849 and updated Maven 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,但保持源和目标指向1.6的POM编译器配置。

<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. 之后,模块3开始编译,但模块4和5由于对某些JRockit库的依赖而中断。 To fix that, I ended up updating the deprecated code and the project finally fully compiled. 为了解决这个问题,我最终更新了已弃用的代码,最终完全编译了项目。

So I believe there was a problem on the Maven version I was using. 所以我相信我使用的Maven版本存在问题。 Somehow it seems to get lost when 2 projects depended on the same lib and one of them depended on the other. 不知何故,当2个项目依赖于同一个lib并且其中一个依赖于另一个时,它似乎迷失了。 I haven't tested other versions, but the 3.3.9 works fine for these cases. 我没有测试过其他版本,但3.3.9适用于这些情况。

I had the same issue, i build the project with clean install and selected: resolve workspace artifacts and the project build successfully.我遇到了同样的问题,我使用全新安装构建项目并选择:解决工作区工件并成功构建项目。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM