Maven:无法解析依赖项(未找到工件)

Maven: Could not resolve dependencies (artifact not found)

我正在尝试使用 Maven 构建 moquette,作为 Maven 的新手很难。

我正在使用以下命令进行构建。

mvn clean install -U

mvn clean install -U | grep ERROR

结果如下:

[ERROR] Failed to execute goal on project moquette-broker: Could not resolve dependencies for project org.eclipse.moquette:moquette-broker:jar:0.7-SNAPSHOT: Could not find artifact org.mapdb:mapdb:jar:1.1.0-SNAPSHOT in Paho Releases (https://repo.eclipse.org/content/repositories/paho-releases/) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :moquette-broker

完整输出:

mvn clean install -e -X -U

可以找到here

我的pom.xml看起来像:

<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>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <netty.version>4.0.24.Final</netty.version>
        <source.version>1.7</source.version>
        <target.version>1.7</target.version>
    </properties>

    <groupId>org.eclipse.moquette</groupId>
    <artifactId>moquette-parent</artifactId>

    <packaging>pom</packaging>
    <version>0.7-SNAPSHOT</version>
    <name>Moquette MQTT parent</name>
    <url>http://code.google.com/p/moquette-mqtt/</url>


    <modules>
        <module>parser_commons</module>
        <module>netty_parser</module>
        <module>broker</module>
        <module>distribution</module>
        <module>bundle</module>
    </modules>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.6</version>
            </plugin>
        </plugins>
    </reporting>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${source.version}</source>
                    <target>${target.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

是什么导致了这个问题,我该如何解决?

根据 Moquette 的 documentation,一个简单的 mvn clean install 应该可以做到:

After a git clone of the repository, cd into the cloned sources and: mvn clean package. In distribution/target directory will be produced the selfcontained tar for the broker with all dependencies and a running script.

换句话说,你做的一切都是对的。

但是,缺少依赖项 org.mapdb:mapdb:jar:1.1.0-SNAPSHOT(截至 2015 年 1 月 20 日)。也就是说,安装说明不够。

通过引用 MapDB documentation,他们将夜间构建发布到存储库。如果您将其添加为存储库,它将起作用(我自己刚刚验证了这一点):

<repositories>
    <repository>
        <id>sonatype-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
</repositories>

你可以直接把这个定义放在你的pom文件里,或者在maven安装的settings.xml文件里配置,按照说明here.

因此对于您的 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <repositories>
        <repository>
            <id>sonatype-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </repository>
    </repositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <netty.version>4.0.24.Final</netty.version>
        <source.version>1.7</source.version>
        <target.version>1.7</target.version>
    </properties>

    <groupId>org.eclipse.moquette</groupId>
    <artifactId>moquette-parent</artifactId>

    <packaging>pom</packaging>
    <version>0.7-SNAPSHOT</version>
    <name>Moquette MQTT parent</name>
    <url>http://code.google.com/p/moquette-mqtt/</url>


    <modules>
        <module>parser_commons</module>
        <module>netty_parser</module>
        <module>broker</module>
        <module>distribution</module>
        <module>bundle</module>
    </modules>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.6</version>
            </plugin>
        </plugins>
    </reporting>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${source.version}</source>
                    <target>${target.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

为了进一步解释这一点,maven 在配置的存储库中检查所需的工件。在大多数情况下,工件存在于 "default" 个存储库中,不需要额外的存储库。

另一方面,假设您构建了自己的 Maven 工件,并托管了自己的 Maven 存储库。您将工件发布到该存储库。现在,如果其他用户想要使用它,他们将不得不进行与上述类似的配置。

顺便说一下,-U 强制更新,除非你真的想强制 maven download/re-download 依赖项,否则不需要更新。