java - 如何从另一个 Maven 模块启动 Spring Boot 应用程序?
问题描述
我有一个由两个模块组成的项目。
- 第一个是 Spring Boot REST Web Service
- 第二个是应该与此服务一起使用的代码。
问题:我需要从代码中的另一个模块启动 Web 服务。
当然,这里最好的选择是将 Service 部署到某个远程主机,但是如果我想在本地机器上启动 Service,有哪些选择呢?
第一个想法是打包服务模块,然后复制jar
到第二个模块,使用maven-dependency-plugin
并启动它:
Runtime.getRuntime().exec("java -jar my-rest-service.jar");
我可以直接从另一个模块启动 Spring Boot 应用程序吗?调用Application.main()
方法还是什么?
解决方案
您可以将build
第install
一个模块作为库放入本地 maven 存储库(.m2
默认为文件夹)jar
。然后你可以在你的第二个模块中使用这个库作为 maven 依赖项。
之后,您可以像通常一样启动您的应用程序(第二个模块)spring-boot
- 使用该main
方法。
例子:
第一个模块 - 库:
<?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>
第二个模块 - 应用程序:
<?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>
推荐阅读
- python - Is there a way that I can have a file download within the success function of an ajax call?
- android - 在 Android 上 React Native Facebook SDK 初始化
- java - 有没有办法将双击动作监听器添加到 JScrollPane 上?
- docker - 如何在 docker-compose 命名卷中使用现有目录 ~/.ssh?
- c++ - 强制堆分配超过 4 GB
- python - 仅使用股票 python2.7 库对特定服务器的 DNS 查询
- c++ - 使用 CLANG 内联汇编创建 C++ 预增量操作
- android - 如何找到官方的 android studio 旧版本
- scala - 如何解决使用 magnolia 时“无法找到类型证据参数的隐含值”的问题
- testing - test.before 钩子覆盖 feature.beforeEach 钩子。逻辑不好?