The structure of Maven submodule is as follows:
parent . . configuration-submodule |-- pom.xml -> POM packaging `-- configuration |-- first.json |-- second.jsonThe goal is to validate JSON files. If they are incorrect then the build should fail. I decided to place the validation code within a test. The structure was as follows:
parent . . configuration-submodule |-- pom.xml ->The surprise is that ConfigurationTest.java was not executed after invoking mvn clean install, well - it was not even compiled! The reason for that is the fact the POM packaging has a very simplistic lifecycle during building process: click -> Default Lifecycle Bindings - Packaging pom.:pom |-- configuration | |-- first.json | |-- second.json `--src | `--test | `--java | `-- ConfigurationTest.java
package install deployThe first thing I had to do was to make sure ConfigurationTest was executed. This is why I attached testCompile and test goals to package. The modification of submodule's POM was necessary:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
That was enough to execute my test. However, I encountered one more problem, namely I was not able to access JSON files through a relative path. configuration directory was not on the classpath during test execution. The following addition to maven-surefire-plugin did the trick:
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>${project.basedir}</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
Thanks to that the root submodule directory - configuration-submodule - was on the classpath and I was able to access configuration directory in the following way:
ConfigurationTest.class.getClassLoader().getResource("configuration");That's the end of my short journey. Hopefully you find it interesting!
No comments :
Post a Comment