"mvn spring-boot:run": "/health" endpoint with project version - spring-boot

I'd like to use Spring Boot's actuator endpoint /info to show project metadata such as the Maven-provided project version.
To do so, I followed the appropriate part in Spring Boot's documentation.
The shown solution works for me when my Spring Boot application starts up with java -jar [...].
Just as the documentation says, it doesn't work when the application starts up with mvn spring-boot:run — in that case, my /info endpoint reports "version":"#project.version#".
The documentation mentions that one has to properly configure Spring Boot's Maven plugin, but I could not find any information about doing so.
How can I configure my Maven project in order to have a mvn spring-boot:run-started Spring Boot application to show project metadata?

Configure the spring-boot-maven-plugin with <addResources>false</addResources>.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<addResources>false</addResources>
</configuration>
</plugin>
Source

Related

Spring cloud Contract verifier baseClassMapping no longer working in 3.0.0

The below config works fine perfectly in spring-cloud-starter-contract-verifier 2.2.4.RELEASE. But with 3.0.0 it throws the error below, it is not deprecated in the docs. Does anyone know if the behavior is changed ?
.pom file
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<baseClassForTests>com.mycompany.selectservice.integrationtest.</baseClassForTests>
<baseClassMappings>
<baseClassMapping>
<contractPackageRegex>.*select.*</contractPackageRegex>
<baseClassFQN>com.mycompany.selectservice.integrationtest.SelectBaseTest</baseClassFQN>
</baseClassMapping>
</baseClassMappings>
</configuration>
</plugin>
SelectTest.validate_contract_0:33 » IllegalState You haven't configured a MockMvc.
My spring boot version is 2.2.4.RELEASE, So it means that spring-cloud-starter-contract-verifier 3.0.0 cannot be used with spring boot 2.2.4.release?
As described in our project page (https://spring.io/projects/spring-cloud) you have to use Boot 2.4 to use Spring Cloud Contract. Also, that functionality does work - most likely you're using some old imports or you're using JUnit4 (the default now is JUnit5).

Best way to run spring boot 2 rest services

I am writing an app that exposes some functions via REST service.
To do this I am using Spring Boot 2, but what is the best way to put it on production environment?
Is a good idea run the jar using java?
Short answer
yes it is a good idea.
Long answer
Spring Boot features a plugin that prepends a service script (Unix-compatible) in the JAR file itself. That makes the JAR file executable in Unix/Linux environments and you can easily install it as a service. Excerpt from https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html follows:
To create a "fully executable" jar with Maven, use the following plugin configuration:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
If packaging for Windows, the startup script isn't of much use and can be omitted. You would need to run using java -jar ... on windows, or install a service wrapper. Another excerpt from the Spring Boot doco:
A Spring Boot application can be started as a Windows service by using winsw.
A (separately maintained sample) describes step-by-step how you can create a Windows service for your Spring Boot application.
You can make it fully executable with below code in your pom.xml. You can run with shell script or as systemv or initd service[Spring Boot DOC]. This is the best tutorial link I have found explaining the multiple run as service options. You might want to take a look at spring doc for production ready features.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>

Utility Jar using Spring Boot

Created a custom jar using spring boot and this is using for CRUD operations on a database table.The purpose is used to make this a utility jar, so that other services or applications can use this jar for any operations on that table. Following are the steps I followed:
1). Added this jar entry in pom.xml of a REST SERVICE and build got successfully.
2). Autowired the service class of Utility jar inside the controller of REST SERVICE.
But when I started the REST SERVICE (service is developed on spring boot), I got the error as 'the ****controller can require a bean of type *****serviceUtility. Consider defining a bean of type in your configuration'. But I am not able to see any configuration class inside the rest service and it is using application.yml for datasource related things. I am new to Spring and Spring Boot. Could any one guide me how to configure the utility jar in external services.
It is hard to tell without source code, but Spring Boot jars do not by default work as utility jars. The packaging is different. To get a Spring Boot jar to work as a utility jar within another project, you'll want to configure the build plugin like this :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
Which is described here:
https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html
If you are using spring boot , by default , the utility jar will not move all its dependencies .
You can add all the dependencies in the pom.xml for the Rest service .
Else you end up packaging some core modules twice increasing the size of your jar .

TypeNotPresentExceptionProxy error at integration-test with maven-failsafe-plugin spring-boot 1.4

I'm getting ArrayStoreException: TypeNotPresentExceptionProxy when running integration-test with maven-failsafe-plugin and spring-boot 1.4.
You can see this error if you run joinfaces-example with
mvn -Pattach-integration-test clean install
I realized that the error does not occur if I change spring-boot-maven-plugin to run at pre-integration-test phase instead of package one.
More, this error started when I upgraded spring boot to 1.4. No error occurs if I change jsf-spring-boot-parent version to 2.0.0 which uses spring boot 1.3 version.
I actually found the answer in Spring Boot 1.4 release notes, short answer is that maven-failsafe-plugin is not compatible with Spring Boot 1.4's new executable layout. Full explanation below :
As of Failsafe 2.19, target/classes is no longer on the classpath and
the project’s built jar is used instead. The plugin won’t be able to
find your classes due to the change in the executable jar layout.
There are two ways to work around this issue:
Downgrade to 2.18.1 so that you use target/classes instead
Configure the spring-boot-maven-plugin to use a classifier for the
repackage goal. That way, the original jar will be available and used
by the plugin. For example :
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
An alternative is documented here: https://github.com/spring-projects/spring-boot/issues/6254
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<!--
Make failsafe and spring-boot repackage play nice together,
see https://github.com/spring-projects/spring-boot/issues/6254
-->
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
</configuration>
</plugin>
This worked better for me, because when I used the "exec" solution, Spring failed to find my configuration files when starting the container. Which could probably be fixed by adding some further configuration parameters, I suppose, but this solution works "out of the box" for me.

How can I configure the heap size when starting a Spring Boot application with embedded Tomcat?

I am trying to deploy a Spring Boot powered web app to production. The app is built with Spring Boot 1.0.1 and has the default Tomcat 7 embedded as application server. I want to allocate larger memory to the app when start the app with java -jar myapp.jar command line.
Should I use JVM parameter such as -Xms -Xmx or use environment variable such as JAVA_OPTS? I have tried to look for the answer in documentation or google it, but I did not get an answer. Can anyone give some hints?
If starting the application with the spring-boot plugin:
mvn spring-boot:run -Drun.jvmArguments="-Xmx512m" -Drun.profiles=dev
Otherwise if running java -jar:
java -Xmx512m -Dspring.profiles.active=dev -jar app.jar
Since this is specifically a Spring Boot question, I'd argue that a more useful answer than #DaveSyer's is this:
You can drop a .conf file in the same directory as your WAR file that is effectively a shell script.
For example,
$ ls
myapp.conf
myapp.war
$ cat myapp.conf
export JAVA_OPTS="-Xmx1024m -Xms256m"
Any configuration you do there will be run before the Spring Boot embedded Tomcat starts up. Personally, I version control a .conf.example file in my application itself and then drop a copy of it on each server I deploy to.
Of course, anything you set in that .conf file is overridable with command-line operations.
Just use whatever normal mechanism you would to set up the JVM. Documentation is available on the command line:
$ java -X
...
-Xms<size> Set initial Java heap size
-Xmx<size> Set maximum Java heap size
...
For Spring Boot 2, you have to specify the heap size in the pom.xml file as below:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Xmx64m</jvmArguments>
</configuration>
</plugin>
For Spring Boot 1, the Maven argument to specify in the plugin configuration is jvmArguments, and the user property is run.jvmArguments:
mvn spring-boot:run -Drun.jvmArguments="-Xms2048m -Xmx4096m"
For Spring Boot 2, the Maven argument to specify in the plugin configuration is also jvmArguments, but the user property is now spring-boot.run.jvmArguments:
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xms2048m -Xmx4096m"
So if you use the plugin configuration way, both for Spring Boot 1 and 2 you can do that:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
-Xms4048m
-Xmx8096m
</jvmArguments>
</configuration>
</plugin>

Resources