Maven 项目依赖项 "could not find artifact"

标签 maven intellij-idea pom.xml

我有 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。我需要把它放在我的项目中吗?它应该位于我的文件结构中的什么位置。

对于它的值(value),我使用的是 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>

最佳答案

您必须安装 ProjectA在您的本地 Maven 存储库中(或以其他方式使其在您的 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中的依赖声明,去掉这一行:<type>pom</type> .

关于Maven 项目依赖项 "could not find artifact",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46181465/

相关文章:

java - war 中的类(Class)有没有办法从它的耳朵访问 META-INF?

maven - 仅为特定子模块/项目执行 maven-release-plugin 部署步骤

java - 如何使 .jar 可执行?

java - 如何在普通java项目中使用maven项目jar

eclipse - 我如何(如果可能)使用 Intellij IDEA 键盘快捷键配置 Eclipse?

java - 什么相当于 intellij 中的 eclipse 部署程序集 list 条目?

java - 源和目标的 Maven 编译器插件 jdk 版本

maven - Intellij maven 依赖更喜欢本地代码

eclipse - 运行 Jersey REST 时出现 404 错误

java - Cassandra 连接失败