Maven编译错误:包不存在 [英] Maven compilation error: package does not exist

查看:236
本文介绍了Maven编译错误:包不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为现有企业项目添加maven支持。这是一个多模块项目,前两个模块编译和打包没有问题,但我面临编译错误我尝试在多个模块中使用相同的依赖项。
我的结构是:

 > + parent 
> - pom.xml
> - module-1
> - pom.xml
> - module-2(取决于模块-1)
> - pom.xml
> - module-3
> - pom.xml(取决于模块1和模块2)

我正在打开项目Eclipse并没有显示任何错误。当我从父级运行 mvn clean install 时,它成功安装了模块1和2,但在模块3上失败,说包xxx.yyy不存在找不到符号XXXYYY 。软件包xxx.yyy和符号XXXYYY位于模块2和3的依赖项列表中的jar中。



由于两个模块都依赖于同一个jar,我试图仅在模块2上添加依赖项,我相信模块3应该因为传递依赖性而看到它,但它无法看到包。所以我尝试将依赖项添加到模块2和3 poms并且问题仍然存在。



我已经检查/尝试过这些:




  • 请求的jar列在我的家属中。我运行了以下命令,之后我可以在依赖项列表中看到所需的.jar文件: mvn依赖项:copy-dependencies

  • 正如我上面所说,我试图通过传递依赖关系输入我的类路径,并直接在我的pom上引用它,两个解决方案都不起作用

  • 我试图删除我的整个存储库并再次下载所有内容并且也无法正常工作



我的项目唯一的特点是模块3依赖于模块2和依赖于模块2也依赖的库。



下面我从模块2和模块3粘贴poms。由于公司政策的原因,我改了一些名字。



模块-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>

<依赖>
< groupId> com.company< / groupId>
< artifactId> Module1< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< / dependency>

<依赖>
< groupId> com.company< / groupId>
< artifactId> SharedArtifact< / artifactId>
< version> 1.1< / version>
< / dependency>

...

Module-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>

<依赖>
< 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> - >

...

无法看到的包裹是在'SharedArtifact'中。当我从Module2中删除依赖项时,它不会从'SharedArtifact'包中提供包不存在错误。但是它从Module2 jar中给出包不存在



由于Module3依赖于它们,所以看起来某种程度上依赖关系会丢失。



我'使用 Java 1.6.0_29 Maven 3.0.5 。而且我无法升级到Java 7,因为该项目需要Java 6.并且无法升级Maven,因为升级的Maven仅适用于java 7及更高版本。



更新1:



编译时给出的错误如下:

  [ERROR]编译错误:
[INFO] --------------- ----------------------------------------
[ERROR] \Users \usuario\Documents\workspaces\myworkspace\qa\Module3\src\com\company\sharedartifact\package\SomeClass.java:[8,55]
封装COM .company.sharedartifact.package不存在
[错误] \Users\usuario\Documents\workspaces\myworkspace\qa\Module3\src\com\company\sharedartifact\\ \\package\SomeClass.java:[9,55]
封装com.company.sharedartifact.package不存在
[ERROR] \Users\usuario\Documents\workspaces\m yworkspace\qa\Module3\src\com\company\sharedartifact\package\SomeClass.java:[17,85]
因找不到符号
符号:类SomeOtherClass

由于公司政策,我无法粘贴整个-X日志,但如果有必要,我可以可以更改一些名称。



更新2:



<今天我发现如果运行 mvn clean install ,它就无法编译。但是如果我运行 mvn clean ,那么(在Eclipse中)我运行 Maven Update项目然后`mvn install,it编译!



因为我发现了这个,我已经提到了这个问题我相信我的类路径有问题。当我使用Weblogic Portal应用程序时,我需要在Eclipse上保留一些共享库(项目和运行时都需要的共享库)。可能有些图书馆在我的pom上被遗忘了。奇怪的是,编译声称不存在的包,存在。

解决方案

因为我已经不多了选项,我按照@ 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>

之后,模块3开始编译,但模块4和5由于某些JRockit的依赖而破坏库。为了解决这个问题,我最终更新了已弃用的代码,最终完全编译了项目。



所以我认为我使用的Maven版本存在问题。不知何故,当2个项目依赖于同一个lib并且其中一个项目依赖于另一个项目时,它似乎会丢失。我没有测试过其他版本,但3.3.9适用于这些情况。


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:

> + 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.

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.

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

The only particularity of my project is that module 3 depends on module 2 and depends on a lib that module 2 also depends.

Below I'm pasting the poms from both module 2 and 3. I've changed some names, because of company policy.

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

<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.

It looks like that somehow the dependencies are getting lost since the Module3 depends on them both.

I'm using Java 1.6.0_29 and 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.

Update 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.

Update 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!

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. 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.

解决方案

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.

<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.

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编译错误:包不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆