Maven compilation error: package does not exist

2019-05-04 00:43发布

问题:

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.

回答1:

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.