弹簧启动与maven多模块项目

Sha*_*hal 6 maven-module spring-boot

我有一个maven多模块项目,设计类似于以下SO帖子中的第一个答案: 带有Spring Boot的多模块maven

现在我想要一个通用的maven模块,它可以包含一些模型供多个微服务使用.如果我将这个常见项目作为第一级父pom的子项(这样所有依赖项通过引导注入,如jpa,jackson等可用于公共),那么STS/Spring将其检测为启动应用程序并抱怨没有Main关于maven构建的课程.

有人可以建议我如何实现这一目标吗?

现行代码:

parent pom.xml :(仅包含相关部分)

<project>
<name>...</name>
<groupId>...</groupId>
<artifactId>...</artifactId>
<packaging>pom</packaging>
<version>...</version>
<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>Brixton.M3</version>
    <relativePath />
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)

子(公共模块)pom.xml(只有相关部分),不是启动应用程序:

<project>
<artifactId>...</artifactId>
<version>...</version>
<name>...</name>

<parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
</parent>

</project>
Run Code Online (Sandbox Code Playgroud)

Ste*_*oll 7

我没有关于你的项目的所有细节,但我最好的猜测是spring-boot-maven-plugin在父项上定义(或者你spring-boot-starter-parent在根pom中使用).这有效地要求构建将您的模块打包为Spring Boot应用程序(这不是您想要的).

STS可能会查找该提示,以确定模块是否包含Spring Boot应用程序.如果它寻找一个用@EnableAutoConfiguration(或SpringBootApplication)注释的主类,也许会更好.

您可以通过指定重新打包目标skip属性轻松地(从构建方)修复问题

  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
          <skip>true</skip>
    </configuration>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

如果STS仍然将模块作为Spring Boot应用程序选中,我会在他们的跟踪器中创建一个问题

  • 谢谢您的帮助。我按照您的建议尝试了,但现在确实有用。在子pom中添加了以下部分:`&lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt; org.springframework.boot &lt;/ groupId&gt; &lt;artifactId&gt; spring-boot-maven-plugin &lt;/ artifactId&gt; &lt;executions&gt; &lt;execution&gt; &lt;目标&gt; &lt;goal&gt;重新打包&lt;/ goal&gt; &lt;/ goals&gt; &lt;configuration&gt; &lt;skip&gt; true &lt;/ skip&gt; &lt;/ configuration&gt; &lt;/ execution&gt; &lt;/ executions&gt; &lt;/ plugin&gt; &lt;/ plugins&gt; &lt;/ build&gt;`但是这减少了我的构建时间,因为通用模块没有重新打包为可执行jar (2认同)

R K*_*jal 0

通常,如果模块中不存在 Web 容器,Spring Boot 将不会启动该容器。我建议您使用以下命令分析您的趋势

mvn dependency:tree 
Run Code Online (Sandbox Code Playgroud)

另一种强力的确保方法是在您的 application.properties 中使用此配置

spring.main.web-environment=false
Run Code Online (Sandbox Code Playgroud)