首页 文章

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

提问于
浏览
134

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

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

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

其他选择?

TIA,

斯科特

2 回答

  • 143

    You can use the spring-boot-starter-parent like a "bom" (cf Spring和Jersey其他现在支持此功能的项目),并且仅在scope = 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>1.1.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
       </dependencies>
    </dependencyManagement>
    
  • 18

    使用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]
    

    不,如果您使用带有"@"的Spring引导的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/中的示例 .

相关问题