Skip to content
Advertisement

Add maven project as dependency in maven project

I have two Maven projects, both created from the default Maven structure in Eclipse.

Project A is dependant on Project B. In order to utilise classes from A in B, I can add B to A’s build path.

How can I achieve the same effect using Maven?

Currently, I have both of the projects on my file system, but I would like to add this dependency in as similar a way as possible to, for example, adding GSON from a remote repository (Fig. 1), as that is how I would like to handle my own repositories in future.

Currently, when I try this:

<dependency>
    <groupId>vision.voltsofdoom</groupId>
    <artifactId>voltsofdoom</artifactId>
    <version>0.0.1</version>
    <type>pom</type>
</dependency>

… and remove any other projects from the build path, Eclipse is unable to resolve any of my imports (everything works fine when I add Project B (voltsofdoom) to A’s (casketofazamgarath) build path.

B’s (voltsofdoom) basic pom looks like this.

    <groupId>vision.voltsofdoom</groupId>
    <artifactId>voltsofdoom</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>

B (voltsofdoom) does not appear in the “Local Repository” tab in the “Maven Repositories” View, instead appearing in “Workspace Projects”.

Figure 1 (Adding GSON):

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency>

==

Answer: Follow the correct marked answer. The amended snippet is:

<dependency>
    <groupId>vision.voltsofdoom</groupId>
    <artifactId>voltsofdoom</artifactId>
    <version>0.0.1</version>
</dependency>

Advertisement

Answer

You can run the maven clean install goal on the project that has no dependency on the other (i.e. the “dependency”), which will “export” your project to your local .m2 directory in your users directory.

After that, you should be able to access it from the depending project with the code snippet you mentioned in your question (although I’m not quite sure what the <type> tag does).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement