【问题标题】:How to remove all the jars from a spring boot application using maven如何使用 maven 从 Spring Boot 应用程序中删除所有 jar
【发布时间】:2017-03-28 05:43:28
【问题描述】:

我需要从我使用 spring boot 构建的 war 中删除所有 jars(整个 lib 和 lib-provided 文件夹),因为 Web 应用程序将使用 commons 共享库部署在 Websphere 上。

有什么办法可以做到这一点,因为无法使用 war 插件中的排除项,而 boot 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>artifactId</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <name>Application Name</name>
    <description>Application Description</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
        <powermock.version>1.6.5</powermock.version>
        <snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
        <hibernate.version>4.2.15.Final</hibernate.version>
    </properties>

    <dependencies>
        <!-- SPRING DEPENDENCIES -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-jpa-2.0-api</artifactId>
                    <groupId>org.hibernate.javax.persistence</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-validator</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-legacy</artifactId>
            <version>1.0.2.RELEASE</version>
        </dependency>

        <!-- DATABASE RELATED DEPENDENCIES -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
            <scope>provided</scope>
        </dependency>

        <!-- TEST & DEVELOPEMENT DEPENDENCIES -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Documentation.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.2</version>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html</backend>
                            <doctype>book</doctype>
                            <attributes>
                                <snippets>${snippetsDirectory}</snippets>
                            </attributes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version><!--$NO-MVN-MAN-VER$ -->
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.outputDirectory}/static/docs
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>
                                        ${project.build.directory}/generated-docs
                                    </directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 完全不要使用重新包装。只是一个常规的战争文件,就像没有 Spring Boot 一样。
  • 不是我要找的..
  • 嗯,不清楚您在寻找什么。我的观点是,您的用例没有特定的 Spring Boot。如果您想在提供库的环境中部署它,只需使用常规战争即可。所以不要使用maven-war-plugin 的重新打包和常规功能。
  • @StephaneNic​​oll 是对的,如果您想要将 Web 应用程序部署到任何容器中,则不需要 spring-boot。但如果你坚持,请查看我更新的答案。

标签: java maven spring-boot build war


【解决方案1】:

如果您的意思是要排除 your-project.war/WEB-INF/lib 中的 jar,那么您可以尝试使用 &lt;scope&gt;provided&lt;/scope&gt;

例子

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
    <scope>provided</scope>
</dependency>

This is example 给你。

编辑

通过查看和测试您的pom.xml,您可以删除这些行:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

这样,我再也看不到 lib 提供的目录了。

HTH。

【讨论】:

  • 这是我的问题的部分解决方案,因为一切都在 lib 提供的内部......我需要一个更轻的战争文件......
  • 什么是lib-provided 文件夹?它在您的应用程序中的什么位置?
  • 当我在 WEB-INF 中构建我的应用程序时,我同时提供了 lib 和 lib-provided。 lib-provided 包含我的 pom 中标记为提供的所有 jar。
  • 这个lib-provided 实际存在于您的项目目录中吗?因为 AFAIK,标准 maven 项目结构没有任何名为 lib-provided 的目录。或者,如果您不介意,请将您的 pom.xml 和任何相关的 maven 文件放入您的问题中。删除任何密码或任何敏感数据(如果存在)。
猜你喜欢
  • 2020-07-16
  • 2017-01-26
  • 2018-11-19
  • 2019-07-17
  • 2016-01-16
  • 2020-11-10
  • 2020-01-19
  • 2015-04-14
  • 1970-01-01
相关资源
最近更新 更多