java Spring Boot - 如何指定备用启动类?(多个入口点)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31076911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - How to specify an alternate start-class? (Multiple Entry Points)
提问by The Gilbert Arenas Dagger
I want to add an alternate entry point to my Spring-Boot application. I would prefer to keep this as a fat jar. Is this possible?
我想向我的 Spring-Boot 应用程序添加一个备用入口点。我宁愿把它当作一个胖罐子。这可能吗?
According to their documentation, the property loader.main
specifies the name of the main class to launch.
根据他们的文档,该属性loader.main
指定要启动的主类的名称。
I tried java -jar MyJar.jar --loader.main=com.mycompany.AlternateMain
but the start-class specified in my pom.xml was still run (and if I remove this from the pom.xml then I error during the packaging).
我试过了,java -jar MyJar.jar --loader.main=com.mycompany.AlternateMain
但在我的 pom.xml 中指定的 start-class 仍在运行(如果我从 pom.xml 中删除它,那么我在打包过程中会出错)。
Alternatively, I tried java -cp MyJar.jar com.mycompany.AlternateMain
but I don't know of a good way to add all the nested jars to the classpath.
或者,我尝试过,java -cp MyJar.jar com.mycompany.AlternateMain
但我不知道将所有嵌套 jar 添加到类路径的好方法。
Any suggestions?
有什么建议?
Edit: Here is the solution that I used
编辑:这是我使用的解决方案
As jst suggested, I changed my launcher to use the PropertiesLauncher. I did this by modifying the configuration of my spring-boot-maven-plugin.
正如 jst 所建议的,我将启动器更改为使用 PropertiesLauncher。我通过修改 spring-boot-maven-plugin 的配置来做到这一点。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
<layout>ZIP</layout>
...
The <layout>ZIP</layout>
triggers Spring Boot to use the PropertiesLauncher
.
该<layout>ZIP</layout>
触发器春季启动使用PropertiesLauncher
。
I created my fat jar (mvn package) then called the alternate main like this:
我创建了我的胖 jar(mvn 包),然后像这样调用备用主:
java -jar -Dloader.main=com.mycompany.AlternateMain MyJar.jar
Thanks for the help!
谢谢您的帮助!
采纳答案by jst
I don't believe that property would apply in your case. There are 3 different "Launchers" (go back to the docs and see). If you are building a jar it uses the JarLauncher class. If you switch it to PropertiesLauncher then loader.main would be useful.
我不相信该财产适用于您的情况。有 3 个不同的“启动器”(返回文档并查看)。如果您正在构建一个 jar,它将使用 JarLauncher 类。如果您将其切换到 PropertiesLauncher,那么 loader.main 会很有用。
META-INF/MANIFEST.MF
元信息/清单.MF
Main-Class: org.springframework.boot.loader.PropertiesLauncher
回答by Eric B.
I took a different approach and use a command line parameter to determine which class to use as my SpringApplication class. I only have a single main() method, but different Application classes with different configurations that are used based on a command line param.
我采用了不同的方法并使用命令行参数来确定将哪个类用作我的 SpringApplication 类。我只有一个 main() 方法,但是具有不同配置的不同应用程序类基于命令行参数使用。
I have a single class with a main() in it:
我有一个带有 main() 的类:
public static void main(String[] args) {
SpringApplication app;
if( ArrayUtils.contains(args, "--createdb")){
app = new SpringApplication(CreateDB.class);
args = (String[])ArrayUtils.add(args, "--spring.jpa.hibernate.ddl-auto=create");
} else {
app = new SpringApplication(Application.class);
}
app.setWebEnvironment(false);
app.setShowBanner(false);
app.addListeners(new ConfigurationLogger());
// launch the app
ConfigurableApplicationContext context = app.run(args);
// finished so close the context
context.close();
}
But I have 2 different SpringApplication classes: Application.class & CreateDB.class. Each class defines a different @ComponentScan
path as well as different @EnableAutoConfiguration
options and different @Configuration
options. Finally, based on my command line arguments, I can decide whether to programatically enable additional profiles/etc.
但我有 2 个不同的 SpringApplication 类:Application.class 和 CreateDB.class。每个类定义了不同的@ComponentScan
路径以及不同的@EnableAutoConfiguration
选项和不同的@Configuration
选项。最后,根据我的命令行参数,我可以决定是否以编程方式启用其他配置文件/等。
In my case, I want a different launcher to just create the DB schema and exit, so I've forced the command line parameter.
就我而言,我想要一个不同的启动器来创建数据库架构并退出,所以我强制使用命令行参数。
回答by chrylis -cautiouslyoptimistic-
I would suggest having a single main
but using Spring profiles (or configuration properties) to select one or other "entry point" @Configuration
class.
我建议使用单个main
但使用 Spring 配置文件(或配置属性)来选择一个或其他“入口点”@Configuration
类。