understanding Spring Boot Maven Plugin in Multi-Module projects - spring-boot

I'm learning about Spring Boot multi module projects and I try to understand what exactly does spring boot maven plugin.
For example I have 2 different projects. In the first project I have the spring boot maven plugin in the web module:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.4.RELEASE</version>
<configuration>
<mainClass>dgs.web.DemoApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And in the second project I have this plugin in the data module:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And this is in the parent pom of the 2nd project:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Can somebody explain me what exaclty does this spring boot maven plugin especially in the second project? And why there is no reference about mainClass. I see this mainClass tag only in the first project.

The maven plugin is for packaging and executing via Maven.
You should read the docs:
https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html
https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/maven-plugin/

Related

how to include static resource from a dependency in spring boot fat jar at the time of build

I have a spring boot application whose UI separately is built and added as a dependency to my application. Now I want to unpack the dependency and add to the resource folder of the spring boot application at the time of build so that it becomes a part of fat jar. Could someone guide me as to how this can be done with spring-boot-maven-plugin.
note: the project is using maven for build
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>nflow-explorer</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>io.nflow</includeGroupIds>
<includeArtifactIds>nflow-explorer</includeArtifactIds>
<outputDirectory>
${project.build.directory}/resources/main/static/explorer
<!-- or: ${project.basedir}/wherever/you/want/it -->
</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You don't have to unpack the JAR.
Simply use the Maven Resource Plugin http://maven.apache.org/plugins/maven-resources-plugin/index.html
You can specify directories to include like this:
<project>
...
<build>
...
<resources>
<resource>
<directory>[your folder here]</directory>
</resource>
</resources>
...
</build>
...
</project>

pluginManagement does not work

This is the pluginManagement in the parent POM, and it can be executed by child project that no plugins defined in child project, why??
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
But if I remove this plugin from parant POM then the child project can't be executed.
In your parent POM, you need to have something like:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Then in your project that extends this POM, you just need to do:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
The idea here is that you don't need to have the same code copied all over the place. You define the settings in one central place and then just extend them.
In your original code, you didn't have the <version/> defined. Usually, in parent POM-s you'd like to control the versions of <dependencies/> and <plugins/>. For dependencies you can also define things like <exclusions/> and <scope/>.
For plugins, you could have the same configuration defined centrally and then, if a case arises where you'd like it to be slightly different, you can just apply the necessary changes in the extending POM.
If in your parent POM you also have a definition of <plugins/> outside the <pluginManagement/> section, then those plugins will be invoked for any project extending this parent.

convert maven plugin into gradle plugin

I'm planning to use aspects-jcabi for benchmarking my methods (http://aspects.jcabi.com/annotation-loggable.html). However, it uses maven-plugin, and my project is on gradle. I'm not yet that familiar with Gradle though.
Can I write the following in gradle (http://aspects.jcabi.com/example-weaving.html)?
<project>
<build>
<plugins>
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.8</version>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Building Spring Boot Application - Maven Errors

I've been trying to package my spring-boot application into a war file but the errors keep coming, I've solved 3 so far but I'm stuck with this one.
Error
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage
(default) on project pns: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage failed:
A required class was missing while executing org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage:
org/springframework/boot/loader/tools/LaunchScript
App Properties
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>iq.pns.PushNotificationServerApplication</start-class>
</properties>
Build Config
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
<layout>WAR</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</build>
Any ideas why this is happening?
Thanks in advance.
Turns out to be a corrupted jar in the Maven cache, just like #AndyWilkinson suggested.
Solved by deleting the ~/.m2/repository folderand letting Maven re-download everything.

Using bootRepackage=false in Maven

Does anybody know what is the Gradle bootRepackage=false equivalent in Maven? How can you configure spring boot plugin to not generate boot war?
The problem that I face is that I have a multi module project. When I build the project with mvn clean install, the module jar contain the entire libraries defined in its pom.
The solution above applies to older versions. Spring-boot maven plugin 1.2 introduced:
<properties>
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties>
Skip the execution. Default value is: false. User property is: spring-boot.repackage.skip.
https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/#goals-repackage
and
https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/#goals-repackage-parameters-details-skip
You can skip the repackage goal from being executed by setting the skip attribute to true:
Skip the execution. Default: false.
In your plugin configuration, you can then have:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip><!-- true or the result of a Maven/system property for example --></skip>
</configuration>
</execution>
</executions>
</plugin>
It`s works for me
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources