spring boot loading jars (application dependencies and external file system jars)

后端 未结 3 1115
傲寒
傲寒 2020-12-15 09:00

I am trying to figure out what is the best way to setup a spring boot application in such a way that its has its own jar dependencies but additional jars are added to classp

相关标签:
3条回答
  • 2020-12-15 09:30

    The PropertiesLauncher was designed to work with fat jars, so you should be able to keep the fat jar and add as many additional dependencies as you like in an external location, e.g. with loader.path=/opt/app/lib:lib. I guess that's your option 2? If it doesn't work we can discuss in a github issue.

    0 讨论(0)
  • 2020-12-15 09:37

    thank you @Ashraf Sarhan, you rescue my two days :) I added in pom file:

             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <executable>true</executable>
                    <mainClass>vn.com.Mymainclass</mainClass>
                    <excludes>
                        <exclude>
                            <groupId>com.vn.groupId</groupId>
                            <artifactId>excluded-id-a</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>com.vn.groupId</groupId>
                            <artifactId>excluded-id-b</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    And Placed ./lib folder containing two jars of two files which excluded above beside with my-main-spring-boot-app.jar file, and I ran:

    java -Dloader.path="lib/" -jar my-main-spring-boot-app.jar
    

    It worked perfectly.

    0 讨论(0)
  • 2020-12-15 09:50

    I resolved this issue using the following spring-boot-maven-plugin configuration, I had to build my Uber jar without excluded artifacts to create my external "lib" directory, then I added my excluded artifacts again and packaged my Uber jar with my application specific dependencies only.

               <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>
    

    Then, I added the following property to my "application.properties" which inside my jar "resources/" dir to specify my "lib" dir for Spring PropertiesLauncher where I put "lib" dir along with my jar in the same dir.

    loader.path=lib/
    

    Finally, I did run my jar using the following command

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

    Also, you can add the "loader.path" property to your command line without putting it in your "application.properties" like the following command but this way didn't work with me as I packaged my jar as an executable one which I'm running as linux service.

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

    Now, I successfully reduced my jar size from 29 M to only 1 M jar which contains only my application specific libs and it works out of the box.

    0 讨论(0)
提交回复
热议问题