我试图找出以这样的方式设置spring启动应用程序的最佳方法,即它具有自己的jar依赖关系,但是当它作为java -jar命令运行时,在运行时将额外的jar添加到classpath.什么方法更有意义
使用原始jar(不添加依赖项)并将所有jar(应用程序和运行时)放在文件系统上的文件夹中,并使用PropertiesLauncher指定jars文件夹的loader.path.
使用fat jar(带有应用程序jar)将额外的jar放在文件系统上,并以某种方式包含那些需要添加到classpath的额外jar.不知道如何做到这一点.
有没有更好的方法来做到这一点
它PropertiesLauncher被设计为与胖罐一起使用,因此您应该能够保留胖罐并在外部位置添加任意数量的附加依赖项,例如loader.path=/opt/app/lib:lib.我想这是你的选择2?如果它不起作用,我们可以在github问题中讨论.
小智 5
我使用以下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>
Run Code Online (Sandbox Code Playgroud)
然后,将以下属性添加到我的jar.resources /目录中的“ application.properties”中,以为Spring PropertiesLauncher指定我的“ lib”目录,在其中我将“ lib”目录和jar放在同一目录中。
loader.path=lib/
Run Code Online (Sandbox Code Playgroud)
最后,我确实使用以下命令运行了jar
java -jar back-tester-0.0.1-beta-01.jar
Run Code Online (Sandbox Code Playgroud)
另外,您可以将“ loader.path”属性添加到命令行中,而无需像下面的命令那样将其放入“ application.properties”中,但是这种方式对我不起作用,因为我将jar打包为可执行文件,正在作为linux服务运行。
java -Dloader.path="lib/" -jar back-tester-0.0.1-beta-01.jar
Run Code Online (Sandbox Code Playgroud)
现在,我成功地将jar大小从29 M减小到仅1 M jar,该jar仅包含我的应用程序特定的库,并且可以直接使用。
| 归档时间: |
|
| 查看次数: |
18228 次 |
| 最近记录: |