如何 spring-boot: 在多模块项目的根 pom.xml 上运行

qxl*_*lab 5 java maven spring-boot

我有一个带有 Spring Boot 的多模块项目。

我的根 pom.xml 只包含这个:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.something</groupId>
    <artifactId>my-application</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>library</module>
        <module>application</module>
    </modules>

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

spring-boot-maven-pluginapplication模块上,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.something.tcc</groupId>
    <artifactId>my-application-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <jsf.version>2.2.11</jsf.version>
    </properties>

    <dependencies>
        <!-- use this for automatic hot deployment on code changes -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>                
            </plugin>
        </plugins>
    </build>

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

所以今天,从我的根目录,我必须像这样执行我的应用程序:

mvn spring-boot:run -pl application
Run Code Online (Sandbox Code Playgroud)

如果我尝试mvn spring-boot:run,我会收到错误:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) on project my-application-library: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1]
Run Code Online (Sandbox Code Playgroud)

我想知道我需要做什么才能在我的根目录中准确执行以下命令:

mvn spring-boot:run
Run Code Online (Sandbox Code Playgroud)

编辑:根据评论的建议,我尝试将配置从application模块移动到根 pom.xml,但我仍然收到相同的错误:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.something</groupId>
    <artifactId>my-application</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

<properties>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.something.tcc.Main</start-class>
</properties>

    <modules>
        <module>library</module>
        <module>application</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>  
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>                
            </plugin>
        </plugins>
    </build> 

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

谢谢

Hos*_*adi 8

要解决这个问题,你只需要更改 pom.xml 中 spring-boot-maven-plugin 的配置。

\n\n

考虑到您有一个 spring boot 多模块项目,您的项目结构应该是这样的

\n\n
spring-boot-projcet  \n\xe2\x94\x9c\xe2\x94\x80service  \n\xe2\x94\x94\xe2\x94\x80webapp  \n
Run Code Online (Sandbox Code Playgroud)\n\n

我发现的最直接、最简单的例子是Burkhard Graves提供的项目
\n https://github.com/drahkrub/spring-boot-multi-module

\n\n

要解决您的问题,请在父模块中添加对 pom.xml 的更改

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

将 chnages 添加到 webapp 模块中的 pom.xml

\n\n
<build>\n    <plugins>\n        <plugin>\n            <groupId>org.springframework.boot</groupId>\n            <artifactId>spring-boot-maven-plugin</artifactId>\n            <configuration>\n                <skip>false</skip>\n            </configuration>\n        </plugin>\n    </plugins>\n</build> \n
Run Code Online (Sandbox Code Playgroud)\n\n

通过此设置,通常mvn clean spring-boot:run可以在项目的根目录中进行。

\n


Aka*_*hra 1

start-class您可以在 pom.xml 中使用属性或配置 xml 标记定义主类,在此处mainClass查找说明。