3

I have a maven project with the following structure

- Root pom.xml
    - module1
    - module2

module2 is an integration test module using Spring Boot annotations -

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Config.class, webEnvironment = RANDOM_PORT)

module2 has a dependancy on module1, when the project is built using the spring-boot-maven-plugin it is repackaged and module2 cannot find packages in module1; This can be solved using the classifier as follows

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>

The problem now is that when running module2 integration tests the main class can no longer be found, I am guessing this is because module2 is now using the original artifact and not the repackaged one.

Execution default of goal org.springframework.boot:spring-boot-maven-plugin4.6.18.RELEASE:repackag e failed: Unable to find main class

How can this be solved and what is best practice to project strucure when it comes spring boot and integration tests?

clD
  • 2,523
  • 2
  • 22
  • 38

1 Answers1

0

I solved this with maven profiles. In the root pom I define:

<profiles>
    <profile>
        <id>skip-spring-repackaging</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <property>
                <name>spring.repackage.skip</name>
            </property>
        </activation>
        <modules>
            <module>integration-tests</module>
        </modules>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${spring-boot.version}</version>
                        <configuration>
                            <attach>false</attach>
                        </configuration>
                        <executions>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
    <profile>
        <id>spring-repackaging</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${spring-boot.version}</version>
                        <configuration>
                            <attach>true</attach>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>

And in the application pom simply:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

By default the repackaging will run. The integration tests are run with a separate maven command where I pass -Dspring.repackage.skip.

This has the disadvantage that it only works with separate Maven invocations. Probably there are cleaner solutions possible that I am unaware of.

MatKuhr
  • 505
  • 4
  • 13