Maven 构建没有主要清单属性

我正在使用 Maven 构建一个具有依赖项并且是可执行 jar 文件的小型应用程序。


这是我的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>com.company</groupId>

    <artifactId>site-downloader</artifactId>

    <version>1.0.0</version>

    <packaging>jar</packaging>


    <properties>

        <maven.compiler.target>1.8</maven.compiler.target>

        <maven.compiler.source>1.8</maven.compiler.source>

    </properties>


    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->

        <dependency>

            <groupId>org.seleniumhq.selenium</groupId>

            <artifactId>selenium-java</artifactId>

            <version>3.141.59</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->

        <dependency>

            <groupId>commons-io</groupId>

            <artifactId>commons-io</artifactId>

            <version>2.6</version>

        </dependency>

        <dependency>

            <groupId>com.company.siteripper</groupId>

            <artifactId>common-interfaces</artifactId>

            <version>1.0.0</version>

        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-assembly-plugin</artifactId>

                <executions>

                    <execution>

                        <phase>package</phase>

                        <goals>

构建完成没有任何问题,但是在执行它写入的 jar 时


没有主要清单属性,在本地 maven repo / my jar中。


我在文档和Baeldung寻求帮助也无济于事。


我寻求帮助使 jar 包含其依赖项并同时可执行。


慕标5832272
浏览 85回答 2
2回答

白衣非少年

要创建可执行 jar,您必须使用以下 maven jar 插件。我在下面提供了代码片段。<plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-jar-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; your main class&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>有关更多详细信息,请参阅下面的 github 链接以了解如何创建可执行/可运行的 jar。https://github.com/debjava/runnableJar根据文档,Maven 的 Maven Assembly 插件主要旨在允许用户将项目输出及其依赖项、模块、站点文档和其他文件聚合到一个可分发的存档中。请参阅下面的 Maven Assembly 插件的详细信息。 http://maven.apache.org/plugins/maven-assembly-plugin/

侃侃无极

这就是我解决你的问题的方法:请忽略业务逻辑,因为我使用的是 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 客户端代码并打印集合名称执行&nbsp;mvn clean packagejava -jar test.jar&nbsp;我得到的&nbsp;执行 输出是:INFO: Opened connection [connectionId{localValue:1, serverValue:9}] to localhost:27017Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger logINFO: 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 logINFO: Opened connection [connectionId{localValue:2, serverValue:10}] to localhost:27017adminconfiglocaltest[XenonSuite] Successfully connected to MongoDBpom.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"&nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; <groupId>test</groupId>&nbsp; <artifactId>test</artifactId>&nbsp; <version>1.0-SNAPSHOT</version>&nbsp; <name>test</name>&nbsp; <!-- FIXME change it to the project's website -->&nbsp; <url>http://www.example.com</url>&nbsp; <properties>&nbsp; &nbsp; <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>&nbsp; &nbsp; <maven.compiler.source>1.8</maven.compiler.source>&nbsp; &nbsp; <maven.compiler.target>1.8</maven.compiler.target>&nbsp; &nbsp; <mainClass>test.App</mainClass>&nbsp; </properties>&nbsp; <packaging>jar</packaging>&nbsp; <dependencies>&nbsp; <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.mongodb</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>mongo-java-driver</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>3.10.2</version>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; <groupId>junit</groupId>&nbsp; &nbsp; &nbsp; <artifactId>junit</artifactId>&nbsp; &nbsp; &nbsp; <version>4.11</version>&nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; </dependency>&nbsp; </dependencies>&nbsp; <build>&nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-assembly-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.1.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-dependency-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.1.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-jar-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.1.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-shade-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.2.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>package</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>shade</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <shadedArtifactAttached>true</shadedArtifactAttached>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <transformers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>test.App</mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </transformer>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </transformers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <finalName>test</finalName>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; </plugins>&nbsp; </build></project>有多种方法可以解决这个问题 Ref: https://www.baeldung.com/executable-jar-with-maven
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java