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

What do you recommend for projects that need to extend from an organizational parent (this is extremely common and even something many/most projects published to Maven central depending on the feeder repos they come from). Most of the build stuff is related to creating executable JARs (e.g. running embedded Tomcat/Jetty). There are ways to structure things so that you can get all the dependencies without extending from a parent (similar to composition vs. inheritance). You can't get a build stuff that way though.

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

其他选项吗?

TIA,

斯科特


你可以像使用“bom”一样使用Spring -boot-starter-parent (c.f. Spring和Jersey其他项目现在也支持这个特性),并且只在依赖管理部分中使用scope=import将它包含进来。这样你就得到了很多使用它的好处(即依赖管理),而不需要替换实际父目录中的设置。

它所做的其他两件主要事情是

定义一组属性,用于快速设置要覆盖的依赖项的版本 使用默认配置配置一些插件(主要是Spring Boot maven插件)。如果你使用自己的父级,这些就是你必须手动做的事情。

Spring Boot文档中提供的示例:

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

更新2022-05-29与1.5.9.RELEASE。

我在这里有完整的代码和可运行的示例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]

不不,如果你用“@”而不是“${}”(比如这个例子https://www.surasint.com/spring-boot-maven-resource-filter/)使用Maven配置文件和Spring引导资源过滤器,这仍然是不够的。然后需要显式地添加这个

    <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/中的示例。


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