简体   繁体   中英

Can spring-boot-maven-plugin be used for non spring project?

The spring-boot-maven-plugin main goal is to package spring boot application into executable jar with all dependencies. Can it be used in projects without spring or spring boot? I meen not using spring dependencies, but just classloader mechanism for loading depended jars.

Yes you can! Actually I tested and I was happy to find the spring-boot-maven-plugin repackage is completely agnostic of what the Main class uses and is for the most part completely decoupled from the Springframework (its unfortunate the documentation doesn't mention this but I guess Spring really wants use their stuff).

It is basically Spring's version of the onejar plugin (but it also adds its own shells script to the jar).

The spring-boot-maven-plugin repackage ( spring-boot:repackage ) essentially creates its own micro container with a small loader that looks for a specific layout (where the jars are located) which is determined by your project packaging (ie <packaging>war</packaging> or <packaging>jar</packaging> ). The small loader does NOT load up any Springframework code .

Of course your Main class has to know how to start up a servlet container (or whatever you plan on loading). Most servlet containers have documentation on how to run in embedded mode.

As for a code example you would put the following in the <build> section :

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <fork>true</fork>
            </configuration>
        </plugin>

Then you run the build like mvn package spring-boot:repackage . The documentation for the plugin is actually not bad.

I guess spring boot maven plugin is meant only for spring project to bring all dependencies in one shot.

If you have a problem with identifying dependencies you can simply create one spring boot project. Once done you will get all the libraries in your spring project. Copy or import those libraries in your project with out spring.

With this approach setup of the project with all dependencies will become easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM