无法为非常简单的Eclipse Maven项目生成可运行的JAR [英] Cannot generate runnable JAR for very simple Eclipse Maven project

查看:49
本文介绍了无法为非常简单的Eclipse Maven项目生成可运行的JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to generate a runnable JAR for my Eclipse Java projects. The JAR should contain all my code, with unmodified namespaces, classes, resources etc. My workspace is organized into a multitude of different projects.

当我右键单击Project Explorer中的项目节点,然后选择"运行方式>Maven安装",我得到以下信息:

When I right-click a project node in Project Explorer, and select "Run As > Maven install", I get the following:

[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文件为:

foo.Project1 runs fine under Eclipse. Its pom.xml file is:

<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:/b9bb4aa840daa6e7b2dd2c793acdb56420200626103047/32c0fd )

(A ZIP of the two projects can be accessed here: https://wetransfer.com/downloads/b9bb4aa840daa6e7b2dd2c793acdb56420200626103047/32c0fd)

我尝试了中提出的第一个解决方案如何使用Maven创建具有依赖项的可执行JAR?(得分2300以上),方法:

I tried the first solution suggested in How can I create an executable JAR with dependencies using Maven? (with 2300+ score) by:

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

1: adding the following in the build section of the pom.xml:

  <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:右键单击项目节点,然后单击"运行方式>Maven构建... "

2: right-clicking the project node, then "Run As > Maven build..."

3:定义目标";如: clean编译Assembly:single ,然后点击Run

3: defining "Goals" as: clean compile assembly:single, and hitting Run

相同的错误.

我在做什么错了?

推荐答案

如果要使用 foo.Project2 作为依赖项,则需要先使用 clean install 进行构建>.

If you want to use foo.Project2 as dependency, you need to build it first with clean install.

在将其用作Maven构建中的依赖项之前,您始终需要构建一个项目.

You always need to build a project before you can use it as dependency in the Maven build.

Eclipse还通过允许您从工作空间中引用项目来作弊,因此可以运行该程序.但是对于实际构建",您需要以正确的顺序构建所有内容.

Eclipse cheats a bit by also allowing you to reference projects from the workspace and therefore allows you to run the program. But for the "real build", you need to build everything in the right order.

这篇关于无法为非常简单的Eclipse Maven项目生成可运行的JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆