【问题标题】:Maven module using spring-boot使用 spring-boot 的 Maven 模块
【发布时间】:2014-01-10 22:35:14
【问题描述】:

我喜欢在 Maven 中通过创建模块来配置我的应用程序:

<groupId>com.app</groupId>
<artifactId>example-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>app-api</module>
    <module>app-impl</module>
    <module>app-web</module>
</modules>

然后模块使用“example-app”作为父级。

现在我想为我的 Web 应用程序使用“spring-boot”。

有没有办法配置 maven,让我的“app-web”是一个 spring-boot 应用程序?

我面临的问题是您必须使用 spring-boot 作为父级。

【问题讨论】:

    标签: java spring maven spring-boot


    【解决方案1】:

    您不必使用 spring-boot-starter-parent,它只是一种快速入门的方法。它提供的只是依赖管理和插件管理。你可以自己做,如果你想要一个中途步骤,你可以使用 spring-boot-dependencies(或等效的父级)来管理依赖项。为此,请像这样使用scope=import

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <type>pom</type>
                <version>1.0.2.RELEASE</version>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    【讨论】:

    • 轻微编辑 - 你需要在 下添加一个 元素,否则它似乎也对我有用。
    • @Dave “或等效的父母”究竟是什么意思?你能把它也包括在例子中吗?
    • 我的意思是(就dependencyManagement 而言)scope=import 或使用该POM 作为父级是等效的。 (当您将其用作父级时,您将获得其他功能,例如插件配置。)
    • 当我将 spring-boot-starter-parent 的声明替换为“spring-boot-dependencies”工件时,我收到以下错误:在 target/my-app- 中没有主清单属性0.1.0.jar 是不是意味着我得把spring-boot-starter-parent里的所有插件都复制过来?
    • 我猜这意味着你必须至少复制其中一个:-)。如果它是一个普通的 jar,那么可能只是 spring-boot 插件。
    【解决方案2】:

    另一种选择是在父pom中包含spring boot的父声明,如post所示

    示例应用 pom.xml:

    <project>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.5.RELEASE</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        // rest of the example-app pom declarations
    </project>
    

    之后,在 poms 模块(app-web、app-impl 等)中,您将 example-app 声明为父级,但现在您可以像通常在常规项目中那样包含启动器依赖项。

    app-web pom.xml:

    <project>
        <parent>
            <groupId>org.demo</groupId>
            <artifactId>example-app</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <name>app-web</name>
        <artifactId>app-web</artifactId>
        <version>1.0-SNAPSHOT</version> 
        <packaging>war</packaging>
    
        <dependencies>
            <dependency>
                <groupId>org.demo</groupId>
                <artifactId>app-api</artifactId>
                <version>1.0-SNAPSHOT</version> 
            </dependency>
            <dependency>
                <groupId>org.demo</groupId>
                <artifactId>app-impl</artifactId>
                <version>1.0-SNAPSHOT</version> 
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
        // rest of the app-web pom declarations
    </project>
    

    关于版本管理,我在这些示例中使用的并不是最佳实践,但由于超出了问题的范围,我跳过了依赖管理和父属性的使用。

    另外,如果每个模块都有一个starter,你可以在父pom中声明依赖,然后所有模块都会继承它(例如spring-boot-starter-test)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 2014-11-12
      • 1970-01-01
      相关资源
      最近更新 更多