简体   繁体   中英

Insert Project version to scala code in compile time

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM