I have mixed Java/Scala project, I use Maven as a building tools and I have the version of my project mentioned in the pom.xml file:
<parent>
<groupId>com</groupId>
<artifactId>stuff</artifactId>
<version>3.6.7.7-SNAPSHOT</version>
</parent>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>scala-compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
I want to use the project Version (3.6.7.7-SNAPSHOT) in my scala code, which means I need to inject it somehow in the compilation process, is there any way to do it?
Already tried to use getProperty function -
val properties = System.getProperties()
override def version: String = properties.get("parent.version")
but it returned null.
edit- I found a similar question for Java code.
I decided to use the maven-replacer-plugin
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>.\src\main\resources\Installer.scala</file>
<outputFile>.\src\main\scala\com\Installer.scala</outputFile>
<replacements>
<replacement>
<token>@pomversion@</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
but the drawback is that I had to create a new file in the resources directory, is there any way I can return the class back to its original state after the compilation,even in case were the compilation fails?
Solved-
I used regex with the maven-replacer-plugin
, located the line where the version variable was defined and change it's value using -
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>.\src\main\scala\com\Installer.scala</file>
<outputFile>.\src\main\scala\com\Installer.scala</outputFile>
<replacements>
<replacement>
<token>override def version: String = (.*)</token>
<value>override def version: String = "${project.version}"</value>
</replacement>
</replacements>
</configuration>
</plugin>
Using this method the variable change every build according to the right version without adding new files to the project.