Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Maven Plugin Include Resources

Example

Is it possible to somehow configure the spring boot maven plugin to include resources from a dependency.

E.g. if in my spring boot project I have:

  <dependency>
    <groupId>co.company</groupId>
    <artifactId>example</artifactId>
  </dependency>

and in that JAR file there is a properties file such as example.properties

jar -tf example.jar | grep example.properties | wc -l

results in 1.

However, when I build the spring boot JAR, this properties file is not added to the src/main/resources. The JAR containing it is included in BOOT-INF/lib/example.jar

However, in my case. I'd like to extract the content of src/main/resources out into the boot BOOT-INF/classes/ directory of the spring boot JAR so that things like auto configuration can pick it up.

Real World

In the real world, i'm trying to do this with:

  • thymeleaf templates (e.g. my dependency JAR provides the HTML template files but in the deployed boot jar these templates are not resolved)
  • liquibase changelog files (my dependency includes changelog files but these aren't executed - I presume liquibase autoconfig doesn't find the changelog file because it's not in the src/main/resources of the boot JAR).
like image 367
David Avatar asked Feb 05 '23 01:02

David


1 Answers

I think the solution for this problem would be very similar to the solution for another question you asked.

You can use the unpack goal of the maven-dependency-plugin in your Spring Boot module:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>module-a</artifactId>
                        <version>${project.version}</version>
                        <includes>**/*.yaml</includes>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>${project.build.directory}/classes/BOOT-INF/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

That will copy the resources from module-a to BOOT-INF directory of your boot-module. I've posted a more complete example on GitHub.

like image 149
jjlharrison Avatar answered Feb 11 '23 18:02

jjlharrison