0

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.

Community
  • 1
  • 1
Karnimrod
  • 425
  • 1
  • 4
  • 8
  • 1
    Possible duplicate of [Generate a Version.java file in Maven](http://stackoverflow.com/questions/2469922/generate-a-version-java-file-in-maven) – Basilevs Jan 03 '16 at 10:01

2 Answers2

2

You can do this using maven resources plugin.

Put details.txt file under src/main/resources

details.txt content

project.version=${project.version}

Now use following plugin in your POM

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.build.directory}/details.txt</directory>
                        <includes>
                            <include>version.txt</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

This will write details.txt with the project.version value. You can get any other variable values also.

Now since details.txt is part of your build, reading it's content is trivial.

  • thanks yabukijoe for your answer! but this solution is a bit problematic since I don't want to add files, and I feel I don't have control on it since any user can change those text files and impect the output. – Karnimrod Jan 04 '16 at 13:08
0

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.

Karnimrod
  • 425
  • 1
  • 4
  • 8