Spring Boot - How to specify an alternate start-class? (Multiple Entry Points)

人走茶凉 提交于 2021-02-06 01:55:11

问题


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?

According to their documentation, the property loader.main specifies the name of the main class to launch.

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

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.

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.

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

I created my fat jar (mvn package) then called the alternate main like this:

java -jar -Dloader.main=com.mycompany.AlternateMain MyJar.jar

Thanks for the help!


回答1:


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.

META-INF/MANIFEST.MF

Main-Class: org.springframework.boot.loader.PropertiesLauncher



回答2:


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.

I have a single class with a main() in it:

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.

In my case, I want a different launcher to just create the DB schema and exit, so I've forced the command line parameter.




回答3:


I would suggest having a single main but using Spring profiles (or configuration properties) to select one or other "entry point" @Configuration class.



来源:https://stackoverflow.com/questions/31076911/spring-boot-how-to-specify-an-alternate-start-class-multiple-entry-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!