Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven integration-test doesn't find my class in same package structure

Here are my files:


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 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 mvn clean install -P integration I receive the 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 inside test structure in my-module-integration-test it works (Go Horse)

Could someone help me?

Ps.: The name os modules and projects could be wrong just for hide the original names.

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

Travis: https://travis-ci.org/LucasHCruz/stack40664101

like image 446
Lucas Avatar asked Dec 14 '22 02:12

Lucas


1 Answers

-- Update after github post

The problem is the repackaging spring-boot-maven-plugin invoked in the pom.xml of the mp-schedule-ws module. Once spring-boot-maven-plugin has been included in your pom.xml it will automatically attempt to rewrite archives to make them executable using the spring-boot:repackage goal.

Due to the dependency in the mp-integration-test package, the mp-schedule-ws/target/mp-schedule-ws-1.0-SNAPSHOT.jar will actually be on the classpath, but if you look at the internals you will see it loads org/springframework/boot/loader/* classes and that your classes will reside in the BOOT-INF folder, such as BOOT-INF/classes/com/cnova/mpschedule/Application.class.

If you put the spring-boot-maven-plugin in comment, your build works like a charm.

To solve this, there are some strategies you could follow:

  • use a classifier to build a seperate jar for the repackaged executable
  • conditionally bind the spring-boot-maven-plugin build execution to a specific packaging profile, so that it is not executed in the integration test profile
  • create a seperate module only for building your spring boot executable jar
  • the spring boot plugin keeps a backup of the original jar with the .jar.original extension, you could copy that using a maven plugin and add it to the classpath [ugly hack]

An example of the first strategy by adding the classifier in the pom.xml of the 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>

This will give you 2 jars:

$ 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 profile in the mp-schedule-ws module, such as:

<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 to build the 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 fits your project best.

-- old answer - obsolete, kept for reference

It works here on maven 3.x, the only thing I had to change was to add the

<version>1.0-SNAPSHOT</version>

to the my.artifact.ws since you declared the dependency in my.artifact.integration.test

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

I also added a missing <properties> start tag in the local profile.

like image 90
Nick Vanderhoven Avatar answered Jan 23 '23 05:01

Nick Vanderhoven