gpt4 book ai didi

java - 如何在没有依赖项的情况下编译 spring-boot-maven-plugin?

转载 作者:行者123 更新时间:2023-11-28 22:16:45 26 4
gpt4 key购买 nike

我正在尝试使用 Spring Boot 在 Java 中进行微服务练习,为此我在不同的项目中开发了两个 Web 服务,目的是将它们部署在 tomcat 中,就像两个独立的文件 (.war) 一样。

我已经阅读了有关设置 tomcat 以在指定文件夹中具有依赖项以与其他服务共享它的信息,这样就不会在所有服务中增加相同的库。

问题是,当我通过 Artifact spring-boot-maven-plugin 使用 maven 编译服务时,.war 文件中始终包含依赖项。因为我想知道是否有人知道如何配置 maven从 .war 文件中删除依赖项......在 Spring Boot 中。

.war 跟随着里面的依赖,编辑:我已经添加了像迈克尔波特所说的提供和执行。它工作正常。我的 pom.xml 如下:

<?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>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>demo1</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>

最佳答案

要让 Maven 不在 WAR 文件中包含依赖项,您需要将其scope 指定为provided。 Maven 官方文档中作用域的描述:

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
<scope>provided</scope>
</dependency>

依赖项将被下载以编译源代码,但不会打包到 WAR 中。

关于 spring-boot-maven-plugin。默认情况下,它重新打包一个 WAR,允许您从控制台启动它。因此,它将所有必需的依赖项打包到存档中——即使是在 provided 范围内。您可以在目标目录中看到两个文件:{project-name}.war{project-name}.war.original - 不应该重新打包的文件包含提供的依赖项。要禁用重新打包,您应该将 spring-boot-maven-plugin 配置更改为以下内容:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>

然后您需要将所需的依赖项放置到tomcat/lib 文件夹并重新启动Tomcat。

关于java - 如何在没有依赖项的情况下编译 spring-boot-maven-plugin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44448183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com