【发布时间】:2022-01-20 14:08:55
【问题描述】:
背景 我使用带有 Spring 的 REST 接口开发了一个 webapp。通常我使用 Eclipse 开发和执行我的软件。当我的程序启动后,我在 Chrome 中输入 localhost:8080/RESTPath 以使用我的程序访问 RESTPath。
问题 现在我想用 Maven 构建和执行我的程序,而不是用一个可执行的 jar。问题是我对 Maven 的了解有限,所以我不知道该怎么做。当然,我已经用谷歌搜索了我的问题,并且测试了一些示例,但没有成功。到目前为止,我认为我应该在我的 POM.xml 中使用插件 tomcat7-maven-plugin 来生成一个 jar 以使用 java -jar file.jar 执行。我已经按照使用 tomcat7-maven-plugin 的 webapp 示例进行操作,但从未生成 jar 文件:https://www.baeldung.com/executable-jar-with-maven。
我的 POM:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- My project -->
<groupId>tobbe</groupId>
<artifactId>stocks-rest</artifactId>
<packaging>jar</packaging>
<version>1.0.2-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- My projects -->
<dependency>
<groupId>tobbe</groupId>
<artifactId>Stocks_Backend</artifactId>
<version>1.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>tobbe</groupId>
<artifactId>Other</artifactId>
<version>1.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
问题 我的 POM 应该如何看起来像我应该如何使用 Maven 构建(即 mvn clean compile)以创建可执行 jar?
【问题讨论】: