6

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

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

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

当前代码:

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

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

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

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

    </project>
4

3 回答 3

11

我没有关于您的项目的所有详细信息,但我最好的猜测是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>

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

于 2015-12-21T07:40:16.863 回答
0

以下是解决此问题的两种方法:

  1. 您可以添加像提到的@Stephane Nicoll这样的跳过属性。但是,这将完全忽略该模块内的测试用例。https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/it-skip.html
  2. 另一种选择是添加一个分类器属性,以便从此模块中生成一个单独的可执行 jar。https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html

    <executions>
              <execution>
                <id>repackage</id>
                <goals>
                  <goal>repackage</goal>
                </goals>
                <configuration>
                  <classifier>exec</classifier>
                </configuration>
              </execution>
    

此修复将确保依赖模块获得所需的 jar,并且源模块仍然是可执行的。

于 2018-11-05T21:40:05.273 回答
0

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

mvn dependency:tree 

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

spring.main.web-environment=false
于 2015-12-21T07:36:13.523 回答