如何编译没有依赖项的spring-boot-maven-plugin? [英] how to compile spring-boot-maven-plugin without dependencies?

查看:678
本文介绍了如何编译没有依赖项的spring-boot-maven-plugin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to do an excercise with micro-services in Java with Spring boot, for this I am developing two web services in different projects with the intention of deploying them in tomcat like two independent files (.war).

我读过有关设置tomcat的信息,要求在指定文件夹中使用依赖项与其他服务共享它,这样就不会在所有服务中增加相同的库。

I have read about set up tomcat to have the dependencies in an specified folder to share it with other services and this way not to increase the same libraries in all services.

问题在于,当我使用maven通过工件spring-boot-maven-plugin编译服务时,.war文件始终具有依赖关系。因为我想知道是否有人知道如何将maven配置为
从Spring报文中移除.war文件中的依赖项.......

The ploblem is that when I compiled the service with maven through the artifact spring-boot-maven-plugin the .war files always has the dependencies inside. Because of I want to know if someone know how to configure maven to remove dependencies from .war file..... in Spring Boot.

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

The .war follows with the dependencies inside, Edited: I have added the provided like said Michael Potter and the execution. it works fine. My pom.xml is the follow:

<?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文件中包含依赖项需要将范围指定为提供。来自官方Maven文档的范围描述:

For Maven not to include dependency in your WAR file you need to specify its scope to provided. The description of the scope from official Maven documentation:


这很像编译,但表示你期望JDK或
容器在运行时提供依赖性。例如,当
为Java Enterprise Edition构建Web应用程序时,您需要将
设置对Servlet API和相关Java EE API的依赖性设置为
作用域,因为Web容器提供了这些类。这个
范围仅在编译和测试类路径上可用,并且
不可传递。

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中。

The dependency will be downloaded to compile the sources, but not packed in the WAR.

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

Concerning spring-boot-maven-plugin. By default it makes repackaging of a WAR that allows you to launch it from console. Thus, it packages all required dependencies to the archive - even with the provided scope. You can see in your target directory two files: {project-name}.war which is repackaged and {project-name}.war.original - the one that should not contain provided dependencies. To disable repackaging you should change spring-boot-maven-plugin configuration to the following:

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

Then you need to place the required dependency to tomcat/lib folder and restart the Tomcat.

这篇关于如何编译没有依赖项的spring-boot-maven-plugin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆