【问题标题】:Spring Boot - parent pom when you already have a parent pomSpring Boot - 当你已经有一个父 pom 时的父 pom
【发布时间】:2014-02-14 12:32:18
【问题描述】:

是否有特定的推荐方法将 spring-boot 父 pom 包含到已经具有所需父 POM 的项目中?

对于需要从组织父级扩展的项目,您有什么建议(这是非常常见的,甚至许多/大多数项目都发布到 Maven 中心,具体取决于它们来自的馈线存储库)。大多数构建内容与创建可执行 JAR 相关(例如,运行嵌入式 Tomcat/Jetty)。有一些方法可以构造事物,这样您就可以在不从父级扩展的情况下获得所有依赖项(类似于组合与继承)。但是,您无法以这种方式获得构建的东西。

因此,最好将所有 spring-boot 父 pom 包含在所需的父 POM 中,还是在项目 POM 文件中简单地具有 POM 依赖项。

其他选项?

TIA,

斯科特

【问题讨论】:

    标签: maven spring-boot parent-pom


    【解决方案1】:

    您可以像“bom”一样使用 spring-boot-starter-parent (参见 Spring 和 Jersey 其他现在支持此功能的项目),并且仅将其包含在依赖项管理中带有 scope=import 的部分。这样您就可以获得很多使用它的好处(即依赖管理),而无需替换实际父级中的设置。

    它做的另外两件事是

    1. 定义大量属性以快速设置您想要覆盖的依赖项版本
    2. 使用默认配置配置一些插件(主要是 Spring Boot maven 插件)。因此,如果您使用自己的父母,则必须手动执行这些操作。

    Spring Boot documentation中提供的示例:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    【讨论】:

    • 我猜这确实会引入插件 - 阴影等?
    • 没有。阅读链接:“你仍然可以保持依赖管理的好处(但不是插件管理)”。
    • @DaveSyer 请澄清:1:当我有我的自定义project-parent 时,我可以将spring-boot-starter-parent 添加到这个project-parent - 可以吗?在这种情况下我还需要spring-boot-dependencies 吗? 2:我使用 spring-boot:run 运行的普通 spring-boot 项目。如何使用不同的父 pom 运行项目?
    • 上面的sn-p代码就足以得到依赖管理功能。如果你想要插件(比如spring-boot:run),你需要分别反驳它们。如果需要,您可以从 Boot 父级复制粘贴。
    • 我知道这是一个旧线程,但它对我有很大帮助,谢谢。我最终在源代码maven.apache.org/guides/introduction/… 上阅读了有关此机制的信息。它解释了很多。
    【解决方案2】:

    使用 1.5.9.RELEASE 更新 2018-01-04。

    我在这里有完整的代码和可运行的示例https://www.surasint.com/spring-boot-with-no-parent-example/

    你需要这个作为基础

       <dependencyManagement>
            <dependencies>
                <dependency>
                    <!-- Import dependency management from Spring Boot -->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${springframework.boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    

    但这还不够,您还需要为 spring-boot-maven-plugin 显式定义目标(如果您使用 Spring Boot 作为父级,则不必显式定义)

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${springframework.boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    

    否则你无法构建为可执行的 jar 或 war。

    还没有,如果你使用的是JSP,你需要有这个:

    <properties>    
      <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
    

    否则,您将收到以下错误消息:

        [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project spring-boot-09: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executi
    ng in update mode) -> [Help 1]
    

    NO NO ,如果您使用带有“@”而不是“${}”的 Spring Boot 的 Maven 配置文件和资源过滤器(如此示例 https://www.surasint.com/spring-boot-maven-resource-filter/),这仍然不够。然后你需要在

    中显式添加这个
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    

    这个在

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
    

    查看链接https://www.surasint.com/spring-boot-with-no-parent-example/中的示例。

    【讨论】:

    • @Arvind 一个警告,我的例子是 1.5.9。您应该使用 1.5.11(昨天发布),因为此版本修复了严重的安全问题。
    • 正如您的博客所述,我尝试添加 maven-resources-plugin ,但它仍然给了我“目标中没有主要清单属性”。我尝试运行 spring-boot:run,这也不起作用:(
    • @sagar27 您使用的是 1.5.X 还是 2.x 哪个版本?如果是 2.x ,可能需要更多的东西。
    • 链接无法访问。
    • @NickLegend 是的,我知道,谷歌删除了我的整个虚拟机,没有给我任何警告,没有给我一封电子邮件......所以,不要使用 gcloud。
    【解决方案3】:

    根据 Surasin Tancharoen 的回答,您可能还想定义 maven surefire 插件

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
            </plugin>
    

    并且可能包括快速失败插件

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 2015-01-31
      相关资源
      最近更新 更多