简体   繁体   English

使用Java Spring Boot,如何将依赖项,源代码和Spring合并到一个Jar文件中

[英]Using Java Spring Boot, How do I combine the dependencies, my source code, and Spring into one Jar file

been wrestling with this issue for a few days and would appreciate some guidance. 一直在努力解决这一问题,希望能提供一些指导。

I am working with Spring Boot and am attempting to build my project into a jar that I can run. 我正在使用Spring Boot,并试图将我的项目构建到可以运行的jar中。 There are some nested dependencies (OpenCV) that require me to include them all together into the one Jar file. 有一些嵌套的依赖项(OpenCV),要求我将它们全部一起包含到一个Jar文件中。

Before I added these dependencies, I was able to successfully build a Jar file that worked just fine; 在添加这些依赖项之前,我能够成功构建一个运行良好的Jar文件。 it wasn't until I started trying to build a combined jar that things started to become an issue. 直到我开始尝试构建组合罐时,事情才开始成为问题。

The problem I am facing is this: How do I successfully build a Jar file that includes all of the Spring files, nested dependencies, and my source code all into one fat Jar file? 我面临的问题是: 如何成功构建一个Jar文件,将所有Spring文件,嵌套的依赖项以及我的源代码全部包含到一个胖Jar文件中?

Here is my POM file below with a few of the dependencies omitted. 这是下面的我的POM文件,其中省略了一些依赖项。

<?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.myapp</groupId>
    <artifactId>mytestproject</artifactId>
    <packaging>jar</packaging>
    <!--Change the above to a war instead of jar if you need a war file-->
    <name>Spring OSGi Bundle</name>
    <version>0.1</version>
    <url>http://www.springframework.org/osgi</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <properties>
        <spring.osgi.version>1.2.1</spring.osgi.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>

    <!-- ================================================ -->
    <!--            Repository Configuration              -->
    <!-- ================================================ -->

    <!-- Repos -->
    <repositories>

        <!-- Apache imaging https://commons.apache.org/proper/commons-imaging/index.html -->
        <repository>
            <id>apache.snapshots</id>
            <name>Apache Development Snapshot Repository</name>
            <url>https://repository.apache.org/content/repositories/snapshots/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>


        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>

        <repository>
            <id>i21-s3-osgi-repo</id>
            <name>i21 osgi artifacts repo</name>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <url>http://maven.springframework.org/osgi</url>
        </repository>


        <repository>
            <id>com.springsource.repository.bundles.external</id>
            <name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
            <url>http://repository.springsource.com/maven/bundles/external</url>
        </repository>

        <repository>
            <id>com.springsource.repository.bundles.release</id>
            <name>SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases</name>
            <url>http://repository.springsource.com/maven/bundles/release</url>
        </repository>

        <repository>
            <id>com.springsource.repository.bundles.milestone</id>
            <name>SpringSource Enterprise Bundle Repository - SpringSource Milestone Releases</name>
            <url>http://repository.springsource.com/maven/bundles/milestone</url>
        </repository>


        <repository>
            <id>spring-release</id>
            <name>Spring Portfolio Release Repository</name>
            <url>http://maven.springframework.org/release</url>
        </repository>

        <repository>
            <id>eclipse-repository</id>
            <name>Eclipse Repository</name>
            <url>http://repo1.maven.org/eclipse/</url>
        </repository>

        <repository>
            <id>spring-ext</id>
            <name>Spring External Dependencies Repository</name>
            <url>
                https://springframework.svn.sourceforge.net/svnroot/springframework/repos/repo-ext/
            </url>
        </repository>
    </repositories>

    <!-- Plugin Repos -->
    <pluginRepositories>
        <pluginRepository>
            <id>maven-repo</id>
            <name>maven repo</name>
            <url>http://repo1.maven.org/maven2/</url>
        </pluginRepository>
        <pluginRepository>
            <id>com.springsource.repository.bundles.milestone</id>
            <name>SpringSource Enterprise Bundle Repository - SpringSource Milestone Releases</name>
            <url>http://repository.springsource.com/maven/bundles/milestone</url>
        </pluginRepository>
    </pluginRepositories>

    <!-- Dependencies -->
    <dependencies>

        <!-- Local Jar File for open.cv -->
        <dependency>
            <groupId>org.openpnp</groupId>
            <artifactId>opencv</artifactId>
            <scope>system</scope>
            <version>3.2.0</version>
            <systemPath>${basedir}/src/main/libs/opencv-320.jar</systemPath>
        </dependency>

        <!-- OSGI -->
        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-test</artifactId>
            <version>${spring.osgi.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-annotation</artifactId>
            <version>${spring.osgi.version}</version>
            <scope>test</scope>
        </dependency>




    </dependencies>

    <!-- Build Tag -->
    <build>
        <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
        <resources>
            <!-- standard Maven folder -->
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <!-- plus root folder -->
            <resource>
                <directory>.</directory>
                <includes>
                    <include>plugin.xml</include>
                    <include>META-INF/*</include>
                </includes>
            </resource>
        </resources>


        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--<classifier>sources</classifier>-->
                    <!--<classifier>spring-boot</classifier>-->
                    <mainClass>com.myapp.internal.MainLauncher</mainClass>
                    <addResources>true</addResources>
                    <!--<executable>true</executable>-->
                </configuration>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <!--<phase>compile</phase>-->
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <!--<classpath>.</classpath>-->
                            <addClasspath>true</addClasspath>
                            <mainClass>com.myapp.internal.MainLauncher</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <!--<execution>-->
                        <!--<id>attach-sources</id>-->
                        <!--<phase>compile</phase>-->
                        <!--<goals>-->
                            <!--<goal>single</goal>-->
                        <!--</goals>-->
                    <!--</execution>-->
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

There are 3 different commands I am trying to compile with at the terminal in the Intellij IDE: 我尝试在Intellij IDE的终端上使用3种不同的命令进行编译:

1) mvn clean package 1)MVN清洁包装

2) mvn clean install 2)MVN全新安装

3) mvn clean compile assembly:single 3)mvn clean编译程序集:单个

Those 3 commands coupled with changing the commented code within the build tag always yields one of two results: 这3条命令加上更改build标签中的注释代码始终会产生以下两个结果之一:

Either I get one large jar file which has everything in it except my source code is not in the base directory (it is located under the BOOT-INF directory and can't be found when ran) and I get the standard "Could not find MainLauncher" error whenever I run it using java -jar {MyJar}.jar. 要么得到一个包含所有内容的大jar文件, 除了我的源代码不在基本目录中(它位于BOOT-INF目录下,运行时找不到),并且得到标准的“找不到”每当我使用java -jar {MyJar} .jar运行它时,就会出现“ MainLauncher”错误。

or 要么

I get 2 jar files where the "jar-with-dependencies" version has my source code as well as the other libs, but has none of the Spring libs in it. 我得到2个jar文件,其中“ jar-with-dependencies”版本具有我的源代码以及其他库,但其中没有任何Spring库。

Consequently, neither of these 2 jars work as I need some combination of both of them. 因此,这两个罐子都不起作用,因为我需要将它们两者组合在一起。

Things I have tried thus far: 到目前为止我尝试过的事情:

1) Packaging spring sub-projects into one jar 1)将春季子项目包装到一个罐中

2) No source jar attached with maven-assembly-plugin 2) 没有附带Maven-assembly-plugin的源jar

3) Include spring xml in jar by maven-assembly-plugin packaging 3) 通过maven-assembly-plugin包装在jar中包含spring xml

4) How do I create an an executable jar in Spring Boot? 4) 如何在Spring Boot中创建一个可执行jar?

5) https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/ 5) https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/

What am I missing in order to accomplish the goal of one jar file with my source, spring files, and nested dependencies? 为了实现一个带有我的源文件,spring文件和嵌套依赖项的jar文件的目标,我缺少什么?

Ok, took quite a bit of research, but the short answer is, you cannot do what I had hoped you could with certain external libraries. 好的,做了很多研究,但是简短的答案是,您不能使用某些外部库来完成我希望的工作。

The longer explanation is this: 更长的解释是这样的:

First, as per this answer , you need to remove the secondary gradle assembly dependency to have your POM file look like this: 首先,按照此答案 ,您需要删除辅助gradle程序集依赖项,以使POM文件如下所示:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>com.myapp.internal.MainLauncher</mainClass>
            <addResources>true</addResources>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

Then make the build with this command: 然后使用以下命令进行构建:

mvn clean install

Second, as per this sample , you need to have the library (in my case, OpenCV ) and the respective files located along your path / classpath. 其次,按照此样本 ,您需要具有路径(在我的情况下为OpenCV )和沿着路径/类路径定位的各个文件。 Here's the link on how to do that with Java. 这是有关如何使用Java 的链接

Once those steps are complete, you can run your jar with the standard 完成这些步骤后,即可使用标准配置运行jar

java -jar YOUR_JAR_HERE.jar

and it should work correctly. 它应该可以正常工作。

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

相关问题 如何从 Kubernetes 上的 jar 文件启动应用程序源代码 - how to spring boot application source code from jar file on kubernetes 使用Maven将Java代码和依赖项组合到一个jar(可执行jar)中 - Combine java code and dependencies into one jar (executable jar) using maven Spring Boot Maven plugin - How do I build a zip file with Java jar file and script to run it? - Spring Boot Maven plugin - How do I build a zip file with Java jar file and script to run it? 如何使用 Gradle 7 在 Spring 启动应用程序中添加外部 jar 文件依赖项? - How to add external jar file dependencies in Spring boot app with Gradle 7? 如何在Spring Boot中使用不活动的数据源创建Jar文件? - How to Create a Jar file in Spring boot with Data Source that is not live? 带有依赖项的Jar Spring Boot Gradle - Jar with dependencies spring boot gradle 如何使用Windows批处理文件一个一启动Spring Boot jar文件? - How to start spring boot jar files one by one using windows batch file? Spring 启动 我的 jar 文件在哪里 - Spring boot where is my jar file Java Spring Boot外部日志文件jar - Java Spring Boot external logging file jar 依赖项没有从导入的 jar 文件反映到我的 spring 引导项目中 - Dependencies not being reflected from the imported jar file into my spring boot project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM