80.7 Create a non-executable JAR with exclusions
Often if you have an executable and a non-executable jar as build products, the executable version will have additional configuration files that are not needed in a library jar. E.g. the application.yml
configuration file might excluded from the non-executable JAR.
Here’s how to do that in Maven:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <classifier>exec</classifier> </configuration> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <id>exec</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <!-- Need this to ensure application.yml is excluded --> <forceCreation>true</forceCreation> <excludes> <exclude>application.yml</exclude> </excludes> </configuration> </execution> </executions> </plugin> </plugins> </build>
In Gradle you can create a new JAR archive with standard task DSL features, and then have the bootRepackage
task depend on that one using its withJarTask
property:
jar { baseName = 'spring-boot-sample-profile' version = '0.0.0' excludes = ['**/application.yml'] } task('execJar', type:Jar, dependsOn: 'jar') { baseName = 'spring-boot-sample-profile' version = '0.0.0' classifier = 'exec' from sourceSets.main.output } bootRepackage { withJarTask = tasks['execJar'] }