【问题标题】:How to Build specific module in a multi module spring-boot maven project如何在多模块 spring-boot maven 项目中构建特定模块
【发布时间】:2018-09-17 19:20:04
【问题描述】:

我已经创建了一个多模块 spring boot maven 项目。

但是当我使用

mvn clean package -pl module2 spring-boot:run

在控制台中。它告诉我 module1 中的某些类找不到。

但是我在module2中添加了依赖。 module2 是最终的项目。

项目结构如下。

父项目的 pom.xml

<groupId>com.example</groupId>
<artifactId>multi-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>module1</module>
    <module>module2</module>

</modules>

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

<properties>
    ...
</properties>

</dependencies>
    ...
<dependencyManagement>
    ...
</dependencyManagement>

module1中的pom:

<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
    <groupId>com.example</groupId>
    <artifactId>multi-module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

module2中的pom.xml:

<artifactId>personalinfo</artifactId>
<name>personalinfo</name>
<url>http://maven.apache.org</url>

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

<parent>
    <groupId>com.example</groupId>
    <artifactId>multi-module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>module1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </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>

【问题讨论】:

  • 你能显示错误吗?
  • 您是否先从 root 完成了 mvn install
  • 当我在根目录中使用 mvn clean package 时。它运作良好。但我想知道如何使用“mvn clean package -pl [targetModule] spring-boot:run”来运行spring boot模块。
  • 报错如下:[ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /src/main/java/com/example/dao/ClassInModule1.java:[12,29] package com.package.in.module1 does not exists

标签: maven spring-boot


【解决方案1】:

你需要调整你的pom

父pom:

   <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

模块 pom(包含您的 Main)

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>Your mainClass</mainClass>
                <fork>true</fork>
                <skip>false</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

强调Configuration &gt; skip

【讨论】:

  • 嗯,谢谢。我试过了,但没有生效。
  • @bitscanbyte 是对的,它应该这样工作 - 看看我创建的简单演示项目:github.com/drahkrub/spring-boot-multi-module
  • 是的,你是对的。重要性是```` true````
【解决方案2】:

@bitscanbyte 的回答是正确的,但配置可以简化:

父 pom

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

Module pom(@SpringBootApplication所在的模块)

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

一个简单的演示项目可以在这里找到:https://github.com/drahkrub/spring-boot-multi-module

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 2015-10-10
    • 2014-06-02
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多