19

I googled this and it seems that no one has an answer, yet it seems like such an elementary thing that it should be possible.

I have the following project structure:

parent
   ---sub-project1
   ---sub-project2

sub-project2 needs to have sub-project1 as a dependency.

So I have this in sub-project2's pom:

 <dependencies>
    <dependency>
         <artifactId>sub-project1</artifactId>
        <groupId>mygroup</groupId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

....

When I do this, Maven tries to dowload the sub-project1.jar file, which does not exist because it's not ready for the repo yet.

I tried to put a <scope>import</scope> in the dependency, but that didn't work either -- same result.

So what do I have to do to get Maven to look at sub-project1 when building sub-project2?

EDIT Here are some pom snippets:

Parent:

<project 
   xmlns="http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0          
    http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <prerequisites>
      <maven>2.0.9</maven>
   </prerequisites>
   <modules>
    <module>sub-project1</module>
    <module>sub-project2</module>
   </modules>
 ....

sub-project1:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>parent</artifactId>
    <groupId>mygroup</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>sub-project1</artifactId>
....

sub-project2:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>parent</artifactId>
    <groupId>mygroup</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

   <artifactId>sub-project1</artifactId>

    <dependencies>
     ....
       <dependency>
          <artifactId>sub-project2</artifactId>
          <groupId>mygroup</groupId>
          <version>1.0-SNAPSHOT</version>
          <scope>import</scope>
      </dependency>
  </dependencies>

The error I'm getting when I got mvn clean install on the parent is:

[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure

With a lot of classes/package not found errors

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
MikeHoss
  • 1,477
  • 4
  • 18
  • 35
  • 1
    Are you specifying the groupId and version of the subprojects? And you don't need the import scope. – mgv Apr 06 '10 at 14:49

2 Answers2

20

You should have a master pom at parent's level, in which you will list the modules of your project.

  <modules>
    <module>sub-project1</module>
    <module>sub-project2</module>>
  </modules>

In each subproject you have to reference your parent:

<parent>
    <artifactId>parent</artifactId>
    <groupId>mygroup</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

And you specify the dependencies between the project just as you did. I think you've missed some of the steps I've described.

Edit: you should issue your mvn clean install at the parent level.

mgv
  • 8,384
  • 3
  • 43
  • 47
  • No I Have all that. But I know I'm missing something so I'll just post some pom snippets. – MikeHoss Apr 06 '10 at 14:24
  • You do not need to use the import scope. I'm already using multimodule projects without a problem, post your POMs so we can take a look! :) – mgv Apr 06 '10 at 14:26
  • 1
    Actually, you don't have to reference the parent (even if this is most often the case), inheritance and aggregation are separated concepts – Pascal Thivent Apr 06 '10 at 16:34
  • in my case when i try to use submodule1 class to my submodule2, i get error in submodule1 class as if its required dependency jar is not found...any suggestion? – H Raval Mar 11 '17 at 05:21
  • I have somewhat similar situation, but I need sub-project1 dependency in parent project. I have parent, dependency set as exactly mentioned here. Any leads on how we can resolve this? Let me know if I should post poms here. – Kiran A B Mar 14 '17 at 15:48
10

When I do this, Maven tries to dowload the sub-project1.jar file, which does not exist because it's not ready for the repo yet.

That's the normal behavior, Maven resolves dependencies through the local repository so you need to install sub-project1 first. Actually, the common way to deal with this kind of situation is to launch a reactor build (a multi-modules build) from the parent.

Assuming you are aggregating modules in the parent i.e. you have something like this declared in the "parent" pom.xml:

<modules>
  <module>sub-project1</module>
  <module>sub-project2</module>>
</modules>

Just cd into the parent directory and launch a reactor build:

$ cd parent
$ mvn install

Maven will then calculate the build order (deducted from the oriented graph made of modules and their dependencies) and run install on all modules in the calculated order (parent first, then sub-project1 and finally sub-project2 for your particular example).

But don't use a scope of type import, you are misusing it here. Remove it.

Update: The question has been updated while I was answering and the POMs shown do no illustrate the situation given in the original question (reversed dependency, probable mistake in the artifact id). But the suggested approach still applies. Remove the <scope>import</scope> on the dependency and start a reactor build from the parent.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • in my case when i try to use submodule1 class to my submodule2, i get error in submodule1 class as if its required dependency jar is not found...any suggestion? – H Raval Mar 11 '17 at 05:21