Maven compilation error: package does not exist
Asked Answered
S

3

11

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.

Suckow answered 4/4, 2017 at 18:16 Comment(7)
Could you print the debug output (containing the error) with the X switchSaurel
I can't paste the whole output because of some names that the company doesn't allow me to make public. I'm editing the question with the error that is gives. When I run with the -X I see the jar that I need in the classpath, the Java and Maven version are alright and it goes as expected since the compilation errorSuckow
FWIW, it should be possible to upgrade both Java and Maven for builds and use old Java for runtime. You just need to ensure that the Java source and target are configured to 1.6 in your POMs, You can do this manually via configuring individual plugins (Compiler, Surefire, etc.) or by using Toolchains. If you're concerned about new Java stuff sneaking in, use the animal-sniffer plugin and/or maven-enforcer to break the build if newer classes are used.Palaeography
Did you ever figure this out? I'm having a similar project with a parent project with two children, child A depends on child B, and child A can't see child B's classes/packages.Mahdi
@Mahdi I couldn't figure it out yet. So I'm using the (very bad) approach that I mentioned on update 2. It compiles and packages, but I got a dependency on Eclipse.Suckow
@Suckow I resolved my problem, maybe it helps you: #43358516Mahdi
@Mahdi I finally figured out the problem. Posted it as answer. Hope it fixes your problem as well. Just noticed your problem was different. NevermindSuckow
B
0

I had the same issue, i build the project with clean install and selected : resolve workspace artifacts and the project build successfully.

Bernardina answered 12/8, 2022 at 6:28 Comment(0)
P
0

For my multimodule project, I had to run the maven commands from the root directory containing all the modules, and also specify the build/settings.xml

Philender answered 16/8, 2024 at 21:2 Comment(0)
S
-2

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.

Suckow answered 20/4, 2017 at 14:38 Comment(2)
Shouldn't you have "<groupId>org.apache.maven.plugins</groupId>" before the artifactId tags ?Coper
Generally yes, but org.apache.maven.plugins is the default value and so does not have to be provided.Zobias

© 2022 - 2025 — McMap. All rights reserved.