Sunday, November 6, 2016

File validation in Maven

Hi again after a long period of inactivity! Today's objective is to validate the files that are generated by an external system in JSON format. The files are stored under the umbrella of Maven submodule with packaging type set to POM. The external system is not perfect and sometimes it produces wrong JSONs. The application relies on these files and it is important to detect any problems as fast as possible - fail fast - by preventing a build to be successful if there is anything wrong in JSON.
The structure of Maven submodule is as follows:
    parent
         .
         .
         configuration-submodule
         |-- pom.xml -> POM packaging
         `-- configuration
             |-- first.json
             |-- second.json 
The 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 -> pom
         |-- configuration
         |    |-- first.json
         |    |-- second.json       
         `--src
              |
              `--test
                    |
                    `--java
                          |
                          `-- ConfigurationTest.java
 
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.:
package
install
deploy
The 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