首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Java Spring Boot,如何将依赖项、源代码和Spring组合到一个Jar文件中

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

Stack Overflow用户
提问于 2018-05-27 22:26:39
回答 1查看 1.2K关注 0票数 2

我已经在这个问题上挣扎了几天了,希望能得到一些指导。

我正在使用Spring Boot,并试图将我的项目构建到一个我可以运行的jar中。有一些嵌套依赖项(OpenCV)需要我将它们全部包含到一个Jar文件中。

在我添加这些依赖项之前,我能够成功地构建一个工作正常的Jar文件;直到我开始尝试构建一个组合的jar文件,事情才开始成为一个问题。

我面临的问题是:如何成功地构建一个Jar文件,将所有的Spring文件、嵌套依赖项和源代码都包含到一个fat Jar文件中?

下面是我的POM文件,其中省略了一些依赖项。

代码语言:javascript
复制
<?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>

在Intellij IDE的终端上,我尝试使用3个不同的命令进行编译:

1) mvn清洁封装

2) mvn全新安装

3) mvn清洁编译程序集:single

这3个命令与更改build标记内的注释代码相结合,总是会产生以下两个结果之一:

要么我得到一个大的jar文件,其中除了我的源代码不在基目录中(它位于BOOT-INF目录下,并且在运行时找不到它),而且每当我使用java -jar {MyJar}.jar运行它时,我都会得到标准的“找不到MainLauncher”错误。

我得到了两个jar文件,其中“带依赖项的jar”版本包含我的源代码和其他库,但其中没有Spring库。

因此,这两个jars都不能工作,因为我需要它们的一些组合。

到目前为止我已经尝试过的东西:

1) Packaging spring sub-projects into one jar

2) No source jar attached with maven-assembly-plugin

3) Include spring xml in jar by maven-assembly-plugin packaging

4) How do I create an an executable jar in Spring Boot?

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

为了用我的源代码、spring文件和嵌套依赖项实现一个jar文件的目标,我错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 13:18:46

好吧,花了相当多的研究,但简短的答案是,你不能做我希望你能用某些外部库做的事情。

更详细的解释是:

首先,根据this answer,您需要删除二级gradle程序集依赖项,才能使您的POM文件看起来像这样:

代码语言:javascript
复制
<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>

然后使用以下命令进行构建:

代码语言:javascript
复制
mvn clean install

其次,根据this sample,您需要将库(在我的示例中为OpenCV)和相应的文件放在路径/类路径中。Here's the link介绍了如何使用Java实现这一点。

一旦这些步骤完成,您就可以使用标准的

代码语言:javascript
复制
java -jar YOUR_JAR_HERE.jar

它应该能正常工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50553132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档