java 如何将本地项目(不是 jar)添加为 Maven 项目的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44074179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:54:52  来源:igfitidea点击:

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

javamavenintellij-idea

提问by Mehran

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

我有两个 Maven 项目,我已将它们作为两个模块添加到一个 Intellij Idea 的项目中。项目 B 依赖于项目 A。

Here are simplified versions of their pom.xml files.

这是他们的 pom.xml 文件的简化版本。

Project A:

项目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.

项目 A 很容易编译,因为它没有依赖性。但是项目 B 依赖于项目 A,因为我没有告诉 Maven 如何找到它,所以它不能用mvn package. 但是,如果我没记错的话,使用 Intellij Idea 的“Meven Projects”菜单是可能的,因为 Intellij Idea 两个项目都是已知的。

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

但是现在,由于某种未知的原因,即使在 Intellij Idea 中我也无法编译项目 B。当我这样做时,它提示:

[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.

我的问题是,如何将一个项目作为其依赖项包含在另一个项目中?请注意,我不是在 Maven 中寻找本地依赖项注入,因为我希望将来当我将我的包上传到某个存储库时它可以工作。

回答by Nitin Prabhu

Your project B depends on project A and you have already specified this as a dependency in your pom file. The only reason project B is not able to find project A may be due because of project A artifact is not installed in your local .m2 repository. So you have to first do a mvn clean install of your project A which will create the artifact in your maven repository. You should be then able to use artifact A in project B.

您的项目 B 依赖于项目 A,并且您已经在 pom 文件中将其指定为依赖项。项目 B 无法找到项目 A 的唯一原因可能是因为项目 A 工件未安装在您的本地 .m2 存储库中。因此,您必须首先对项目 A 进行 mvn 全新安装,这将在您的 Maven 存储库中创建工件。然后您应该能够在项目 B 中使用工件 A。