Java – Maven integration test cannot find my class in the same package structure
This is my file:
POM parents:
<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 folder structure:
my-module |-- my-module-integration-test | `-- src | `-- test | `-- java | `-- my | `-- module | `-- ws | `-- rest | `-- MyTest `-- my-module-ws `-- src `-- main `-- java `-- my `-- module `-- Application
When I run the MVN clean install - P integration, I receive a message:
[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
If I put the application class in the test structure in my module integration test, it can work (go horse)
Can anyone help me?
PS: the name OS module and project may be wrong just to hide the original name
GitHub: https://github.com/LucasHCruz/stack40664101
Travis: https://travis-ci.org/LucasHCruz/stack40664101
Solution
– updated after GitHub release
The problem is in the POM of the MP schedule WS module Repackage spring-boot-maven-plugin. called in XML Once the spring boot Maven plugin is included in your POM XML, it will automatically try to rewrite the files and make them executable using the spring boot: repackage target
Due to the dependency in the MP integration test package, MP schedule WS / target / MP schedule ws-1.0-snapshot Jar will actually be on the classpath, but if you look inside, you will see that it loads org / springframework / boot / loader / * classes, and your classes will reside in the boot-inf folder, such as boot-inf / classes / COM / cnova / mpschedule / application class.
To solve this problem, you can follow the following strategies:
>Use the classifier to build a separate jar for the repackaged executable > conditionally bind the spring boot Maven plugin build execution to a specific packaging configuration file so that it will not execute in the integration test configuration file > create a separate module, which is only used to build the spring boot executable jar > spring boot plug-in jar. The original extension keeps a backup of the original jar, which you can copy using the Maven plug-in and add to the classpath [ugly hacker]
Through POM. In MP schedule WS First policy example of adding a classifier to 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>
This will give you 2 cans:
$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
Another example of the second strategy is to define a specific build configuration file in the MP schedule WS module, for example:
<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>
This gives:
$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
And build an executable jar for spring boot:
$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
Of course, you can choose the solution strategy that best suits your project
– old answer – obsolete, reserved for reference
It's in Maven 3 Working on X, the only thing I need to change is to add
<version>1.0-SNAPSHOT</version>
Because you're in my artifact. integration. The dependency is declared in test, so my artifact. ws
<dependency> <groupId>my.group</groupId> <artifactId>my.artifact.ws</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
I also added a missing < Properties > startup tag in the local configuration file