Spring Boot - 当你已经拥有父pom时父pom

Sco*_* C. 163 maven parent-pom spring-boot

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

对于需要从组织父级扩展的项目,您有什么建议(这是非常常见的,甚至很多/大多数项目都发布到Maven中心,具体取决于它们来自的支线回购).大多数构建内容与创建可执行JAR(例如,运行嵌入式Tomcat/Jetty)有关.有一些方法可以构建事物,以便您可以获得所有依赖项而无需从父项扩展(类似于组合与继承).你无法通过这种方式获得构建内容.

因此,最好在所需的父POM中包含所有spring-boot父pom,或者只是在项目POM文件中包含POM依赖项.

其他选择?

TIA,

斯科特

Dav*_*yer 165

您可以使用spring-boot-starter-parent作为"bom"(参见Spring和Jersey其他支持此功能的项目),并将其仅包含在scope = import的依赖关系管理部分中.这样您就可以获得很多使用它的好处(即依赖管理)而不替换实际父级中的设置.

它做的另外两件事是

  1. 定义一组属性,以便快速设置要覆盖的依赖项版本
  2. 配置一些默认配置的插件(主要是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>
Run Code Online (Sandbox Code Playgroud)

  • 我知道这是一个老线程,但它帮助了我很多,谢谢.我最后在源代码https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies上阅读了这个机制.它解释了很多. (3认同)
  • 没有.阅读链接:"你仍然可以保持依赖管理(但不是插件管理)的好处". (2认同)
  • 不,那里没有。它必须是父 pom 才能继承 `&lt;properties/&gt;`。 (2认同)

Sur*_*oen 36

使用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>
Run Code Online (Sandbox Code Playgroud)

但这还不够,你还需要为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>
Run Code Online (Sandbox Code Playgroud)

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

还没有,如果你使用JSP,你需要这样:

<properties>    
  <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
Run Code Online (Sandbox Code Playgroud)

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

    [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]
Run Code Online (Sandbox Code Playgroud)

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

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
Run Code Online (Sandbox Code Playgroud)

而这在

        <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>
Run Code Online (Sandbox Code Playgroud)

请参阅链接https://www.surasint.com/spring-boot-with-no-parent-example/中的示例.