带有配置文件的 Spring Boot Maven 多模块项目 - 未找到来自其他模块的包 [英] Spring Boot Maven multi module project with profile - packages from other module not found

查看:44
本文介绍了带有配置文件的 Spring Boot Maven 多模块项目 - 未找到来自其他模块的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>.├──应用1||── src│ └── pom.xml|── 应用2||── src│ └── pom.xml|── 共享|└── src|└── pom.xml|└── pom.xml

根目录下的pom是这样的:

<modelVersion>4.0.0</modelVersion><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/></父母><groupId>nl.example.parent</groupId><artifactId>example-parent</artifactId><packaging>pom</packaging><version>1.0.0-SNAPSHOT</version><!-- 子模块--><模块><module>application1</module><module>application2</module><模块>共享</模块></模块><属性><java.version>1.8</java.version><maven.plugins.version>2.22.1</maven.plugins.version></属性><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></依赖></依赖项><构建><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><执行><执行><目标><目标>重新打包</目标></目标><配置><classifier>exec</classifier></配置></执行></执行></插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-failsafe-plugin</artifactId><version>${maven.plugins.version}</version><配置><useSystemClassLoader>false</useSystemClassLoader></配置></插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>${maven.plugins.version}</version><配置><useSystemClassLoader>false</useSystemClassLoader></配置></插件></plugins></build><个人资料><个人资料><id>application1</id><模块><模块>共享</模块><module>application1</module></模块></个人资料><个人资料><id>application2</id><模块><模块>共享</模块><module>application2</module></模块></个人资料></个人资料></项目>

application1 和 application2 目录中的 pom 如下所示:

<modelVersion>4.0.0</modelVersion><父母><groupId>nl.example.parent</groupId><artifactId>example-parent</artifactId><version>1.0</version></父母><groupId>nl.example.application1</groupId><artifactId>example-child</artifactId><包装>罐</包装><version>1.0</version><依赖项><!-- 这是将共享模块导入 application1 或 application2 的正确方法吗?--><依赖><groupId>nl.example.shared</groupId><artifactId>example-shared</artifactId><version>1.0</version></依赖></依赖项></项目>

然后我有一个共享模块,其中大多数类将在 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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><父母><groupId>nl.example.parent</groupId><artifactId>example-parent</artifactId><version>1.0</version></父母><groupId>nl.example.shared</groupId><artifactId>example-shared</artifactId><包装>罐</包装><version>1.0</version><依赖项><!-- 此处共享依赖项--></依赖项></项目>

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

解决方案

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

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

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

I'm currently try to structure my Spring Boot Maven application with multiple pom files. I have two applications which needs to share a lot of classes. I have the following structure:

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

The pom in the root directory looks like this:

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

The pom in both the application1 and application2 directory look like this:

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

Then I have a shared module of which most classes will be shared between application1 and application2. The pom of the shared directory looks like this:

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

However, when I try to build the specific profile for one of the two applications I get errors for every import that I try to make from the shared module. Obviously I'm doing something wrong but I can't figure out why the classes from the shared folder are not reachable when called from application1? I did include the shared module as dependency in both application1 and application2

解决方案

Your Parent POM has Spring Boot as parent. But this does not work. You will not get the dependencies in your modules.

You have to configure a project that does not inherit from Spring Boot Parent and add Spring Boot dependencies to dependency management.

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

这篇关于带有配置文件的 Spring Boot Maven 多模块项目 - 未找到来自其他模块的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆