EyeQ Tech
EyeQ Tech

Reputation: 7358

Maven, package does not exist

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 on the command line. Any ideas? Thanks

Upvotes: 27

Views: 127242

Answers (11)

Russell
Russell

Reputation: 11

Adding plugin management in pom.xml of my jar file worked for me.

Upvotes: 0

Loner
Loner

Reputation: 309

  • 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

Upvotes: 0

wytten
wytten

Reputation: 3000

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.

Upvotes: 1

Onat Korucu
Onat Korucu

Reputation: 1032

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

Upvotes: 23

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

Upvotes: -1

eloone
eloone

Reputation: 4344

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

Upvotes: 13

j4nus
j4nus

Reputation: 1

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>

Upvotes: -1

eebbesen
eebbesen

Reputation: 5148

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.

Upvotes: 0

H.Rabiee
H.Rabiee

Reputation: 4837

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.

Upvotes: 2

Charlee Chitsuk
Charlee Chitsuk

Reputation: 9059

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.

Upvotes: 2

Adrian Shum
Adrian Shum

Reputation: 40036

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

Upvotes: 13

Related Questions