我对 Maven 口号相对较新,但我正在尝试使用 Maven 构建一个命令行可运行的 jar。我已经设置了依赖项,但是当我运行
mvn install
并尝试运行 jar 时,会发生两件事。首先,没有找到主类,这是可以纠正的。当我更正这个问题后,我在运行时收到错误,指出找不到类。
Maven 没有将我的依赖库打包到 jar 中,因此我无法将 jar 作为独立应用程序运行。我该如何纠正这个问题?
最简单的方法是使用
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>
然后,要生成程序集,只需运行:
mvn assembly:assembly
如果您想在构建过程中生成程序集,只需将
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>
只需运行:
mvn package
Maven 不会将您的依赖项打包到 jar 文件中,因为您通常不会使用 Java 程序执行此操作。
相反,您将依赖项与 jar 文件一起提供,并在 Manifest
的
Class-Path
标头中提及它们。
要走这条路线,您需要为
addClasspath
启用 maven-jar-plugin
属性(记录于here)。
如果您确实想在 jar 文件中包含所有依赖项,那么您可以使用 Maven Assembly 插件创建一个
jar-with-dependencies
。
这就是我对小型项目所做的事情。大多数时候你不需要一个大罐子。
构建: mvn clean dependency:复制依赖包
执行(在目标目录): java -cp myjar.jar:./dependency/* com.something.MyClass
我同意约阿希姆·索尔的观点,
您应该像 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>
只需在 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>
我有一个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>
要将依赖项打包到 JAR 中以便它可以作为独立应用程序运行,您需要配置 Maven Assembly Plugin 或 Maven Shade Plugin。以下是使用 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>
org.mapReduce.WordCountRunner
替换为主类的完全限定名称。mvn clean package
创建阴影 JAR。