【问题标题】:Maven configuration with Spring Boot & multi modules - run application in Intellij使用 Spring Boot 和多模块的 Maven 配置 - 在 Intellij 中运行应用程序
【发布时间】:2015-05-29 05:26:34
【问题描述】:

我目前正在使用 Spring Boot 开发 REST API。

我是 Maven 新手,刚刚开始使用 IDEA 进行编码(还不太了解这个 IDE),我遇到了一个问题...

这是我的项目结构:

  • 父级
    • pom.xml
    • 主模块
      • 控制器
      • App.java(Spring Boot 主类)
      • pom.xml
    • 子模块(需要主模块作为依赖)
      • 控制器
      • pom.xml

所以当我在 Intellij 中运行项目时,它会启动,并且我可以访问主模块控制器中定义的所有 URL。但不是子模块控制器中的那些......看起来只加载了主模块。

这是我的父 pom.xml :

<project>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.1.RELEASE</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<name>Test :: Test :: Parent POM</name>

<groupId>test.test.test</groupId>
<artifactId>project-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
    <!-- Specify Java Compiler Version -->
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <!-- Spring -->
    <spring-boot.version>1.2.1.RELEASE</spring-boot.version>

    <!-- Sonar -->
    <sonar-maven-plugin.version>2.5</sonar-maven-plugin.version>
    <sonar.language>java</sonar.language>
    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>

    <!-- Plugin -->
    <maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
    <jacoco-maven-plugin.version>0.7.3.201502191951</jacoco-maven-plugin.version>


    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<modules>
    <module>submodule</module>
    <module>main</module>
</modules>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*IT.java</include>
                        <include>**/*Story.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco-maven-plugin.version}</version>
            <configuration>
                <destFile>${project.basedir}/../target/jacoco.exec</destFile>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>${sonar-maven-plugin.version}</version>
        </plugin>

    </plugins>
</build>

</project>

这里是我的主模块 pom.xml :

<project>
<parent>
    <artifactId>project-parent</artifactId>
    <groupId>test.test.test</groupId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>main</artifactId>

<name>Test :: Test :: Main</name>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <commons-lang3.version>3.3.2</commons-lang3.version>
    <commons-codec.version>1.10</commons-codec.version>
    <jsr305.version>3.0.0</jsr305.version>

    <!-- Testing dependencies -->
    <http-commons.version>4.3.6</http-commons.version>
    <jbehave.version>3.9.5</jbehave.version>
    <assertj.version>1.7.1</assertj.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>${jsr305.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons-lang3.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${commons-codec.version}</version>
    </dependency>

    <!-- Test dependencies -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>


    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${http-commons.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>fluent-hc</artifactId>
        <version>${http-commons.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jbehave</groupId>
        <artifactId>jbehave-spring</artifactId>
        <version>${jbehave.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>${assertj.version}</version>
    </dependency>

</dependencies>

<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

这里是子模块 pom.xml :

<project>
<parent>
    <artifactId>project-parent</artifactId>
    <groupId>test.test.test</groupId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>submodule</artifactId>

<name>Test :: Test :: Submodule</name>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <commons-lang3.version>3.3.2</commons-lang3.version>
    <commons-codec.version>1.10</commons-codec.version>
    <jsr305.version>3.0.0</jsr305.version>

    <!-- Testing dependencies -->
    <http-commons.version>4.3.6</http-commons.version>
    <jbehave.version>3.9.5</jbehave.version>
    <assertj.version>1.7.1</assertj.version>
</properties>

<dependencies>
    <dependency>
        <groupId>test.test.test</groupId>
        <artifactId>main</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
</project>

我认为这是正确的,但不确定... 我在 Intellij 中使用带有配置的 Maven 运行项目:

  • 工作目录是根目录(不是子模块)
  • 命令行mvn spring-boot:run -Drun.arguments=--spring.profiles.active=dev -e -pl main
  • 带有 parent.main.App 的属性开始类

需要您的帮助来配置所有这些东西以运行 Spring Boot,并在 IDE 中加载所有子模块以用于开发目的......因为我真的不知道我的配置有什么问题!

谢谢!

【问题讨论】:

    标签: java maven intellij-idea module spring-boot


    【解决方案1】:

    你需要告诉 SpringBoot 在哪里寻找你的控制器。默认情况下,这只发生在您的 @SpringBootApplication 类的子包中(可能不包括您的子模块)。

    为了改变你可以使用@ComponentScan("path.to.package")来改变默认包。

    此外,您可以使用@EntityScan 对可能在您的子模块中的@Entity 类执行相同的操作。

    【讨论】:

    • 谢谢你的回答。现在我只有 1 个子模块,但将来会有更多,我需要包含所有到 @ComponentScan 的路径吗?有没有办法自动包含不同子模块中存在的所有控制器?
    • 如果他们都有一个共同的根包,你可以指定它,它也会扫描所有的子包。
    • 如果有很多类要扫描和加载,这当然会增加应用程序的启动时间。但这在实践中并不重要。
    • 我不明白为什么标准的 Spring 包没有被这种方法选中:Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfiguration': Injection of autowired dependencies failed;
    • @ACV 我不太明白你的问题是什么。您应该使用完整的堆栈跟踪提出不同的问题。
    【解决方案2】:

    您的项目结构如下:

    • 父级
      • pom.xml
      • 主模块
        • 控制器
        • App.java(Spring Boot 主类)
        • pom.xml(添加子模块到主模块作为依赖)
      • 子模块
        • 控制器
        • pom.xm

    如果你的App.java在一个包中:com.xxxx.pro,那么设置子模块的包是com.xxx.pro,比如你的子模块的控制器为TestController.java,代码为:

    package com.xx.pro.web;
    @RestController
    public class TestController{
    
    }
    

    所以,这个子模块的 TestController 将被 App.java 删除。试试吧,祝你好运。

    【讨论】:

      【解决方案3】:

      还要注意,如果 JPA 实体和存储库不在 Application.java 包的子包中,则必须在 Application 类中声明 @Entityscan 和 @EnableJpaRepositories,例如:

      @Configuration
      @ComponentScan(basePackages="com.my.pack")
      @EnableAutoConfiguration
      @EnableJpaRepositories(basePackages="com.my.pack")
      @EntityScan(basePackages="com.my.pack")
      public class Application{
      
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-04-15
        • 2017-01-04
        • 2019-03-26
        • 2017-02-09
        • 1970-01-01
        • 2014-02-25
        • 2017-12-01
        • 2021-11-16
        • 2016-07-03
        相关资源
        最近更新 更多