我有一个Spring Boot 1.2应用程序打包为WAR,因为我需要能够在应用程序服务器中部署该应用程序。
我还想配置一个外部路径,该路径将包含要添加到类路径中的jar。阅读启动器文档后,我将构建配置为此使用PropertiesLauncher:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
...
<layout>ZIP</layout>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我尝试使用此附加系统属性的各种组合来启动应用程序: -Dloader.path=lib/,lib-provided/,WEB-INF/classes,<my additional path>
但是我总是会遇到这个错误:
java.lang.IllegalArgumentException: Invalid source folder C:\<path to my war>\<my war>.war
at org.springframework.boot.loader.archive.ExplodedArchive.<init> ExplodedArchive.java:78)
at org.springframework.boot.loader.archive.ExplodedArchive.<init>(ExplodedArchive.java:66)
at org.springframework.boot.loader.PropertiesLauncher.addParentClassLoaderEntries(PropertiesLauncher.java:530)
at org.springframework.boot.loader.PropertiesLauncher.getClassPathArchives(PropertiesLauncher.java:451)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:60)
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:609)
Run Code Online (Sandbox Code Playgroud)
我查看了源代码,似乎PropertiesLauncher只能处理jar档案(以“ .jar”或“ .zip”结尾)和“爆炸的档案”(不以前者结尾)
有可能做到我想要的吗?我做错了吗?
如果不可能,那有什么选择?
小智 5
如果有人最终来到这里,这可能会有用:
java -cp yourSpringBootWebApp.war -Dloader.path=yourSpringBootWebApp.war!/WEB-INF/classes/,yourSpringBootWebApp.war!/WEB-INF/,externalLib.jar org.springframework.boot.loader.PropertiesLauncher
Run Code Online (Sandbox Code Playgroud)
(Spring-Boot 1.5.9)
在 Spring Boot 1.2 中,PropertiesLauncher将句柄.jar和.zip文件视为“jar 存档”,将其他所有内容视为“分解存档”(解压缩的 jar)。它没有正确处理.war
这是我找到的替代方案:
我最终切换回常规的战争启动器,并设法配置一个文件夹,将 jar 内容添加到类路径中,使用如下所示SpringApplicationRunListener(简洁的伪代码):
public class ClasspathExtender implements SpringApplicationRunListener {
public void contextPrepared(ConfigurableApplicationContext context) {
// read jars folder path from environment
String path = context.getEnvironment().getProperty("my.jars-folder");
// enumerate jars in the folder
File[] files = new File(path).listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) { return name.endsWith(".jar"); }
});
URL[] urls = // convert files array to urls array
// create a new classloader which contains the jars...
ClassLoader extendedClassloader = new URLClassLoader(urls, context.getClassLoader());
// and replace the context's classloader
((DefaultResourceLoader) context).setClassLoader(extendedClassloader);
}
// other methods are empty
}
Run Code Online (Sandbox Code Playgroud)
该侦听器是通过在META-INF/spring.factories文件中声明它来实例化的:
org.springframework.boot.SpringApplicationRunListener=my.ClasspathExtender
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2423 次 |
| 最近记录: |