简体   繁体   English

使用 Maven 2 构建可运行的 jar

[英]Building a runnable jar with Maven 2

I'm relatively new to the Maven mantra, but I'm trying to build a command-line runnable jar with Maven.我对 Maven 口头禅相对较新,但我正在尝试使用 Maven 构建命令行可运行 jar。 I've setup my dependencies, but when I run mvn install and attempt to run the jar, two things happen.我已经设置了依赖项,但是当我运行mvn install并尝试运行 jar 时,会发生两件事。 First, no main class is found, which is correctable.首先,没有找到主要的class,这是可以纠正的。 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. Maven 没有将我的依赖库打包到 jar 中,因此我无法将 jar 作为独立应用程序运行。 How do I correct this?我该如何纠正?

The easiest way to do this would be to create an assembly using the maven-assembly-plugin and the predefined jar-with-dependencies descriptor. 最简单的方法是使用maven-assembly-plugin和预定义的jar-with-dependencies描述符创建一个程序集。 You'll also need to generate a manifest with a main-class entry for this uber jar. 您还需要为此超级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: 如果要将程序集生成为构建的一部分,只需将assembly:single绑定assembly:single mojo到程序包阶段:

<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

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-Path header of the Manifest . 相反,您将依赖项与jar文件一起提交,并在Manifest的Class-Path标头中提及它们。

To go this route, you'll need to enable the addClasspath property (documented here ) for the maven-jar-plugin . 要采用这种方法,您需要为maven-jar-plugin启用addClasspath属性( 此处记录 )。

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

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依赖:copy-dependencies包

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

I Agree with Joachim Sauer, 我同意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>

Just add the below code in pom.xml and Run as: maven:install . 只需在pom.xml中添加以下代码,然后运行为:maven:install。 The jar will be created in target folder of eclipse which can be used as "java -jar Hello.jar" . jar将在eclipse的目标文件夹中创建,可以用作“java -jar Hello.jar”。 but make sure that name of main class is given com.abc.HelloWorld.java 但要确保主类的名称是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>

I have a spring boot application, and the jar created by maven package can be run without setting up additional plugins.我有一个 spring 启动应用程序,以及由 maven ZEFE90A8E604A7C840E8ZD0 创建的 jar

Inside my pom.xml:在我的 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 https://spring.io/projects/spring-boot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM