简体   繁体   English

Maven使用Spring Boot Parent创建可执行jar包

[英]maven create executable jar package with spring boot parent

I need to make an executable jar package out of my application. 我需要用我的应用程序制作一个可执行的jar包。

Here's my pom.xml: 这是我的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nws</groupId>
    <artifactId>rest-signer</artifactId>
    <version>0.1</version>
    <name>rest-signer</name>
    <description>rest wrapper for signing lib</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--<dependency>-->
        <!--<groupId>org.glassfish.jersey.media</groupId>-->
        <!--<artifactId>jersey-media-multipart</artifactId>-->
        <!--<scope>compile</scope>-->
    <!--</dependency>-->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <compress>false</compress>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <useUniqueVersions>false</useUniqueVersions>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.nws.restsigner.RestSignerApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>com.googlecode.addjars-maven-plugin</groupId>
            <artifactId>addjars-maven-plugin</artifactId>
            <version>1.0.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>add-jars</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>${basedir}/lib</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
            <configuration>
                <!-- put your configurations here -->
                <minimizeJar>false</minimizeJar>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I build package with: mvn clean package -DskipTests=true and run it by java -jar i get this: 我用以下命令构建软件包: mvn clean package -DskipTests=true并由java -jar运行,我得到了:

 Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry BOOT-INF/lib/spring-boot-starter-jersey-2.1.2.RELEASE.jar
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:108)
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:86)
        at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:70)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:49)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
    Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/spring-boot-starter-jersey-2.1.2.RELEASE.jar'
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:256)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:241)
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:103)
        ... 4 more
    Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/spring-boot-starter-jersey-2.1.2.RELEASE.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
        at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:284)
        at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:264)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:252)
        ... 6 more

mvn --version Apache Maven 3.6.0 (NON-CANONICAL_2018-11-06T03:14:22+01:00_root; 2018-11-06T03:14:22+01:00) Maven home: /opt/maven Java version: 1.8.0_192, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-openjdk/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.20.0-arch1-1-arch", arch: "amd64", family: "unix" mvn --version Apache Maven 3.6.0(NON-CANONICAL_2018-11-06T03:14:22 + 01:00_root; 2018-11-06T03:14:22 + 01:00)Maven主页:/ opt / maven Java版本: 1.8.0_192,供应商:Oracle Corporation,运行时:/ usr / lib / jvm / java-8-openjdk / jre默认语言环境:en_US,平台编码:UTF-8操作系统名称:“ linux”,版本:“ 4.20.0- arch1-1-arch”,拱门:“ amd64”,家庭:“ unix”

so what should I change in my packaging to be able to run this finally? 那么我应该怎样改变包装才能最终运行呢?

spring-boot-maven-plugin creates executable jar by default. spring-boot-maven-plugin默认创建可执行的jar。 So, you don't have to create it manually. 因此,您不必手动创建它。

Basically, it's enough to just have this in the pom.xml : 基本上,将其包含在pom.xml就足够了:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.2.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

If your usage require some custom configuration (eg setting main class), here you can find good documentation. 如果您的用法需要一些自定义配置(例如,设置主类),则可以在此处找到好的文档。

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

相关问题 Spring Boot Maven-无法将Web应用程序打包为可执行jar / war - Spring Boot Maven - Unable to package Web app as executable jar/war spring-boot Maven:如何使用主类创建可执行 jar? - spring-boot Maven: How to create executable jar with main class? 使用Proguard的Spring Boot无法创建可执行jar - Spring boot with proguard not able to create executable jar 创建Jar of Maven Spring Boot项目 - Create Jar of maven spring boot project 使用 Spring-Boot 创建一个简单的 JAR 而不是可执行的 JAR - Create a simple JAR instead of an executable JAR with Spring-Boot 如何使用 maven 将 spring-boot 打包到带有本地 jar 的 jar - How to package spring-boot to jar with local jar using maven 如何 package React 前端与 Spring 启动并运行可执行文件 jar? - How to package React front end with Spring Boot and run the executable jar? 如何使用 maven 创建基于 spring 的可执行文件 jar? - How to create spring-based executable jar with maven? 为使用spring数据jpa和maven的项目创建一个可执行jar - Create an executable jar for a project that uses spring data jpa and maven 如何在离线环境中使用 gradle 为 Spring 启动创建可执行 Jar - How to create an executable Jar for Spring Boot using gradle in an offline environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM