Maven integration-test 在相同的包结构中找不到我的 class
Maven integration-test doesn't find my class in same package structure
这是我的文件:
Pom parent:
<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>
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>
Integration-test 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>
我的文件夹结构:
my-module
|-- my-module-integration-test
| `-- src
| `-- test
| `-- java
| `-- my
| `-- module
| `-- ws
| `-- rest
| `-- MyTest
`-- my-module-ws
`-- src
`-- main
`-- java
`-- my
`-- module
`-- Application
当我 运行 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
如果我将应用程序 class 放入 my-module-integration-test
中的测试结构中,它就可以工作(Go Horse)
有人可以帮我吗?
Ps.: 名称os模块和项目可能是错误的,只是为了隐藏原始名称。
-- 更新后 github post
问题 是在 mp-schedule-ws
模块的 pom.xml
中调用的重新打包 spring-boot-maven-plugin。一旦 spring-boot-maven-plugin
包含在您的 pom.xml 中,它会自动尝试重写档案以使用 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
。
If you put the spring-boot-maven-plugin in comment, your build works like a
charm.
要解决此问题,您可以遵循一些策略:
- 使用 classifier 为重新打包的可执行文件构建一个单独的 jar
- 有条件地将
spring-boot-maven-plugin
构建执行绑定到特定的打包配置文件,这样它就不会在集成测试配置文件中执行
- 创建一个单独的模块,仅用于构建您的 spring 引导可执行文件 jar
- spring 启动插件保留了带有 .jar.original 扩展名的原始 jar 的备份,您可以使用 maven 插件复制它并将其添加到类路径 [丑陋的黑客]
一个例子的第一个策略是在mp-schedule-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>
这会给你 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
第二种策略的另一个例子是在mp-schedule-ws
模块中定义一个特定的build profile,例如:
<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
并为 Spring 引导构建可执行 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>
到 my.artifact.ws
因为你在 my.artifact.integration.test
中声明了依赖关系
<dependency>
<groupId>my.group</groupId>
<artifactId>my.artifact.ws</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
我还在本地配置文件中添加了一个缺失的 <properties>
开始标记。
这是我的文件:
Pom parent:
<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>
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>
Integration-test 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>
我的文件夹结构:
my-module
|-- my-module-integration-test
| `-- src
| `-- test
| `-- java
| `-- my
| `-- module
| `-- ws
| `-- rest
| `-- MyTest
`-- my-module-ws
`-- src
`-- main
`-- java
`-- my
`-- module
`-- Application
当我 运行 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
如果我将应用程序 class 放入 my-module-integration-test
中的测试结构中,它就可以工作(Go Horse)
有人可以帮我吗?
Ps.: 名称os模块和项目可能是错误的,只是为了隐藏原始名称。
-- 更新后 github post
问题 是在 mp-schedule-ws
模块的 pom.xml
中调用的重新打包 spring-boot-maven-plugin。一旦 spring-boot-maven-plugin
包含在您的 pom.xml 中,它会自动尝试重写档案以使用 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
。
If you put the spring-boot-maven-plugin in comment, your build works like a charm.
要解决此问题,您可以遵循一些策略:
- 使用 classifier 为重新打包的可执行文件构建一个单独的 jar
- 有条件地将
spring-boot-maven-plugin
构建执行绑定到特定的打包配置文件,这样它就不会在集成测试配置文件中执行 - 创建一个单独的模块,仅用于构建您的 spring 引导可执行文件 jar
- spring 启动插件保留了带有 .jar.original 扩展名的原始 jar 的备份,您可以使用 maven 插件复制它并将其添加到类路径 [丑陋的黑客]
一个例子的第一个策略是在mp-schedule-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>
这会给你 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
第二种策略的另一个例子是在mp-schedule-ws
模块中定义一个特定的build profile,例如:
<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
并为 Spring 引导构建可执行 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>
到 my.artifact.ws
因为你在 my.artifact.integration.test
<dependency>
<groupId>my.group</groupId>
<artifactId>my.artifact.ws</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
我还在本地配置文件中添加了一个缺失的 <properties>
开始标记。