Add VERSION_INFO to jar - spring-boot

How to add VERSION_INFO meta data to jar with spring boot ?
I have seen the plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
with the execution "repackage" but I cannot add this meta_data in my executable jar file.

Do you really need the version in META-INF/MANIFEST.MF?
If you add these placeholders to application.yml properties file:
# app name and build version updated during build process from Maven properties.
info:
app:
name: #project.artifactId#
build:
version: #project.version#
They get replaced while building the artifact and available when sending a GET request to /info actuator endpoint, for instance:
curl "http://localhost:8080/info"
{"app":{"name":"demo-cxf"},"build":{"version":"0-SNAPSHOT"}}

Related

Failure: (ID: 838926df) did not find any jar files with a Main-Class manifest entry

when running gcloud app deploy on my spring boot app, this error happens in Cloud Build.
There can be one or many issues because of which you get this error.
For resolution please check below things -
Your app.yaml should have entrypoint and runtime information as below -
runtime: java11
entrypoint: java -Xmx64m -jar blahblah.jar
your pom.xml should have appengine maven plugin dependency
`
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
</plugin>
`
don't modify jar if you want to replace any configs use command -
jar uf blahblah.jar filename.yaml
make sure you have packaging as a jar in pom.xml like this -
<packaging>jar</packaging>
I deleted my maven plugin by accident, so don't delete it.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
I was able to fix it for my AppEngine + Gradle project by adding
jar {
enabled = false
archiveClassifier = ''
}
to the build.gradle file.
Try to comment out in pom.xml packaging into war file, example:
<!-- <packaging>war</packaging> -->
It fixed the issue for me. Good minimal+workable example is here: https://codelabs.developers.google.com/codelabs/cloud-app-engine-springboot#0

Add maven build time to jar name build with Spring Boot Maven Plugin

How can add maven build time to jar file name using Spring Boot Maven plugin?
I want to achieve something like: jar_name-build_time.jar
By default, Spring Boot Maven Plugin builds jar file with name ${project.build.finalName}.
This can be configured with non-required property finalName.
Maven build time can be used as ${maven.build.timestamp}
So, putting all things together, all you need to do is append build time to default jar name:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.6.RELEASE</version>
<configuration>
<mainClass>com.marand.thinkmed.meds.config.boot.MedsConfigApplication</mainClass>
<finalName>${project.build.finalName}-${maven.build.timestamp}</finalName>
</configuration>
</plugin>
Also, make sure to change timestamp format so it doesn't violate file naming strategies:
<properties>
<maven.build.timestamp.format>yyyy-MM-dd-HH-mm</maven.build.timestamp.format>
</properties>

How to include external configuration resources to classpath in Spring (Spring Boot)?

I have 3rd party library that is configured by placing properties file on the root of the classpath. That library is using getClass().getResourceAsStream("/file.properties") to load that file. As it is 3rd party, it is unmodifiable. I have placed that configuration file into external resources directory (not to be mistaken with resources from eg. Maven's or Gradle's directory structure.
Directory structure is like this.
How to run/configure Spring boot to include content of resources directory to the classpath so getResourceAsStream wil work?
On SE application I would simply do java -jar myApp.jar with classpath in MANIFEST and that would work.
EDIT:
Just a word of clarification - putting configuration file inside project resources (along sources) is missing the whole point. I want to keep configuration externalized.
Here is how you can do it:
1.- Change your spring-boot-maven-plugin configuration to enable the Spring Boot PropertiesLauncher:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
</build>
2.- Launch your Spring Boot Application setting the location of the external properties file:
java -jar -Dloader.path=PATH_TO_PROPERTIES_FOLDER spring-ms-0.0.1-SNAPSHOT.jar
Assuming this folders:
/home/user/
|--- file.properties
|--- spring-mg-0.0.1-SNAPSHOT.jar
You should launch it like this: java -jar -Dloader.path=/home/user spring-ms-0.0.1-SNAPSHOT.jar

info endpoint - application.yml maven filtering not working

We are using spring boot 1.3.1.
I have following details in my application.yml file.
info:
build:
artifact: ${project.artifactId}
name: ${project.name}
description: ${project.description}
version: ${project.version}
I am building the war file and deploying it to tomcat. When I access /info endpoint, i don't see values are substituted.
I am not using 'spring-boot-maven-plugin' as i don't need to run the application standalone.
Does the filtering work for YML files by default?
Also, is there any example of showing application version on banner.txt file using maven project.version property?
After Dave's comment, I updated my application.yml file as shown below and it is working.
info:
build:
artifact: #project.artifactId#
name: #project.name#
description: #project.description#
version: #project.version#
Also, I found that application.version is not picked up in standalone tomcat for banner.txt file. Tomcat fails to read manifest.mf file.
Spring boot issue: https://github.com/spring-projects/spring-boot/issues/3406
I don't think filtering is switched on in maven by default. If you use the spring-boot-starter-parent it is enabled, but the key for the placeholders is #..# not ${..}.
I don't know which version of spring boot you are using,
But for me version: #project.version# did not work.
Instead version: #project.version# worked for me.
My project is having following parent pom config :
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.4.1.RELEASE</version>
<relativePath />
</parent>

Spring boot application with apache axis

I am trying to run a spring boot jar which has axis2 dependencies in it. I am using spring boot maven plugin to build the jar (with dependencies). When I try to run my jar, I get the following exception in my console:
org.apache.axis2.AxisFault: The G:application\myapp\target\myapp.jar!\lib\axis2-1.6.1.jar file cannot be found.
at org.apache.axis2.deployment.repository.util.DeploymentFileData.setClassLoader(DeploymentFileData.java:111)
at org.apache.axis2.deployment.ModuleDeployer.deploy(ModuleDeployer.java:70)
at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136)
at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:813)
at org.apache.axis2.deployment.RepositoryListener.loadClassPathModules(RepositoryListener.java:222)
at org.apache.axis2.deployment.RepositoryListener.init2(RepositoryListener.java:71)
at org.apache.axis2.deployment.RepositoryListener.<init>(RepositoryListener.java:64)
at org.apache.axis2.deployment.DeploymentEngine.loadFromClassPath(DeploymentEngine.java:175)
at org.apache.axis2.deployment.FileSystemConfigurator.getAxisConfiguration(FileSystemConfigurator.java:135)
at ...
I then checked the structure of my jar. It has lib folder inside it, which contained all the jars (including the above mentioned axis jar). Attached is the screen shot of lib folder.
Following are the solutions which I have tried:
Placed axis jar in the same directory as application jar.
Created lib folder in the same directory as application jar and placed axis jar inside it.
Modified manifest file to include Class-Path: /lib/
None of the solutions has worked. However, when I run the application class in eclipse, the app starts and runs perfectly. But, once I create the jar, nothing seems to run.
Can anyone please help? Thanks in advance.
It looks like Axis can't cope with being run from a jar that's nested within another jar. It works fine in Eclipse as the Axis jar is available directly on the filesystem rather than being nested inside your Spring Boot application's jar file.
You can configure your application's fat jar file so that Spring Boot knows to unpack the Axis jar into a temporary location when it's run. If you're using Maven:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<requiresUnpack>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>
</plugins>
</build>
And if you're using Gradle:
springBoot {
requiresUnpack = ['org.apache.axis2:axis2']
}
See the Spring Boot documentation for some further details.

Resources