简体   繁体   English

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

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

I have two Maven projects that I've added to one Intellij Idea's project as two modules.我有两个 Maven 项目,我已将它们作为两个模块添加到一个 Intellij Idea 的项目中。 Project B depends on Project A.项目 B 依赖于项目 A。

Here are simplified versions of their pom.xml files.这是他们的 pom.xml 文件的简化版本。

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:项目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.项目 A 很容易编译,因为它没有依赖项。 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 .但是项目 B 取决于项目 A,并且由于我没有告诉 maven 如何找到它,因此无法使用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.但如果我没记错的话,这可以使用 Intellij Idea 的“Meven Projects”菜单,因为对于 Intellij Idea,这两个项目都是已知的。

But right now, for some unknown reason, I can not compile project B even in Intellij Idea.但是现在,由于某种未知的原因,即使在 Intellij Idea 中我也无法编译项目 B。 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.请注意,我不是在 maven 中寻找本地依赖注入,因为我希望将来当我将包上传到某个存储库时它可以工作。

Your project B depends on project A and you have already specified this as a dependency in your pom file. 您的项目B依赖于项目A,并且您已将其指定为pom文件中的依赖项。 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. 项目B无法找到项目A的唯一原因可能是因为项目未在本地.m2存储库中安装工件。 So you have to first do a mvn clean install of your project A which will create the artifact in your maven repository. 因此,您必须首先对项目A执行mvn clean安装,这将在您的maven存储库中创建工件。 You should be then able to use artifact A in project B. 然后,您应该可以在项目B中使用工件A.

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由于我来这里是在与Z32F7222222226696F30787889194DEE83E5Z一起解决的同一问题,因此在某些评论中提到(其中Intellij焦点似乎并不明显),我想指向Z322F72222222222222222222222222222222222222222222222222222222222222222222222222222222202696f30788888888888883E1919483EESEE: /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.对我们来说,关键点是工作区中的依赖项目pom.xml版本匹配(在依赖和依赖pom.xml中) ,因此工作区分辨率有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM