Maven集成测试在相同的包结构中找不到我的类

Luc*_*cas 3 java testing integration-testing maven

这是我的文件:


Pom父母:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.group</groupId>
    <artifactId>my.artifact.pom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>my.artifact.ws</module>
    </modules>

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

    <properties>
        <!-- test -->
        <maven.test.failure.ignore>false</maven.test.failure.ignore>
    </properties>

    //lot of dependencies...

    <!-- PROFILES -->
    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
                <unit-tests.skip>false</unit-tests.skip>
                <integration-tests.skip>true</integration-tests.skip>
            </properties>
        </profile>
        <profile>
            <id>integration</id>
            <modules>
                <module>my-module-integration-test</module>
            </modules>
            <properties>
                <unit-tests.skip>true</unit-tests.skip>
                <integration-tests.skip>false</integration-tests.skip>
            </properties>
        </profile>
    </profiles>

    <!-- PLUGIN -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${encoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

Module-ws pom:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>my.artifact.ws</artifactId>
    <packaging>jar</packaging>
    <parent>
        <groupId>my.group</groupId>
        <artifactId>my.artifact.pom</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    //lot of dependencies...

</project>
Run Code Online (Sandbox Code Playgroud)

集成测试pom:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>my.artifact.integration.test</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>my.group</groupId>
        <artifactId>my.artifact.pom</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <properties>
        <unit-tests.skip>false</unit-tests.skip>
        <integration-tests.skip>true</integration-tests.skip>
    </properties>

    <dependencies>
        <dependency>
            <groupId>my.group</groupId>
            <artifactId>my.artifact.ws</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构:

my-module
|-- my-module-integration-test
|   `-- src
|       `-- test
|           `-- java
|               `-- my
|                   `-- module
|                       `-- ws
|                           `-- rest
|                               `-- MyTest
`-- my-module-ws
    `-- src
        `-- main
            `-- java
                `-- my
                    `-- module
                        `-- Application
Run Code Online (Sandbox Code Playgroud)

当我跑步时,mvn clean install -P integration我收到消息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project mp-schedule-integration-test: Compilation failure: Compilation failure:
[ERROR] /Users/me/dev/my-module/my-module-integration-test/src/test/java/my/module/ws/rest/MyTest.java:[3,28] cannot find symbol
[ERROR] symbol:   class Application
[ERROR] location: package my.module
Run Code Online (Sandbox Code Playgroud)

如果我把Application类放在测试结构里面my-module-integration-test就可以了(Go Horse)

有人能帮助我吗?

Ps:名称os模块和项目可能是错误的只是为了隐藏原始名称.

GitHub:https://github.com/LucasHCruz/stack40664101

特拉维斯:https://travis-ci.org/LucasHCruz/stack40664101

Nic*_*ven 11

- github发布后更新

问题是在模块中调用的重新打包spring-boot-maven-plugin.一旦包含在您的pom.xml中,它将自动尝试重写存档以使其可以使用目标执行.pom.xmlmp-schedule-wsspring-boot-maven-pluginspring-boot:repackage

由于mp-integration-test包中的依赖性,mp-schedule-ws/target/mp-schedule-ws-1.0-SNAPSHOT.jar实际上将在类路径上,但是如果你看内部,你会看到它加载org/springframework/boot/loader/*类,你的类将驻留在BOOT-INF文件夹中,例如BOOT-INF/classes/com/cnova/mpschedule/Application.class.

如果你把spring-boot-maven-plugin放在评论中,那么你的构建就像一个魅力.

要解决此问题,您可以遵循以下策略:

  • 使用分类器为重新打包的可执行文件构建一个单独的jar
  • 有条件地将spring-boot-maven-plugin构建执行绑定到特定的打包配置文件,以便它不会在集成测试配置文件中执行
  • 创建一个单独的模块,仅用于构建spring boot可执行jar
  • spring boot插件使用.jar.original扩展保留原始jar的备份,你可以使用maven插件复制它并将其添加到类路径[丑陋的黑客]

通过在以下内容pom.xml中添加分类器的第一个策略的示例mp-schedule-ws:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <classifier>exec</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

这将给你2个罐子:

$ ls -al mp-schedule-ws/target/
-rwxr--r--  1 nick  wheel  55188756 Nov 23 06:38 mp-schedule-ws-1.0-SNAPSHOT-exec.jar
-rw-r--r--  1 nick  wheel     20311 Nov 23 06:38 mp-schedule-ws-1.0-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

第二个策略的另一个示例是在mp-schedule-ws模块中定义特定的构建配置文件,例如:

<profiles>
    <profile>
        <id>package-application</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <executable>true</executable>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

这给出了:

$apache-maven-3.3.9/bin/mvn clean install -P integration

[INFO] mp-schedule ........................................ SUCCESS [  0.230 s]
[INFO] mp-schedule-core ................................... SUCCESS [  3.845 s]
[INFO] mp-schedule-ws ..................................... SUCCESS [  0.563 s]
[INFO] mp-schedule-integration-test ....................... SUCCESS [  0.721 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.751 s
Run Code Online (Sandbox Code Playgroud)

并为Spring Boot构建可执行jar:

$apache-maven-3.3.9/bin/mvn clean install -P package-application

[INFO] mp-schedule ........................................ SUCCESS [  0.255 s]
[INFO] mp-schedule-core ................................... SUCCESS [  3.822 s]
[INFO] mp-schedule-ws ..................................... SUCCESS [  0.968 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.396 s
Run Code Online (Sandbox Code Playgroud)

当然,您可以选择最适合您项目的解决方案策略.

- 旧答案 - 过时,保留供参考

它在maven 3.x上工作,我唯一需要改变的是添加

<version>1.0-SNAPSHOT</version>
Run Code Online (Sandbox Code Playgroud)

my.artifact.ws,因为你声明的依赖在my.artifact.integration.test

<dependency>
    <groupId>my.group</groupId>
    <artifactId>my.artifact.ws</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我还在<properties>本地配置文件中添加了一个缺少的开始标记.