VSCode, Java, Spring Boot and Maven - Environment variables not being read - java

Note Well
Please see the edit section of the question below, as further investigation has rendered the majority of this question redundant.
Similar to this question, I can't seem to pass environment variables to my app on startup while using VSCode. The problem is, I have no idea what (or if an) extension is causing the issue.
I have VSCode 1.38.1 with the following extensions;
Language support for Java 0.48.0
Java Extension Pack 0.8.0
Maven for Java 0.18.2
Spring Boot Dashboard 0.1.6
Spring Boot Tools 1.10.0
and several others that I don't feel are the culprits.
What I want to do is use maven profiles to conditionally bring in dependencies, and use environment variables to trigger that. In my pom.xml, I have;
<profiles>
<profile>
<id>dev-mysql</id>
<activation>
<!-- activate if system properties 'env=dev-mysql' passed in on launch.json env property from VSCode -->
<!-- Following does not work :( -->
<property>
<name>env</name>
<value>dev-mysql</value>
</property>
<!-- Obtain the following info by running - mvn enforcer:display-info -->
<!-- <os>
<name>windows 7</name>
<family>windows</family>
<arch>amd64</arch>
<version>6.1</version>
</os> -->
</activation>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
</dependencies>
</profile>
</profiles>
I try and pass that env value in by setting it in launch.json via something like;
{
"type": "java",
"name": "Debug (Launch)-MyServiceApplication<my-service>",
"request": "launch",
"mainClass": "myapp.MyServiceApplication",
"projectName": "my-service",
"vmArgs": "-Dspring.profiles.active=local-mysql",
"env": {
"env": "dev-mysql"
}
}
I've also tried adding in maven customEnv through settings.json as the Maven for Java extension seems to suggest that "...These environment variable values will be added to the terminal session before Maven is first executed". My settings.json file looks like;
{
"java.configuration.updateBuildConfiguration": "automatic",
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"maven.terminal.customEnv": [
{
"environmentVariable": "env",
"value": "dev-mysql"
}
]
}
I execute my application by hitting F5, which to my understanding executes what is shown in the launch.json snippet above. I don't quite understand how maven is invoked here. However, no matter what, it doesn't seem to ever find the environment variable and therefore include the profile specific mysql dependency. Even if I use Spring Boot Dashboard, I get the same result; the mysql dependency isn't recognised so I end up with Cannot load driver class: com.mysql.cj.jdbc.Driver
If I comment out the <property> element of my pom.xml and uncomment the <os> element, then it does bring in the profile specific mysql dependency. So I am guessing it uses the pom file correctly.
Does anyone have any clue how to set environment variables so that they'll be recognised with this configuation? Also, any further information as to what is actually executed when I hit F5 with this setup (is it maven, is it Java), it would be much appreciated.
Edit:
Some further investigation has shown that I was "...confusing Environment Variables with System Properties" comment by Wouter Lievens.
This post also suggests that "you cannot activate profiles based on system properties defined after the build plan has started execution", which may well be what happens when I just hit F5 to execute my application. This also links to some further information in this answer which clarifies that if won't work as "...profiles are the first thing evaluated before anything else to determine the effective POM.".
My workaround is to not use F5, but instead, when I have set the spring-profiles-active=local-mysql, I have to use the Maven for Java Explorer to execute a custom target of spring-boot:run -Pdev-mysql
So my last questions are;
Given the extensions I have added to VSCode combined with the above launch.json, what is actually executed when I hit F5 with this setup - is it Maven, Java or something else? If it is Java, how does the pom.xml come into play? If it is Java, is there a way of adjusting the launch.json to use mvn instead?
Is there a way to up the verbosity of the logging when I hit F5 to get a better understanding of what is actually being executed?

when running vscode driven tests, putting the environment variables in settings.json instead of launch.json works for me:
{
"java.configuration.updateBuildConfiguration": "automatic",
"java.test.config": [
{
"workingDirectory": "${workspaceFolder}",
"env": {
"STAGE_ENV": "DEV",
"AWS_REGION": "us-west-2"
}
}
]
}
however: maven tests do not read from this. I can run maven tests by adding the variables in the pom at this location:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
</plugin>
<plugin>
<!-- run unit tests -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<environmentVariables>
<!-- add them here -->
<STAGE_ENV>DEV</AWS_ACCESS_KEY>
<AWS_REGION>us-west-2</AWS_REGION>
<!-- -->
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
I have yet to find a good solution to use a single .env file to support both cases.

Related

VSCode Unbound classpath container JRE System Library [JavaSE-15]

I have been programming a java project using JDK14 and VSCode. Recently JDK15 is available, and I switched to the JDK. As for configuration, I pointed the VScode java.home and system JAVA_HOME to the new JDK folder.
When I clear the VSCode cache and restart the IDE, I started receiving this error
{
"resource": "/E:/dev/java/challenges/",
"owner": "_generated_diagnostic_collection_name_#3",
"code": "963",
"severity": 8,
"message": "Unbound classpath container: 'JRE System Library [JavaSE-15]' in project 'challenges'",
"source": "Java",
"startLineNumber": 1,
"startColumn": 1,
"endLineNumber": 1,
"endColumn": 1
}
I have seen similar questions/answers, but none of them was directed to VSCode.
https://stackoverflow.com/a/42525941/1005462
this one was helpfull;
replace :
build block in POM to
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
According to VsCode-Java in Twitter, JDK15 support won't be released until the end of September, and here is a github issue that's related to Java 15 not support.
I am new to building Java apps in VSCode. In my case, I was developing a microservice using Spring Boot. Here is what I've done to solve the issue ;
Make sure JAVA_HOME env variable is registered to the right path
Configured JDK path in java.jdt.ls.java.home to settings.json located inside .vscode. Here is my configuration.
{
"java.configuration.updateBuildConfiguration": "automatic",
"java.compile.nullAnalysis.mode": "automatic",
"java.jdt.ls.java.home": "/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home"
}
Restart the VSCode

Read POM values in external application properties

I have the following properties in the pom file
<name>DemoApplication</name>
<description>Demo spring project</description>
<version>1.0.0-SNAPSHOT</version>
And I have a class that reads the properties from application.yml
But instead of using the application.yml under src/main/resources I am specifying the properties through an external file as follows
java -jar DemoApplication-1.0.0-SNAPSHOT.jar --spring.config.location=application.yml
In this external application properties, I have the following attributes
swagger:
apiTitle: '#project.name#'
apiDescription: '#project.description#'
apiVersion: '#project.version#'
The issue is that the #project.name# and other properties are not being replaced as expected, but are read as-is.
How should the problem be approached?
According that section of the official documentation of Spring Boot v2, you can configure it with :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
With useDefaultDelimiters set to false or to true depending on your configuration.
The others sections of that official documentation will be helpful for your use case, especially these one : "77.5 Use YAML for External Properties".
If nothing is working, why don't you are loading a custom Properties file ? It could be loaded as you need without any problem. Just reference it with the correct path when you are starting your program, and inside your program, test if your file config.properties is available and contains what you need to work with.
Of course, the Maven way of loading resources files is the best easy way to go, and it should be a simple Properties file too. I have done exactly that way inside the software I am released to manage my configuration :
Writing a app.properties
Loading that file with Maven at runtime with resource configuration
Expanding properties with classical syntax ${my.prop}
Run the program with a Maven task.
Of course, when you distribute your app as a jar, it is a bit different.
Maybe you can try to write your properties files within a Maven goal.

Using google cloud endpoints on AppEngine

I normally use Google Cloud Endpoints on the AppEngine (Java) , as described in :
https://cloud.google.com/appengine/docs/java/endpoints/helloendpoints-java-maven
The dependency for the endpoints library I use is :
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-endpoints</artifactId>
<version>1.9.48</version>
</plugin>
Using this, I can start a local development server using the command:
mvn clean package appengine:devserver
However, there seems to be a new version of cloud endpoints.
https://cloud.google.com/endpoints/docs/frameworks/java/quickstart-frameworks-java .
The new framework is found here
<dependency>
<groupId>com.google.endpoints</groupId>
<artifactId>endpoints-framework</artifactId>
<version>${endpoints.framework.version}</version>
</dependency>
The same maven commands do not work here. I am unable to start a local dev server, open the API explorer or use a local datastore (all of which was possible earlier) . Could someone please guide me on how to work with the new framework.
Also, is the former framework likely to be deprecated ?
To answer my own question partially :
I could finally get the "Echo application" (mentioned in https://cloud.google.com/endpoints/docs/frameworks/java/quickstart-frameworks-java) to work
But I had to make 2 changes:
a) Comment out the block in appengine-web.xml . ie,
<!--
<basic-scaling>
<max-instances>2</max-instances>
</basic-scaling>
-->
After doing this, I got a different error, "failed endpoints-api-configuration: com.google.api.config.ServiceConfigException: Failed to fetch default config version for service"
To get around this :
b) Comment out the ServiceManagementConfigFilter from web.xml , ie,
<!--
<filter>
<filter-name>endpoints-api-configuration</filter-name>
<filter-class>com.google.api.control.ServiceManagementConfigFilter</filter-class>
</filter>
-->
<!--
<filter-mapping>
<filter-name>endpoints-api-configuration</filter-name>
<servlet-name>EndpointsServlet</servlet-name>
</filter-mapping>
-->
After this,
To build : mvn clean package
To run locally : appengine-java-sdk/1.9.44/appengine-java-sdk/appengine-java-sdk-1.9.44/bin/dev_appserver.sh /path/to/war/directory
It would be great if someone could shed more light on implication of these changes, and on how we could get it to work out of the box
There are a few problems you are running into and this stuff is overly sensitive to configuration issues:
To solve the problems follow the instructions in: https://cloud.google.com/endpoints/docs/frameworks/java/quickstart-frameworks-java
Use the correct Google project id when you replace the YOUR_PROJECT_ID in pom.xml. It needs to be a valid project id for all the steps to work.
Same when replacing the YOUR-PROJECT-ID in echo.java
If the project id is not valid (actually exists in AppEngine) the next steps won't work
execute: mvn exec:java -DGetSwaggerDoc
execute: gcloud service-management deploy openapi.json
execute: export ENDPOINTS_SERVICE_NAME=echo-api.endpoints.<your project id>.cloud.goog
The quickstart guide is not very helpful for step 5. Step 4 needs to end with a success message.
Finally the sample comes with a Maven plugin that does not seem to work with the new Endpoints.
Instead of using:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.maven.plugin.version}</version>
</plugin>
use:
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.9.44</version>
</plugin>
The answer to the question why mvn appengine:devserver doesn't work is that the devserver target doesn't exist in the new plugin.
The old Maven plugin allows you to execute: mvn appengine:devserver

Selecting class by Maven build profile

I am quite new to Maven and Java EE programming all-together.
I would like to create a stub class for authentication testing which should be activated in the default Maven build profile.
Currently I have two classes with same name but in different packages. Is it possible to somehow select the correct class to use in the build phase by setting maven build profile parameters? I am also using EJB and JSF2.0 in my project and the authentication object is created in one of the beans:
AuthUtil util = new AuthUtil();
It is possible, with some footwork. You will have to put your class(es) in a dependency and use the profiles in this manner:
<profiles>
<profile>
<id>default</id>
<dependencies>
<dependency>...</dependency>
</dependencies>
</profile>
<profile>
<id>someotherprofile</id>
<dependencies>
<dependency>...</dependency>
</dependencies>
</profile>
</profiles>
Also, the classes will have to be in the same package for this to work.
Cheers,
You can specify the concrete class in a property file, and filter property files by maven build profile, so they would get different values. Property file would then be read by java code and it would be used accordingly.
Is there some reason to do this? It doesn't feel like the right way of doing things...

Retrieve version from maven pom.xml in code

What is the simplest way to retrieve version number from maven's pom.xml in code, i.e., programatically?
Assuming you're using Java, you can:
Create a .properties file in (most commonly) your src/main/resources directory (but in step 4 you could tell it to look elsewhere).
Set the value of some property in your .properties file using the standard Maven property for project version:
foo.bar=${project.version}
In your Java code, load the value from the properties file as a resource from the classpath (google for copious examples of how to do this, but here's an example for starters).
In Maven, enable resource filtering. This will cause Maven to copy that file into your output classes and translate the resource during that copy, interpreting the property. You can find some info here but you mostly just do this in your pom:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
You can also get to other standard properties like project.name, project.description, or even arbitrary properties you put in your pom <properties>, etc. Resource filtering, combined with Maven profiles, can give you variable build behavior at build time. When you specify a profile at runtime with -PmyProfile, that can enable properties that then can show up in your build.
The accepted answer may be the best and most stable way to get a version number into an application statically, but does not actually answer the original question: How to retrieve the artifact's version number from pom.xml? Thus, I want to offer an alternative showing how to do it dynamically during runtime:
You can use Maven itself. To be more exact, you can use a Maven library.
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.3.9</version>
</dependency>
And then do something like this in Java:
package de.scrum_master.app;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.FileReader;
import java.io.IOException;
public class Application {
public static void main(String[] args) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
System.out.println(model.getId());
System.out.println(model.getGroupId());
System.out.println(model.getArtifactId());
System.out.println(model.getVersion());
}
}
The console log is as follows:
de.scrum-master.stackoverflow:my-artifact:jar:1.0-SNAPSHOT
de.scrum-master.stackoverflow
my-artifact
1.0-SNAPSHOT
Update 2017-10-31: In order to answer Simon Sobisch's follow-up question I modified the example like this:
package de.scrum_master.app;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Application {
public static void main(String[] args) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model;
if ((new File("pom.xml")).exists())
model = reader.read(new FileReader("pom.xml"));
else
model = reader.read(
new InputStreamReader(
Application.class.getResourceAsStream(
"/META-INF/maven/de.scrum-master.stackoverflow/aspectj-introduce-method/pom.xml"
)
)
);
System.out.println(model.getId());
System.out.println(model.getGroupId());
System.out.println(model.getArtifactId());
System.out.println(model.getVersion());
}
}
Packaged artifacts contain a META-INF/maven/${groupId}/${artifactId}/pom.properties file which content looks like:
#Generated by Maven
#Sun Feb 21 23:38:24 GMT 2010
version=2.5
groupId=commons-lang
artifactId=commons-lang
Many applications use this file to read the application/jar version at runtime, there is zero setup required.
The only problem with the above approach is that this file is (currently) generated during the package phase and will thus not be present during tests, etc (there is a Jira issue to change this, see MJAR-76). If this is an issue for you, then the approach described by Alex is the way to go.
There is also the method described in Easy way to display your apps version number using Maven:
Add this to pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>test.App</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Then use this:
App.class.getPackage().getImplementationVersion()
I have found this method to be simpler.
If you use mvn packaging such as jar or war, use:
getClass().getPackage().getImplementationVersion()
It reads a property "Implementation-Version" of the generated META-INF/MANIFEST.MF (that is set to the pom.xml's version) in the archive.
To complement what #kieste has posted, which I think is the best way to have Maven build informations available in your code if you're using Spring-boot: the documentation at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info is very useful.
You just need to activate actuators, and add the properties you need in your application.properties or application.yml
Automatic property expansion using Maven
You can automatically expand info properties from the Maven project using resource filtering. If you use the spring-boot-starter-parent you can then refer to your Maven ‘project properties’ via #..# placeholders, e.g.
project.artifactId=myproject
project.name=Demo
project.version=X.X.X.X
project.description=Demo project for info endpoint
info.build.artifact=#project.artifactId#
info.build.name=#project.name#
info.build.description=#project.description#
info.build.version=#project.version#
When using spring boot, this link might be useful: https://docs.spring.io/spring-boot/docs/2.3.x/reference/html/howto.html#howto-properties-and-configuration
With spring-boot-starter-parent you just need to add the following to your application config file:
# get values from pom.xml
pom.version=#project.version#
After that the value is available like this:
#Value("${pom.version}")
private String pomVersion;
Sometimes the Maven command line is sufficient when scripting something related to the project version, e.g. for artifact retrieval via URL from a repository:
mvn help:evaluate -Dexpression=project.version -q -DforceStdout
Usage example:
VERSION=$( mvn help:evaluate -Dexpression=project.version -q -DforceStdout )
ARTIFACT_ID=$( mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout )
GROUP_ID_URL=$( mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout | sed -e 's#\.#/#g' )
curl -f -S -O http://REPO-URL/mvn-repos/${GROUP_ID_URL}/${ARTIFACT_ID}/${VERSION}/${ARTIFACT_ID}-${VERSION}.jar
Use this Library for the ease of a simple solution. Add to the manifest whatever you need and then query by string.
System.out.println("JAR was created by " + Manifests.read("Created-By"));
http://manifests.jcabi.com/index.html
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Get Version using this.getClass().getPackage().getImplementationVersion()
PS Don't forget to add:
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
Step 1: If you are using Spring Boot, your pom.xml should already contain spring-boot-maven-plugin. You just need to add the following configuration.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
It instructs the plugin to execute also build-info goal, which is not run by default. This generates build meta-data about your application, which includes artifact version, build time and more.
Step2: Accessing Build Properties with buildProperties bean. In our case we create a restResource to access to this build info in our webapp
#RestController
#RequestMapping("/api")
public class BuildInfoResource {
#Autowired
private BuildProperties buildProperties;
#GetMapping("/build-info")
public ResponseEntity<Map<String, Object>> getBuildInfo() {
Map<String, String> buildInfo = new HashMap();
buildInfo.put("appName", buildProperties.getName());
buildInfo.put("appArtifactId", buildProperties.getArtifact());
buildInfo.put("appVersion", buildProperties.getVersion());
buildInfo.put("appBuildDateTime", buildProperties.getTime());
return ResponseEntity.ok().body(buldInfo);
}
}
I hope this will help
I had the same problem in my daytime job. Even though many of the answers will help to find the version for a specific artifact, we needed to get the version for modules/jars that are not a direct dependency of the application. The classpath is assembled from multiple modules when the application starts, the main application module has no knowledge of how many jars are added later.
That's why I came up with a different solution, which may be a little more elegant than having to read XML or properties from jar files.
The idea
use a Java service loader approach to be able to add as many components/artifacts later, which can contribute their own versions at runtime. Create a very lightweight library with just a few lines of code to read, find, filter and sort all of the artifact versions on the classpath.
Create a maven source code generator plugin that generates the service implementation for each of the modules at compile time, package a very simple service in each of the jars.
The solution
Part one of the solution is the artifact-version-service library, which can be found on github and MavenCentral now. It covers the service definition and a few ways to get the artifact versions at runtime.
Part two is the artifact-version-maven-plugin, which can also be found on github and MavenCentral. It is used to have a hassle-free generator implementing the service definition for each of the artifacts.
Examples
Fetching all modules with coordinates
No more reading jar manifests, just a simple method call:
// iterate list of artifact dependencies
for (Artifact artifact : ArtifactVersionCollector.collectArtifacts()) {
// print simple artifact string example
System.out.println("artifact = " + artifact);
}
A sorted set of artifacts is returned. To modify the sorting order, provide a custom comparator:
new ArtifactVersionCollector(Comparator.comparing(Artifact::getVersion)).collect();
This way the list of artifacts is returned sorted by version numbers.
Find a specific artifact
ArtifactVersionCollector.findArtifact("de.westemeyer", "artifact-version-service");
Fetches the version details for a specific artifact.
Find artifacts with matching groupId(s)
Find all artifacts with groupId de.westemeyer (exact match):
ArtifactVersionCollector.findArtifactsByGroupId("de.westemeyer", true);
Find all artifacts where groupId starts with de.westemeyer:
ArtifactVersionCollector.findArtifactsByGroupId("de.westemeyer", false);
Sort result by version number:
new ArtifactVersionCollector(Comparator.comparing(Artifact::getVersion)).artifactsByGroupId("de.", false);
Implement custom actions on list of artifacts
By supplying a lambda, the very first example could be implemented like this:
ArtifactVersionCollector.iterateArtifacts(a -> {
System.out.println(a);
return false;
});
Installation
Add these two tags to all pom.xml files, or maybe to a company master pom somewhere:
<build>
<plugins>
<plugin>
<groupId>de.westemeyer</groupId>
<artifactId>artifact-version-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>generate-service</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.westemeyer</groupId>
<artifactId>artifact-version-service</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Feedback
It would be great if maybe some people could give the solution a try. Getting feedback about whether you think the solution fits your needs would be even better. So please don't hesitate to add a new issue on any of the github projects if you have any suggestions, feature requests, problems, whatsoever.
Licence
All of the source code is open source, free to use even for commercial products (MIT licence).
It's very easy and no configuration is needed if you use Spring with Maven.
According to the “Automatic Property Expansion Using Maven” official documentation you can automatically expand properties from the Maven project by using resource filtering. If you use the spring-boot-starter-parent, you can then refer to your Maven ‘project properties’ with #..# placeholders, as shown in the following example:
project.version=#project.version#
project.artifactId=#project.artifactId#
And you can retrieve it with #Value annotation in any class:
#Value("${project.artifactId}#${project.version}")
private String RELEASE;
I hope this helps!
With reference to ketankk's answer:
Unfortunately, adding this messed with how my application dealt with resources:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
But using this inside maven-assemble-plugin's < manifest > tag did the trick:
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
So I was able to get version using
String version = getClass().getPackage().getImplementationVersion();
Preface: Because I remember this often referred-to question after having answered it a few years ago, showing a dynamic version actually accessing Maven POM infos dynamically (e.g. also during tests), today I found a similar question which involved accessing module A's Maven info from another module B.
I thought about it for a moment and spontaneously had the idea to use a special annotation, applying it to a package declaration in package-info.java. I also created a multi-module example project on GitHub. I do not want to repeat the whole answer, so please see solution B in this answer. The Maven setup involves Templating Maven Plugin, but could also be solved in a more verbose way using a combination of resource filtering and adding generated sources directory to the build via Build Helper Maven. I wanted to avoid that, so I simply used Templating Maven.
Accepted answer worked for me once in the step #2 I changed ${project.version} to ${pom.version}

Categories

Resources