使用 Maven 2 构建可运行的 jar

发布于 2024-08-17 02:55:23 字数 214 浏览 9 评论 0原文

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

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

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 install and 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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蘑菇王子 2024-08-24 02:55:23

最简单的方法是使用 maven-assemble-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>

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

mvn assembly:assembly

如果您想生成程序集作为构建的一部分,只需绑定 assemble:single打包阶段的魔力:

<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>

只需运行:

mvn package

The easiest way to do this would be to create an assembly using the maven-assembly-plugin and the predefined jar-with-dependencies descriptor. 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:

<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:single mojo to the package phase:

<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
¢好甜 2024-08-24 02:55:23

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

相反,您将依赖项与 jar 文件一起提供,并在 Manifest 的 Class-Path 标头

要走这条路线,您需要启用 addClasspath 属性(记录为 此处)用于maven- jar-插件

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

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

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

To go this route, you'll need to enable the addClasspath property (documented here) for the 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.

紧拥背影 2024-08-24 02:55:23

这就是我对小型项目所做的事情。大多数时候你不需要一个大罐子。

构建:
mvn clean dependency:复制依赖包

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

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

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

贪了杯 2024-08-24 02:55:23

我同意 Joachim Sauer 的观点,

您应该像 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>

并使用此程序集配置将 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>

I Agree with Joachim Sauer,

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

<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:

<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>
—━☆沉默づ 2024-08-24 02:55:23

只需在 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>

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

<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>
最终幸福 2024-08-24 02:55:23

我有一个spring boot应用程序,maven包创建的jar可以运行而无需设置额外的插件。

在我的 pom.xml 中:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.12</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

https://spring.io/projects/spring-boot

I have a spring boot application, and the jar created by maven package can be run without setting up additional plugins.

Inside my pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.12</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

https://spring.io/projects/spring-boot

帅气尐潴 2024-08-24 02:55:23

要将依赖项打包到 JAR 中以便它可以作为独立应用程序运行,您需要配置 Maven Assembly Plugin 或 Maven Shade Plugin。以下是使用 Maven Shade 插件执行此操作的方法:

  1. 首先,将 Maven Shade 插件添加到您的 pom.xml 文件中的以下部分:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version> <!-- Or use the latest version -->
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.mapReduce.WordCountRunner</mainClass> <!-- Update with your main class -->
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. org.mapReduce.WordCountRunner 替换为主类的完全限定名称。
  2. 添加插件配置后,运行 mvn clean package 来创建阴影 JAR。

To package your dependencies inside the JAR so that it can be run as a standalone application, you need to configure the Maven Assembly Plugin or the Maven Shade Plugin. Here's how you can do it using the Maven Shade Plugin:

  1. First, add the Maven Shade Plugin to your pom.xml file inside the section:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version> <!-- Or use the latest version -->
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.mapReduce.WordCountRunner</mainClass> <!-- Update with your main class -->
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. Replace org.mapReduce.WordCountRunner with the fully qualified name of your main class.
  2. After adding the plugin configuration, run mvn clean package to create the shaded JAR.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文