一尘不染

使用Spring Boot和多模块进行Maven配置-在Intellij中运行应用程序

spring-boot

我目前正在使用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>

我认为这是正确的,但不确定…我使用config的Maven在Intellij中运行该项目:

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

需要您的帮助来配置所有内容以运行Spring Boot,并出于开发目的在IDE中加载所有子模块…因为我很不知道配置中有什么问题!

谢谢 !


阅读 278

收藏
2020-05-30

共1个答案

一尘不染

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

为了更改,您可以使用@ComponentScan("path.to.package")更改默认程序包。

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

2020-05-30