73

I have maven multiple-module project.

 A: parent.
    B: child1.
    C: child2.

B will be packaged to get jar file and then c will use this jar file to compile the code.

In B, if I run mvn package, it will create b.jar (stays in B/target/jars not in B/target -for another purpose).

In C, I need to use that b.jar to compile the code.

Now, from A, when I run: mvn package. First, I am successful to create b.jar file for B.

But when it come to C's compilation phase, it looks like C doesn't recognize b.jar in the classpath (the compilation gets errors because C's code can not import the class file from B).

My question is: How can I solve this problem?

---------- Below are the pom files

A: pom.xml
  <groupId>AAA</groupId>
  <artifactId>A</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

   <modules>
   <module>C</module>
   <module>B</module>
   </modules>

B: pom.xml
        <groupId>AAA</groupId>
 <artifactId>B</artifactId>
 <packaging>jar</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <parent>
  <artifactId>A</artifactId>
  <groupId>AAA</groupId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>

C: pom.xml
       <parent>
  <artifactId>A</artifactId>
  <groupId>AAA</groupId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>

 <groupId>AAA</groupId>
 <artifactId>C</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>

 <dependencies>

  <dependency>
   <groupId>AAA</groupId>
   <artifactId>B</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
....
1
  • 13
    Just as a best practice (unrelated to this question), modules B and C should not declare their own groupId and version; they should inherit these from the parent (module A) POM. They still need to declare their own packaging and artifactId though. Commented Dec 8, 2010 at 0:17

7 Answers 7

51

Try ${project.parent.version}

e.g.

<dependency>
   <groupId>AAA</groupId>
   <artifactId>B</artifactId>
   <version>${project.parent.version}</version>
</dependency>
4
  • 2
    This solved my issue. After going through lot of articles, I think this is the correct practice. Only thing to note is, your module version needs to be in sync with the version of dependent module. Commented Feb 21, 2019 at 6:20
  • 6
    Just want to notice that ${project.version} is deprecated now. Use: ${project.parent.version} in new projects
    – xardbaiz
    Commented Oct 7, 2021 at 6:54
  • 3
    Why does this work? Wouldn't this expression evaluate to the same value as what the OP already had (0.0.1-SNAPSHOT) ? Commented Apr 4, 2022 at 4:59
  • It would be very helpful to give an explanation as why this works, and how different is it from the original solution. Commented Jul 2 at 8:01
32

Looks like it should work to me. But you might try mvn install instead of mvn package.

1
  • 1
    Thanks. It is a good idea. But if I go with install then c war file will also be installed. That one is not accepted in my current project. Only jar file is allowed to install
    – David
    Commented Nov 11, 2010 at 4:46
12

My question is how I can solve this problem?

Dependency resolution is done through the local repository so the canonical way to "solve" the problem is to run install from A so that modules will get installed in the local repository.

Now, regarding the following comment

But if I go with install then c war file will also be installed. That one is not accepted in my current project".

Sure, I'm not on your project, I don't know all constraints and rules. But if you decide to use Maven, this is a totally ridiculous policy (seriously, WTF?) and using a system scoped dependency is certainly not a good solution (more troubles later guaranteed). If this policy is real, better not use Maven in that case.

1
  • 1
    Oh, you are right. If install only copy the artifact to local repository, that is okay to me. I though install will copy the artifact to remote repository (i confused between install and deploy). Thanks.
    – David
    Commented Nov 14, 2010 at 22:11
4

i have a solution: using the dependency with the scope=system

in C pom.xml

           <dependency>
            <groupId>AAA</groupId>
            <artifactId>B</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${basedir}\..\B\target\jars\b.jar</systemPath>
        </dependency>

and in A pom.xml, put module B on the top like this

<modules>
       <module>B</module>       
       <module>C</module>
 </modules>
1
  • 12
    David, I have no idea of what you're doing and why "war is not accepted in your current project" but, not offense, this is definitely not the right way. Commented Nov 12, 2010 at 7:06
1

Doing mvn install only places the artifact into the local .m2 repository of the machine you're running the command on. How can that not be acceptable? I agree with Pascal. If you building A, there should be no reason that a the war is placed there.

On the other hand, if you're using Maven 2.2.x, take a look at the maven reactor plugin? This should help the crazy unacceptable cannot install C.war into your local .m2 repository policy for the current project.

1
  • Oh, you are right. If install only copy the artifact to local repository, that is okay to me. I though install will copy the artifact to remote repository (i confused between install and deploy). Thanks.
    – David
    Commented Nov 14, 2010 at 22:09
0

If you have moduleA on your machine say at D:\moduleA and inside moduleA you have created another module say moduleB at D:\moduleA\moduleB , for you to use moduleB inside moduleA you create a dependency in the pom.xml file of moduleA like so:

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    ...
    <groupId>net.passioncloud</groupId>
    <artifactId>moduleA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>net.passioncloud</groupId>
            <artifactId>moduleB</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
     <dependencies>
     ...

To compile the project so you can use it, from the module folder (moduleB) do:

.\mvnw clean install
-1

Here are what I did to solve it:

  1. From intelij , create new module from existing source.
  2. Change the version of dependency B in A.pom same as version of B in B.pom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.