Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make one module depend on another module artifact?

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> .... 
like image 838
David Avatar asked Nov 10 '10 23:11

David


People also ask

Can two modules depend on each other?

In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.

How do I add one project as dependency in Maven?

Right-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.

Can we have two pom xml?

Yes you can use Maven Profiles to manage this. Obviously you can tweak this approach to suit your needs however works best.


2 Answers

Try ${project.version}

e.g.

<dependency>    <groupId>AAA</groupId>    <artifactId>B</artifactId>    <version>${project.version}</version> </dependency> 
like image 197
Justin Patel Avatar answered Sep 18 '22 06:09

Justin Patel


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

like image 41
Matt McHenry Avatar answered Sep 20 '22 06:09

Matt McHenry