Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure a subproject dependency in Maven without deploying jars?

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

like image 936
MikeHoss Avatar asked Apr 06 '10 13:04

MikeHoss


People also ask

Can I add JARs to Maven 2 build classpath without installing them?

It should be enough to create a single POM to add all of the JARs providing you have a execution block for each jar you want to add. You just need to make sure that each block has a unique id. The result is a single Maven module that will add all the JARs into the local repo.

How do you add a project dependency in POM xml?

Add a Java Maven Dependency to the Utility Project field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK. Expand the utility project, right-click the pom. xml file, and select Run As>Maven Install to install the file into the local repository.

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.


1 Answers

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.

like image 185
mgv Avatar answered Oct 14 '22 10:10

mgv