首页 > 解决方案 > 无法为非常简单的 Eclipse Maven 项目生成可运行的 JAR

问题描述

我想为我的 Eclipse Java 项目生成一个可运行的 JAR。JAR 应该包含我所有的代码,包括未修改的命名空间、类、资源等。我的工作空间被组织成许多不同的项目。

当我在 Project Explorer 中右键单击项目节点并选择“ Run As > Maven install ”时,我得到以下信息:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< foo.Project1:foo.Project1 >----------------------
[INFO] Building foo.Project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for foo.Project2:foo.Project2:jar:0.0.1-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.220 s
[INFO] Finished at: 2020-06-26T12:20:11+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project foo.Project1: Could not resolve dependencies for project foo.Project1:foo.Project1:jar:0.0.1-SNAPSHOT: Could not find artifact foo.Project2:foo.Project2:jar:0.0.1-SNAPSHOT -> [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

foo.Project1 在 Eclipse 下运行良好。它的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>foo.Project1</groupId>
  <artifactId>foo.Project1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>foo.Project2</groupId>
        <artifactId>foo.Project2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

(这两个项目的 ZIP 可以在这里访问:https ://wetransfer.com/downloads/b9bb4aa840daa6e7b2dd2c793acdb56420200626103047/32c0fd )

我尝试了如何使用 Maven 创建具有依赖项的可执行 JAR 中建议的第一个解决方案?(2300+ 分)通过:

1:在 pom.xml 的 build 部分添加以下内容:

  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>foo.Project1.Application</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>

2:右键项目节点,然后“ Run As > Maven build...

3:将“目标”定义为:clean compile assembly:single,然后点击运行

同样的错误。

我究竟做错了什么?

标签: javaeclipsemaven

解决方案


如果要foo.Project2用作依赖项,则需要先使用clean install.

您总是需要先构建一个项目,然后才能将其用作 Maven 构建中的依赖项。

Eclipse 还允许您从工作区引用项目,因此允许您运行程序。但是对于“真正的构建”,您需要以正确的顺序构建所有内容。


推荐阅读