Java 使用 Maven 2 构建一个可运行的 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2022032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:41:20  来源:igfitidea点击:

Building a runnable jar with Maven 2

javamaven-2build-processexecutable-jar

提问by Stefan Kendall

I'm relatively new to the Maven mantra, but I'm trying to build a command-line runnable jar with Maven. I've setup my dependencies, but when I run mvn installand attempt to run the jar, two things happen. First, no main class is found, which is correctable. When I've corrected this, I get errors on run stating that classes cannot be found.

我对 Maven 咒语比较陌生,但我正在尝试使用 Maven 构建一个命令行可运行 jar。我已经设置了我的依赖项,但是当我运行mvn install并尝试运行 jar 时,会发生两件事。首先,没有找到主类,这是可以纠正的。当我更正此问题后,我在运行时收到错误消息,指出找不到类。

Maven is not packaging my dependency libraries inside of the jar, so I am unable to run the jar as a stand-alone application. How do I correct this?

Maven 没有将我的依赖库打包在 jar 中,因此我无法将 jar 作为独立应用程序运行。我该如何纠正?

采纳答案by Pascal Thivent

The easiest way to do this would be to create an assembly using the maven-assembly-pluginand the predefined jar-with-dependenciesdescriptor. You'll also need to generate a manifest with a main-class entry for this uber jar. The snippet below shows how to configure the assembly plugin to do so:

执行此操作的最简单方法是使用maven-assembly-plugin和预定义jar-with-dependencies描述符创建程序集。您还需要为这个 uber jar 生成一个带有主类条目的清单。下面的代码片段显示了如何配置程序集插件来执行此操作:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

Then, to generate the assembly, just run:

然后,要生成程序集,只需运行:

mvn assembly:assembly

If you want to generate the assembly as part of your build, simply bind the assembly:singlemojo to the package phase:

如果您想在构建过程中生成程序集,只需将assembly:singlemojo绑定到包阶段:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

And simply run:

只需运行:

mvn package

回答by Joachim Sauer

Maven is not packaging your dependencies inside your jar file, because you don't usually do this with Java programs.

Maven 不会将您的依赖项打包到您的 jar 文件中,因为您通常不会对 Java 程序执行此操作。

Instead you deliver the dependencies together with your jar file and mention them in the Class-Pathheader of the Manifest.

相反,您将依赖项与 jar 文件一起交付,并在 ManifestClass-Path标题中提及它们。

To go this route, you'll need to enable the addClasspathproperty (documented here) for the maven-jar-plugin.

走这条路线,你需要使addClasspath财产(记录在这里的)maven-jar-plugin

If you really want to include all your dependencies in your jar file, then you can use the Maven Assembly plugin to create a jar-with-dependencies.

如果您真的想在 jar 文件中包含所有依赖项,那么您可以使用 Maven Assembly 插件创建一个jar-with-dependencies.

回答by formixian

I Agree with Joachim Sauer,

我同意约阿希姆·绍尔的观点,

Instead of using jar-with-dependency you should configure the jar plugin like that in your pom.xml:

您应该在 pom.xml 中像这样配置 jar 插件,而不是使用 jar-with-dependency:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <index>true</index>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>[mainClassFullName]</mainClass>
            </manifest>
            <manifestEntries>
                <mode>development</mode>
                <url>${project.url}</url>
                <key>value</key>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

And use this assembly configuration to add the jar dependencies to you assembly:

并使用此程序集配置将 jar 依赖项添加到您的程序集:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>zip-with-jars</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySets>
    <dependencySet>
    <outputDirectory>/</outputDirectory>
    <useProjectArtifact>true</useProjectArtifact>
    <unpack>false</unpack>
    <scope>runtime</scope>
    </dependencySet>
</dependencySets>
  </dependencySets>
</assembly>

回答by user2409824

This is what I would do for small projects. Most of the time you don't want one huge jar.

这就是我会为小项目做的事情。大多数时候你不想要一个大罐子。

to build:mvn clean dependency:copy-dependencies package

构建:mvn clean dependency:copy-dependencies 包

to execute (in target dir):java -cp myjar.jar:./dependency/* com.something.MyClass

执行(在目标目录中):java -cp myjar.jar:./dependency/* com.something.MyClass

回答by Pan

Just add the below code in pom.xml and Run as: maven:install . The jar will be created in target folder of eclipse which can be used as "java -jar Hello.jar" . but make sure that name of main class is given com.abc.HelloWorld.java

只需在 pom.xml 中添加以下代码并运行为: maven:install 。jar 将在 eclipse 的目标文件夹中创建,可用作 "java -jar Hello.jar" 。但请确保主类的名称为 com.abc.HelloWorld.java

<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-shade-plugin</artifactid>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalname>HelloW</finalname>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestentries>
<main-class>com.abc.HelloWorld.java</main-class>
<build-number>1</build-number>
</manifestentries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>