【问题标题】:Spring Boot Maven multi module project with profile - packages from other module not found带有配置文件的 Spring Boot Maven 多模块项目 - 未找到来自其他模块的包
【发布时间】:2019-09-19 22:13:45
【问题描述】:

我目前正在尝试使用多个 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>

application1 和 application2 目录中的 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>

然后我有一个共享模块,其中大多数类将在 application1 和 application2 之间共享。共享目录的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>

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

【问题讨论】:

    标签: java spring maven spring-boot


    【解决方案1】:

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

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

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

    【讨论】:

    • 我也有同样的问题。 @Simon Martinelli 你能分享这个解决方案的 GitHub 存储库吗,这对​​我很有帮助。
    • 在没有父 POM github 示例的情况下使用 Spring Boot 只需 google,您会发现 github.com/subhashlamba/spring-examples/tree/master/…
    猜你喜欢
    • 1970-01-01
    • 2016-04-28
    • 2017-05-31
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多