首页 文章

spring boot加载jar(应用程序依赖项和外部文件系统jar)

提问于
浏览
8

我试图找出以这样的方式设置spring启动应用程序的最佳方法,即它具有自己的jar依赖关系,但是当它作为java -jar命令运行时,在运行时将额外的jar添加到classpath . 什么方法更有意义

  • 使用原始jar(不添加依赖项)并将所有jar(应用程序和运行时)放在文件系统上的文件夹中,并使用PropertiesLauncher指定jars文件夹的loader.path .

  • 使用fat jar(带有应用程序jar)将其他jar放在文件系统上,并以某种方式将其包含在需要添加到classpath的附加jar中 . 不知道如何做到这一点 .

  • 还有另一种更好的方法吗?

2 回答

  • 5

    PropertiesLauncher 旨在与胖 jar 一起使用,因此您应该能够保留胖 jar 并在外部位置添加任意数量的附加依赖项,例如:与 loader.path=/opt/app/lib:lib . 我想我们可以在github问题上讨论's your option 2? If it doesn't .

  • 4

    我使用以下spring-boot-maven-plugin配置解决了这个问题,我必须在没有排除工件的情况下构建我的Uber jar来创建我的外部"lib"目录,然后我再次添加了我的排除工件并且仅使用我的应用程序特定依赖项打包了我的Uber jar .

    <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>1.3.1.RELEASE</version>
                    <configuration>
                        <layout>ZIP</layout>
                        <executable>true</executable>
                        <excludeArtifactIds>
                            <!-- My libs which will be packaged with my Uber jar-->
                            <!-- core,data-feeder,engine,lightspeed-tcp-api,order-manager,store,strategies,utils,viewer -->
                            <!-- Other libs -->
                            antlr,aopalliance,aspectjrt,aspectjweaver,classmate,commons-lang,
                            dom4j,h2,hibernate-commons-annotations,hibernate-core,hibernate-entitymanager,
                            hibernate-jpa-2.1-api,hibernate-validator,jackson-annotations,jackson-core,jackson-databind,
                            jandex,javassist,javax.transaction-api,jboss-logging,jboss-logging-annotations,jcl-over-slf4j,
                            jul-to-slf4j,log4j-over-slf4j,logback-classic,logback-core,mysql-connector-java,slf4j-api,
                            snakeyaml,spring-aop,spring-aspects,spring-beans,spring-boot,spring-boot-autoconfigure,
                            spring-boot-starter,spring-boot-starter-aop,spring-boot-starter-data-jpa,spring-boot-starter-jdbc,
                            spring-boot-starter-logging,spring-boot-starter-tomcat,spring-boot-starter-web,
                            spring-boot-starter-websocket,spring-context,spring-core,spring-data-commons,spring-data-jpa,
                            spring-expression,spring-jdbc,spring-messaging,spring-orm,spring-tx,spring-web,spring-webmvc,
                            spring-websocket,tomcat-embed-core,tomcat-embed-el,tomcat-embed-logging-juli,tomcat-embed-websocket,
                            tomcat-jdbc,tomcat-juli,validation-api,xml-apis
                        </excludeArtifactIds>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    然后,我将以下属性添加到我的"resources/"里面,我的jar "resources/" dir指定我的"lib"目录为Spring PropertiesLauncher,我把"lib" dir和我的jar一起放在同一个目录中 .

    loader.path=lib/
    

    最后,我使用以下命令运行我的jar

    java -jar back-tester-0.0.1-beta-01.jar
    

    此外,您可以将“loader.path”属性添加到命令行而不将其放在“application.properties”中,如下面的命令,但这种方式对我不起作用,因为我将jar打包为可执行文件我我作为linux服务运行 .

    java -Dloader.path="lib/" -jar back-tester-0.0.1-beta-01.jar
    

    现在,我成功地将我的jar大小从29 M减少到只有1 M jar,其中只包含我的应用程序特定的库,它开箱即用 .

相关问题