如何在多模块Spring-boot Maven项目中构建特定模块

如何在多模块Spring-boot Maven项目中构建特定模块

问题描述:

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

I have create a multi module spring boot maven project.

但是当我使用

mvn clean package -pl module2 spring-boot:run

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

in console. It tell me that some class in module1 can not find.

但是我已经在module2中添加了依赖项. module2是最终项目.

But I have added the dependency in module2. The module2 is the final project.

项目结构如下.

父项目的pom.xml

parent project`s 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:

the pom in module1:

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

the pom.xml in module2:

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

您需要调整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)

Module pom (containing your 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 > skip