Spring Boot – Host JaCoCo coverage as static HTML

Tags: , , ,



I am working with Spring Boot 2.2.5 and Java 8.

I have a Spring Boot webservice that I deploy and run as a linux service using the embedded Tomcat in Spring Boot.

During my Maven build, I generate code coverage metrics using the JaCoCo Maven plugin, and I would like to package and host these static HTML pages when I deploy to my server.

The output for these files is target/site/jacoco/index.html.

I know that you can deploy and host webpages through Spring Boot, but I have never accomplished it, and everything I lookup online seems to be more complicated than what im actually trying to do. The only thing i seem to have gathered so far, is that it need to get the html into my /resources directory.

Does anyone know how I can package all of the JaCoCo generated html pages into my .jar file, and host it on my webserver so that I can access it in a similar fashion to how I access any other API in the app?

I build and deploy the app with Jenkins. So if there is some nifty Jenkins way of doing it through my Jenkins.groovy script, that would be nice too.

I would like to be able to access something like: localhost:8080/my-app-context/coverage

Answer

Well, after some more digging and the right google questions, the solution was simpler than I thought. I stumbled across this article from Baeldung.

The goal:

  • get target/site/jacoco into the src/main/resources/static directory
  • get target/apidocs into the src/main/resources/static directory

The challenge:

  • Do it during a Maven/Jenkins build only.

The solution:

  • Use a Maven plugin to move the files after successful build
<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>copy-javadocs</id>
            <phase>install</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/src/main/resources/static/apidocs</outputDirectory>   <!-- output directory -->
                <resources>
                    <resource>
                        <directory>${basedir}/target/apidocs</directory>         <!-- source directory -->
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
        <execution>
            <id>copy-jacoco</id>
            <phase>install</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/src/main/resources/static/jacoco</outputDirectory>   <!-- output directory -->
                <resources>
                    <resource>
                        <directory>${basedir}/target/site/jacoco</directory>         <!-- source directory -->
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

After putting the above code in my pom.xml, once the app is deployed to my server, both my JaCoCo coverage and my JavaDoc static html pages can be accessed at:

  • <context-root>/apidocs/index.html
  • <context-root>/jacoco/index.html

I hope this simplifies it for others looking to do the same.



Source: stackoverflow