25

I have a module whose pom file is:

<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>common module</name>

In that artifact ('common'), I have a package named com.mycompany.common.objects. In the consuming package, my pom file is:

 <dependency>
            <groupId>com.mycompany.Common</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>pom</type>
 </dependency>

When I run mvn install it always complain: package com.mycompany.common.objects does not exist.

I tried explicit importing in the class where the error was:

import com.mycompany.common.objects

No luck. I tried in both the IDE (IntelliJ) and in commandline. Any idea? thanks

EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126

10 Answers10

23

While working with IntellijIDEA, generated files can cause this issue. Writing

mvn idea:idea

on IntellijIDEA Maven console to reset those files did the trick for me. Also, see: Package doesn't exist error in intelliJ

Onat Korucu
  • 992
  • 11
  • 13
13

From your sample, we cannot see any artifact containing the package com.mycompany.common.objects you are using.

You are adding dependency com.mycompany.Common:common as a POM (and you are declaring the packaging of com.mycompany.Common:common as POM too). If it is actually a JAR artifact that contains the package you need to use, then remove the packaging from the POM and dependency (which means, using default which is JAR).

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
12

For anyone struggling with this and not familiar with java, make sure that the said package exists in your local repository. Maven has a local repository ~/.m2 where the packages are installed for local access, so even if your dependency package is correctly declared as a dependency in pom.xml and is compiled and exists in your project, if it does not exist in the local repository, the mvn compile will trigger a "package does not exist" error.

To fix this:

In the missing package folder, do:

mvn install //--> this will package and install your missing package in the local repo

Then in your project that you wanted to compile:

mvn compile // --> now that the missing package is in the local repo it should work

eloone
  • 4,248
  • 2
  • 32
  • 35
  • 1
    Thanks, this helped me - I had 1 project that was dependent on 7 other projects, mvn couldn't find them. I converted them all to maven projects, did the mvn install - and now the original project compiles properly! – JGlass Oct 23 '19 at 19:12
2

Please correct me, If I'm wrong. I understand that the common is a POM that defines several dependencies which intents to be used by other modules. The Importing Dependencies may meet your requirement.

For example

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.mycompany.Common</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

I hope this may help.

Charlee Chitsuk
  • 8,847
  • 2
  • 56
  • 71
2

I had the same problem recently. Everything in my project was setup correctly with dependencies etc. I tried removing /target dirs but nothing worked.

Finally, I solved it by removing the Module Dependency from my dependent project and then readding the dependency. Not sure what is going on in the background, but some sort of refresh of the classpath must have been made. Perhaps the problem was due to the Maven setup.

Hope it helps someone who reaches this question from a search engine.

H.Rabiee
  • 4,747
  • 3
  • 23
  • 35
1

Your IDE (Eclipse in my case) may not distinguish between compile and runtime scope. This means that the IDE will let you use runtime scope dependencies in your code, but maven won't. In such a such change the dependency scope from runtime to compile.

wytten
  • 2,800
  • 1
  • 21
  • 38
0

Not sure if there was file corruption or what, but after confirming proper pom configuration I was able to resolve this issue by deleting the jar from my local m2 repository, forcing Maven to download it again when I ran the tests.

eebbesen
  • 5,070
  • 8
  • 48
  • 70
0

For me the problem was with the sourceDirectory and testSourceDirectory nodes in my pom.xml. I was using

<sourceDirectory>${basedir}/src/test</sourceDirectory>
<testSourceDirectory>${basedir}/test</testSourceDirectory>

and changed it to

<sourceDirectory>../src/test/java</sourceDirectory>
<testSourceDirectory>../src/test/java</testSourceDirectory>
j4nus
  • 1
  • 3
0
  • In my case, I was trying to build a module alone and got this error: package x does not exist.
  • But module1 depended on another module that had this package.
  • The right way to build module1 was to use the -am option.
  • --also-make -am Builds the specified modules, and any of their dependencies in the reactor. Source
  • WRONG: mvn package -pl 'module1'
  • CORRECT: mvn package -pl 'module1' -am
Loner
  • 166
  • 1
  • 4
  • 10
-1

you need to add the maven-plugin into (each) child module (for compiling main and test source)

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
     <plugins>

and then you add the plugin-management into the parent pom, for centralizing the plugin config (version...)

        <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
         </pluginManagement>

Then you can add your dependency into the dependent module pom

<dependency>
        <groupId>com.mycompany.Common</groupId>
        <artifactId>common</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>pom</type>
</dependency>

http://www.jouvinio.net/wiki/index.php/Projet_Maven_multi-modules