简体   繁体   中英

How to launch Spring Boot app from another Maven module?

I got a project which consists of two modules.

  • The first one is Spring Boot REST Web Service
  • And the second one is the code that should work with this service.

The issue: I need to start Web Service from another module from the code.

Of course, the best option here is to deploy Service to some remote host, but what are the options if I want to launch Service on the local machine?

The first idea was packaging Service module, then copying jar to the second module using maven-dependency-plugin and launching it as:

Runtime.getRuntime().exec("java -jar my-rest-service.jar");

Can I start Spring Boot app right from another module? Call Application.main() method or something?

You can build and install first module into your local maven repository ( .m2 folder by default) as the jar library. Then you can use this library in your second module as the maven dependency.

After that you can start your application (second module) as usually spring-boot starts - with the main method.


Example:

First module - library:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <url>https://example.com/module1</url>

    <groupId>com.example</groupId>
    <artifactId>module1</artifactId>
    <name>module1</name>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>12</java.version>
    </properties>

    <build>
        <finalName>module1</finalName>
        <defaultGoal>install</defaultGoal>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Second module - application:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <url>https://example.com/module2</url>

    <groupId>com.example</groupId>
    <artifactId>module2</artifactId>
    <name>module2</name>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <!--packaging>war</packaging-->

    <properties>
        <java.version>12</java.version>
    </properties>

    <build>
        <finalName>module2</finalName>
        <defaultGoal>package</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- com.example -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>module1</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

The Spring Boot Maven plugins package our application as executable JARs – such a file can't be used in another project since class files are put into BOOT-INF/classes .

In order to share classes with another project, the best approach to take is to create a separate jar containing shared classes, then make it a dependency of all modules that rely on them.

So in your spring boot module, you need to add classifier like:

...
<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <classifier>exec</classifier>
      </configuration>
    </plugin>
  </plugins>
</build>

and of course, you need to add your spring boot module dependency in your pom where you want to use then you should be able to use Application.java .


Please check here: Using a Spring Boot Application as a Dependency .

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