【问题标题】:Maven project dependency "could not find artifact"Maven 项目依赖“找不到工件”
【发布时间】:2018-02-21 05:11:19
【问题描述】:

我有 2 个 Maven 项目。项目 A 和项目 B。项目 B 依赖于项目 A,但不是相反。

在我的项目 B pom 中,我有这个:

    <dependency>
        <groupId>com</groupId>
        <artifactId>ProjectA</artifactId>
        <type>pom</type>
        <version>1.0-SNAPSHOT</version>
    </dependency>

当我尝试打包项目时失败并出现以下错误:

 [ERROR] Failed to execute goal on project ProjectB: Could not resolve    dependencies for project com:ProjectB:war:1.0-SNAPSHOT: Could not find  artifact com:ProjectA:pom:1.0-SNAPSHOT -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project ProjectB: Could not resolve dependencies for project com:ProjectB:war:1.0-SNAPSHOT: Could not find artifact com:ProjectA:pom:1.0-SNAPSHOT

所以它找不到我的 ProjectA pom。我需要把它放在我的项目中吗?它应该位于我的文件结构中的什么位置。

不管怎样,我使用的是 intelliJ IDE。

提前致谢。

编辑:当我在 projectA 上运行安装时,出现此错误:

The packaging for this project did not assign a file to the build artifact

EDIT2 - 添加 ProjectA pom:

<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>
<groupId>edu</groupId>
<artifactId>ProjectA</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ProjectA</name>

<build>
    <sourceDirectory>src/main/java</sourceDirectory>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <id>install-jar-lib</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>prepare-package</id>
                    <phase>prepare-package</phase>
                    <configuration>
                        <target>
                            <copy file="${basedir}/src/main/webapp/META-INF/context-${app.environment}.xml"
                                  tofile="${basedir}/src/main/webapp/META-INF/context.xml" />
                            <copy file="${basedir}/src/main/webapp/WEB-INF/web-${app.environment}.xml"
                                  tofile="${basedir}/src/main/webapp/WEB-INF/web.xml" />
                            <copy file="${basedir}/src/main/resources/log4j-${app.environment}.xml"
                                  tofile="${basedir}/src/main/resources/log4j.xml" />
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <phase>compile</phase>
                    <configuration>
                        <target>
                            <copy file="${project.build.directory}/classes/log4j-${app.environment}.xml"
                                  tofile="${project.build.directory}/classes/log4j.xml" />
                            <delete>
                                <fileset dir="${project.build.directory}/classes" includes="**/*-local.*"/>
                                <fileset dir="${project.build.directory}/classes" includes="**/*-test.*"/>
                                <fileset dir="${project.build.directory}/classes" includes="**/*-prod.*"/>
                            </delete>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>package</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <delete file="${basedir}/src/main/webapp/META-INF/context.xml" />
                            <delete file="${basedir}/src/main/webapp/WEB-INF/web.xml" />
                            <tstamp>
                                <format property="last.timestamp" pattern="yyyyMMddHHmmss"/>
                            </tstamp>
                            <property name="build.time" value="${last.timestamp}" />
                        </target>
                        <exportAntProperties>true</exportAntProperties>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <failOnMissingWebXml>true</failOnMissingWebXml>
                <!--<webResources>
                    <resource>
                        <directory>src/main/WEB-INF/lib</directory>
                        <targetPath>WEB-INF/lib</targetPath>
                        <filtering>false</filtering>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>-->
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.coderplus.maven.plugins</groupId>
            <artifactId>copy-rename-maven-plugin</artifactId>
            <version>1.0.1</version>
            <executions>
                <execution>
                    <id>rename-file</id>
                    <phase>package</phase>
                    <goals>
                        <goal>rename</goal>
                    </goals>
                    <configuration>
                        <sourceFile>${project.build.directory}/${artifactId}-${version}.war</sourceFile>
                        <destinationFile>${project.build.directory}/ProjectA##${build.time}.war</destinationFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【问题讨论】:

    标签: maven intellij-idea pom.xml


    【解决方案1】:

    您必须在本地 Maven 存储库中安装 ProjectA(或者以其他方式使其在您的 pom.xml 或 settings.xml 指向的任何远程存储库中可用)。例如:

    cd whereever/projectA/lives
    mvn clean install
    

    这会将com/ProjectA/ProjectA.pom 写入您的本地 Maven 存储库,当您执行此操作时...

    cd wherever/projectB/lives
    mvn clean install
    

    ...Maven 将从该位置解析ProjectA.pom

    注意:你在ProjectA上声明的依赖:

    <dependency>
        <groupId>com</groupId>
        <artifactId>ProjectA</artifactId>
        <type>pom</type>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    

    ... 会将com:ProjectA 中声明的所有依赖项传递到ProjectB 的POM,这绝对是您的意图吗?这仅在 ProjectA 被打包为 POM 时才有意义,如果它被打包为 JAR,则需要更新 ProjectB 中的依赖声明以删除此行:&lt;type&gt;pom&lt;/type&gt;

    【讨论】:

    • 感谢您的回答。所以我对安装有点不清楚,每当我运行那个 maven 命令时都会出错。同样对于你的最后一个问题,我正在构建一个战争神器,所以我删除了 pom 部分
    • 您写道:“我对安装有点不清楚,每当我运行该 maven 命令时都会出现错误”,如果您需要帮助,请更新您的问题以准确描述该错误。重新评论:“我正在构建一个战争工件,所以我删除了 pom”如果项目 A 是一个 WAR,那么你不能声明对它的依赖,你 依赖一个 JAR,你运行一个 WAR。
    • 知道了,我相信我确实需要依赖 pom,因为该项目不是作为 jar 构建的。我编辑了我的原始帖子来描述我在 projectA 上运行 install 命令时遇到的错误
    • 是您的最新更新(此项目的打包未将文件分配给构建工件),任何试图为您诊断的人都需要查看 ProjectA POM . ProjectA 很可能没有生成任何工件,因此没有什么要安装。感觉这可能是一个问题变成了几个子问题:)
    • 是的,它肯定会变成那样。我会发布我的 pom,但如果问题的范围太大,我可能只需要在发布之前做一些研究
    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 2016-10-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多