Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Maven集成测试不需要';在相同的包结构中找不到我的类_Java_Maven_Testing_Integration Testing - Fatal编程技术网

Java Maven集成测试不需要';在相同的包结构中找不到我的类

Java Maven集成测试不需要';在相同的包结构中找不到我的类,java,maven,testing,integration-testing,Java,Maven,Testing,Integration Testing,这是我的档案: 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&g

这是我的档案:


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>

当我运行
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
如果我将应用程序类放在
我的模块集成测试中的测试结构中,它就可以工作(上马)

有人能帮我吗?

注:操作系统模块和项目的名称可能是错误的,只是为了隐藏原始名称

GitHub:

特拉维斯:

--在github发布后更新

问题是在
mp schedule ws
模块的
pom.xml
中调用的重新打包。一旦您的pom.xml中包含了
spring boot maven plugin
,它将自动尝试重写归档文件,以使用
spring 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

如果您将SpringBootMaven插件放在注释中,您的构建就像一个 魅力

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

  • 使用为重新打包的可执行文件构建单独的jar
  • 有条件地将
    springbootmaven插件
    build执行绑定到特定的打包配置文件,以便它不会在集成测试配置文件中执行
  • 创建一个单独的模块,仅用于构建spring boot可执行jar
  • SpringBoot插件使用.jar.original扩展名保留了原始jar的备份,您可以使用maven插件复制它并将其添加到类路径[Shugh hack]
通过在
mp计划ws
pom.xml
中添加分类器的第一种策略的示例:

<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>
第二种策略的另一个示例是在
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>
为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
当然,您可以选择最适合您的项目的解决方案策略

--旧答案-过时,留作参考

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

<version>1.0-SNAPSHOT</version>

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

您确定您对包含应用程序类的模块具有适当的依赖性吗?@Lucas您使用的是哪个maven版本?我无法用maven 3.xYou正在使用的
my.artifact.ws
复制它,但在后面的树中,磁盘上的文件夹称为
my module ws
(带有破折号,而不是点)。是哪一个?请做出决定,否则是帮不上忙的。是否该类受包保护,而您无法访问它?您还需要发布
应用程序
,可能还需要发布
MyTest
。这里有太多的未知数。但底线是这应该是可行的,所以问题在于你没有展示的东西。假设您是从多模块项目的父POM构建的,就像您一直应该做的那样,我的钱花在
应用程序上
不是
公共的
。或者在某个地方有输入错误,因为您发布的POM不能是您真正的POM,因为它们甚至没有有效的XML语法。我无法用问题中提供的信息再现您的问题。但是这个链接可能会帮助你:。我在github存储库中创建了一个可能的多项目设置,其中包括单元/集成和systemtests,用于我们公司的一些讨论。请指定maven版本。你的设置文件有什么特别的地方吗?看看github的帖子谢谢你的github帖子,做得很好,我现在可以复制它,它显示了一些有价值的信息;我更新了我的答案,提出了一些解决问题的建议。创建一个单独的模块来构建Spring boot可执行文件是一个不错的主意。但是这里也使用了一种很好的方法,它可以用Classifier创建Spring Boot的可执行JAR,同时仍然像往常一样构建主工件。请不要手动将“.jar.original”添加到类路径中,这是一个非常丑陋的黑客行为……我也会使用分类器,我用你的反馈更新了我的答案;抄袭原文是邪恶的,我在写的时候感觉很脏。
$ 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
<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>
$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
$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
<version>1.0-SNAPSHOT</version>
<dependency>
    <groupId>my.group</groupId>
    <artifactId>my.artifact.ws</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>