为什么在尝试使用 Maven 程序集插件时出现此错误消息?

Why I obtain this error message when I try to use the Maven assembly plugin?

我完全是 Maven 的新手,我正在学习教程。

在本教程中,我有以下 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>

    <groupId>com.denofprogramming</groupId>
    <artifactId>maventutorial1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MavenTutorial1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <descriptors>
                        <descriptor>jar-with-dependencies</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>3.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

正如您在这个配置文件中看到的那样,它被声明使用 assembly 插件,这部分:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <descriptors>
                    <descriptor>jar-with-dependencies</descriptor>
                </descriptors>
            </configuration>
        </plugin>

作为 descripor 字段,它被设置为 jar-with-dependencies 值(据我所知)必须创建一个新的目标 jar 文件,其中还包含我的项目的 delcared .jar 依赖项。

所以,在 Eclipse 中,我 select 我的项目和 运行 as ---> Maven Build 并进入 Goal 输入部分我写了这个语句:

clean package assembly:single

必须清理我的项目的目标目录,创建包并在 assembly 插件上调用 single 目标jar 依赖到我项目的最终 jar 目标文件中。

问题是这样做我在 Eclipse 控制台中收到以下错误消息:

.......................................................
.......................................................
.......................................................

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maventutorial1 ---
[INFO] Building jar: /home/andrea/git/maven-java-console-application/mvntutorial1/target/maventutorial1-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-assembly-plugin:2.5.5:single (default-cli) @ maventutorial1 ---
[INFO] Reading assembly descriptor: jar-with-dependencies
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.767 s
[INFO] Finished at: 2015-07-20T15:12:19+01:00
[INFO] Final Memory: 19M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.5.5:single (default-cli) on project maventutorial1: Error reading assemblies: Error locating assembly descriptor: jar-with-dependencies
[ERROR] 
[ERROR] [1] [INFO] Searching for file location: /home/andrea/git/maven-java-console-application/mvntutorial1/jar-with-dependencies
[ERROR] 
[ERROR] [2] [INFO] File: /home/andrea/git/maven-java-console-application/mvntutorial1/jar-with-dependencies does not exist.
[ERROR] 
[ERROR] [3] [INFO] File: /home/andrea/git/maven-java-console-application/mvntutorial1/jar-with-dependencies does not exist.
[ERROR] -> [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/MojoExecutionException

好像找不到这个文件:/home/andrea/git/maven-java-console-application/mvntutorial1/jar-with-dependencies

但是根据我对教程的理解,这不是一个配置文件,而是一个特定的 Maven 值,表示将所有 .jar 依赖项放入代表我打包项目的最终目标 jar 文件中。

为什么会出现这个错误?我错过了什么?我该如何解决?

这就是我的 maven POM 在插件方面的样子。

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target> 
                </configuration>
            </plugin>
        </plugins>
    </build>

我只能假设两件事:

  1. 您可能没有正确格式化它,或者您使用的 maven 版本不支持您想要执行的操作。
  2. 您可能没有正确设置您的 bash 配置文件并规定 maven 目录所在的位置。此外,如果可能,请尝试通过 apache 网站上显示的 terminal/command 提示设置您的 Maven 项目,看看它是否有效。 Link 是:http://maven.apache.org/archetype/maven-archetype-plugin/examples/generate-batch.html

例如:这是我用来为我的一个项目创建 Maven 原型的命令:

mvn -B archetype:generate \-DarchetypeGroupId=org.apache.maven.archetypes \-DgroupId=com.weather.mobile.ios.automation.tenday \-DartifactId=IphoneTenDayAutomation

groupID 创建路径(将其视为包),而 artificatID 是 class 的名称。

您似乎没有正确使用 assembly 插件。将插件条目更改为:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>com.test.MainClassName</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> 
      <phase>package</phase> <!-- packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

那么你只需要执行:clean install。这将在 target 目录中创建具有依赖项的 jar。 您可以参考 link 中给出的这些示例:https://maven.apache.org/plugins/maven-assembly-plugin/examples/sharing-descriptors.html

我遇到过类似的 Maven 构建错误问题。我在更新快照选项上尝试了复选框并修复了。如果后台有旧的构建内容或缓存之类的东西,它可能会对那些人有所帮助。