【问题标题】:Insert Project version to scala code in compile time在编译时将项目版本插入到 scala 代码中
【发布时间】:2016-04-07 02:41:58
【问题描述】:

我有混合 Java/Scala 项目,我使用 Maven 作为构建工具,并且我的项目版本在 pom.xml 文件中提到:

<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>

我想在我的scala代码中使用项目版本(3.6.7.7-SNAPSHOT),这意味着我需要在编译过程中以某种方式注入它,有什么办法吗?

已经尝试使用getProperty函数-

val properties = System.getProperties()
override def version: String = properties.get("parent.version")

但它返回 null。

edit-我发现了一个类似的 question 用于 Java 代码。

我决定使用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>

但缺点是我必须在资源目录中创建一个新文件,有什么办法可以在编译后将类恢复到原始状态,即使编译失败?

已解决- 我将正则表达式与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\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>

使用此方法,变量会根据正确的版本更改每次构建,而无需向项目添加新文件。

【问题讨论】:

标签: java scala maven


【解决方案1】:

您可以使用 maven 资源插件来做到这一点。

details.txt文件放在src/main/resources

details.txt 内容

project.version=${project.version}

现在在你的 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>

这将使用 project.version 值写入 details.txt。您也可以获取任何其他变量值。

现在由于 details.txt 是您构建的一部分,阅读它的内容是微不足道的。

【讨论】:

  • 感谢 yabukijoe 的回答!但是这个解决方案有点问题,因为我不想添加文件,而且我觉得我无法控制它,因为任何用户都可以更改这些文本文件并影响输出。
【解决方案2】:

已解决-我将正则表达式与 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\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>

使用此方法,变量会根据正确的版本更改每次构建,而无需向项目添加新文件。

【讨论】:

    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多