2 回答

TA貢獻1155條經驗 獲得超0個贊
要創建可執行 jar,您必須使用以下 maven jar 插件。我在下面提供了代碼片段。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
your main class
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
有關更多詳細信息,請參閱下面的 github 鏈接以了解如何創建可執行/可運行的 jar。
https://github.com/debjava/runnableJar
根據文檔,Maven 的 Maven Assembly 插件主要旨在允許用戶將項目輸出及其依賴項、模塊、站點文檔和其他文件聚合到一個可分發的存檔中。
請參閱下面的 Maven Assembly 插件的詳細信息。 http://maven.apache.org/plugins/maven-assembly-plugin/

TA貢獻2051條經驗 獲得超10個贊
這就是我解決你的問題的方法:
請忽略業務邏輯,因為我使用的是 MongoDB,添加了 MongoDB 驅動程序作為依賴項并且能夠使用 java -jar 命令運行。
最終目標與您的目標相同。
我正在使用 maven-shade 插件。
從Github 源代碼簽出項目:
解壓縮、構建、測試
或
1. 創建一個 maven 項目mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
使用以下更新 pom.xml,從 OP 添加 mongo 客戶端代碼并打印集合名稱
執行
mvn clean package
java -jar test.jar
我得到的 執行 輸出是:
INFO: Opened connection [connectionId{localValue:1, serverValue:9}] to localhost:27017
Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 5]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2249770}
Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:10}] to localhost:27017
admin
config
local
test
[XenonSuite] Successfully connected to MongoDB
pom.xml(忽略包名稱)
檢查陰影插件配置
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mainClass>test.App</mainClass>
</properties>
<packaging>jar</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>test.App</mainClass>
</transformer>
</transformers>
<finalName>test</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
有多種方法可以解決這個問題 Ref: https://www.baeldung.com/executable-jar-with-maven
添加回答
舉報