How to add local project (not jar) as a dependency to a Maven project

Viewed 24876

I have two Maven projects that I've added to one Intellij Idea's project as two modules. Project B depends on Project A.

Here are simplified versions of their pom.xml files.

Project A:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.group</groupId>
    <artifactId>a</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
    </dependencies>
</project>

Project B:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.group</groupId>
    <artifactId>b</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.group</groupId>
            <artifactId>a</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Project A compiles easily since it has no dependency. But Project B depends on Project A and since I'm not telling maven how to find it, it can not be compiled with mvn package. But if I'm not mistaken, this was possible using Intellij Idea's "Meven Projects" menu since to Intellij Idea both projects are known.

But right now, for some unknown reason, I can not compile project B even in Intellij Idea. When I do, it prompts:

[ERROR] Failed to execute goal on project b: Could not resolve dependencies for project com.group:b:jar:1.0-SNAPSHOT: Could not find artifact com.group:a:jar:1.0-SNAPSHOT -> [Help 1]

My question is, how can I include one project into other as its dependency? Please note that I'm not looking for a local dependency injection in maven since I want this to work in the future when I upload my packages into some repository.

2 Answers

Since I came here searxing for the same problem with Eclipse and it is mentioned in some comments (where it seems the IntelliJ focus is not obvious) I want to point to this solution for Eclipse that may be similar to IntelliJ as well: https://stackoverflow.com/a/10696646/1915920

The crucial point for us was the dependent projects pom.xml version match (in the dependent and depending pom.xml) in the workspace, so the workspace resolution works.

Related