简体   繁体   English

如何从另一个 Maven 模块启动 Spring 引导应用程序?

[英]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第一个是 Spring 启动 REST Web 服务
  • 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.问题:我需要从代码中的另一个模块启动 Web 服务。

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?当然,这里最好的选择是将 Service 部署到某个远程主机,但是如果我想在本地机器上启动 Service,有哪些选择呢?

The first idea was packaging Service module, then copying jar to the second module using maven-dependency-plugin and launching it as:第一个想法是打包服务模块,然后使用maven-dependency-pluginjar复制到第二个模块并启动它:

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

Can I start Spring Boot app right from another module?我可以直接从另一个模块启动 Spring 启动应用程序吗? Call Application.main() method or something?调用Application.main()方法还是什么?

You can build and install first module into your local maven repository ( .m2 folder by default) as the jar library.您可以将第一个模块作为jarbuildinstall到本地 maven 存储库(默认为.m2文件夹)中。 Then you can use this library in your second module as the maven dependency.然后你可以在你的第二个模块中使用这个库作为 maven 依赖项。

After that you can start your application (second module) as usually spring-boot starts - with the main method.之后,您可以像通常spring-boot一样启动您的应用程序(第二个模块) - 使用main方法。


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 . 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.为了与另一个项目共享类,最好的方法是创建一个单独的 jar 包含共享类,然后使其成为所有依赖它们的模块的依赖项。

So in your spring boot module, you need to add classifier like:因此,在您的 spring 引导模块中,您需要添加如下分类器:

...
<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 .当然,您需要在要使用的 pom 中添加 spring 引导模块依赖项,然后您应该能够使用Application.java


Please check here: Using a Spring Boot Application as a Dependency .请在此处查看:使用 Spring 引导应用程序作为依赖项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Spring Boot 测试 Maven 模块项目 - How to test Maven module project with Spring Boot Maven Spring Boot插件:如何从另一个项目运行spring boot - Maven Spring Boot Plugin : how to run spring boot from another project 如何从另一个 Maven 模块导入 Spring 应用程序上下文? - How to import Spring application context from another maven module? 如何从另一个模块访问 Spring Boot Quartz Bean? - How to access Spring Boot Quartz Bean from another module? 如何在Spring Boot中将所有通用依赖项从Maven模块移至父模块 - How to move all the common dependencies from maven module into parent module in spring boot 来自另一个模块的 Spring Boot 自动装配 - Spring Boot Autowiring From Another Module Spring Boot Maven多个模块 - Spring boot maven multiple module Spring Boot从不同的Maven模块读取属性文件 - spring boot read properties file from different maven module 如何从spring boot项目创建jar,我们要在另一个spring boot应用中使用的jar? - how to create jar from spring boot project , this jar we want to use in another spring boot app? 在Maven多模块项目中,如何从一个模块到另一个模块访问spring bean - In a maven multi-module project, how to access the spring beans from one module to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM