ChatGPT解决这个技术问题 Extra ChatGPT

Spring Boot - 当你已经有一个父 pom 时的父 pom

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

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

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

其他选择?

TIA,

斯科特


M
Michael

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

它做的另外两件事是

定义一组属性以快速设置要覆盖的依赖项版本,使用默认配置配置一些插件(主要是 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 运行项目?
上面的代码片段足以获得依赖管理功能。如果您想要插件(例如 spring-boot:run),您需要分别反驳它们。如果需要,您可以从 Boot parent 复制粘贴。
我知道这是一个旧线程,但它对我有很大帮助,谢谢。我最终在源 maven.apache.org/guides/introduction/… 处阅读了有关此机制的信息。它解释了很多。
S
Surasin Tancharoen

使用 1.5.9.RELEASE 更新 2022-05-29。

我在这里有完整的代码和可运行的示例 https://github.com/surasint/surasint-examples/tree/master/spring-boot-jdbi/9_spring-boot-no-parent(请参阅 README.txt 以了解您可以尝试)

你需要这个作为基础

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

根据 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>

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅