提问者:小点点

Spring Boot Maven多模块项目与配置文件-未找到来自其他模块的包


我目前正在尝试使用多个pom文件构建我的Spring Boot Maven应用程序。我有两个应用程序需要共享很多类。我有以下结构:

.
├── application1
|   |── src
│   └── pom.xml
|── application2
|   |── src
│   └── pom.xml
|── shared 
|   └── src
|   └── pom.xml
|
└── pom.xml

根目录中的pom如下所示:

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

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

    <groupId>nl.example.parent</groupId>
    <artifactId>example-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <!-- sub modules -->
    <modules>
        <module>application1</module>
        <module>application2</module>
        <module>shared</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <maven.plugins.version>2.22.1</maven.plugins.version>
    </properties>

    <dependencies>
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven.plugins.version}</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.plugins.version}</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>application1</id>
            <modules>
                <module>shared</module>
                <module>application1</module>
            </modules>
        </profile>
        <profile>
            <id>application2</id>
            <modules>
                <module>shared</module>
                <module>application2</module>
            </modules>
        </profile>
  </profiles>

</project>

应用程序1和应用程序2目录中的pom如下所示:

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

    <parent>
        <groupId>nl.example.parent</groupId>
        <artifactId>example-parent</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>nl.example.application1</groupId>
    <artifactId>example-child</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <dependencies>
        <!-- Is this the right way to import the shared module into application1 or application2? -->
        <dependency>
            <groupId>nl.example.shared</groupId>
            <artifactId>example-shared</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

然后我有一个共享模块,其中大多数类将在应用程序1和应用程序2之间共享。共享目录的pom如下所示:

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

    <parent>
        <groupId>nl.example.parent</groupId>
        <artifactId>example-parent</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>nl.example.shared</groupId>
    <artifactId>example-shared</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <dependencies>
    <!-- Shared dependencies here --> 
    </dependencies>

</project>

但是,当我尝试为两个应用程序之一构建特定配置文件时,我尝试从共享模块进行的每次导入都会出现错误。显然我做错了什么,但我无法弄清楚为什么从应用程序1调用共享文件夹中的类时无法访问?我确实将共享模块作为依赖项包含在应用程序1和应用程序2中


共1个答案

匿名用户

您的父POM具有Spring Boot作为父级。但这不起作用。您将无法获得模块中的依赖项。

您必须配置一个不继承自Spring Boot父级的项目,并将Spring Boot依赖项添加到依赖项管理中。

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-maven-without-a-parent