【问题标题】:Maven, Spring Boot - package depandancie issues, integration testsMaven,Spring Boot - 包依赖问题,集成测试
【发布时间】:2018-11-24 02:07:24
【问题描述】:

我有一个 ma​​ven 项目,其结构如下

- Root pom.xml
    - module1
    - module2

module2 是一个使用 Spring Boot 注解的集成测试模块 -

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Config.class, webEnvironment = RANDOM_PORT)

module2 依赖于 module1,当项目使用 spring-boot-maven-plugin 构建时,它会被重新打包并@ module1 中的 987654321@; This can be solved using the classifier as follows

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

现在的问题是,当运行 module2 集成测试时,无法再找到主类,我猜这是因为 module2 现在正在使用原始工件并且不是重新包装的。

目标的执行默认值 org.springframework.boot:spring-boot-maven-plugin4.6.18.RELEASE:repackag e failed: 找不到主类

当涉及到 Spring Boot 和集成测试时,如何解决这个问题以及项目结构的最佳实践是什么?

【问题讨论】:

    标签: java spring maven spring-boot integration-testing


    【解决方案1】:

    我使用 Maven 配置文件解决了这个问题。在我定义的根 pom 中:

    <profiles>
        <profile>
            <id>skip-spring-repackaging</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>spring.repackage.skip</name>
                </property>
            </activation>
            <modules>
                <module>integration-tests</module>
            </modules>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                            <version>${spring-boot.version}</version>
                            <configuration>
                                <attach>false</attach>
                            </configuration>
                            <executions>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
        <profile>
            <id>spring-repackaging</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                            <version>${spring-boot.version}</version>
                            <configuration>
                                <attach>true</attach>
                            </configuration>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>repackage</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
    </profiles>
    

    在应用程序 pom 中:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    

    默认情况下,重新打包将运行。集成测试使用单独的 maven 命令运行,我通过 -Dspring.repackage.skip

    这有一个缺点,它只适用于单独的 Maven 调用。可能有我不知道的更清洁的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 2017-06-05
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多