简体   繁体   English

有没有办法在 Maven pom.xml 中插入所有 ${project.version} 变量?

[英]Is there a way to interpolate all ${project.version} variables in a Maven pom.xml?

I need to replace all occurrences of ${project.version} with the real value in a pom.xml.我需要用 pom.xml 中的真实值替换所有出现的${project.version}

I can do that with the maven-flatten-plugin , but that will rewrite the whole pom.xml instead of just replacing the project.version only.我可以用maven-flatten-plugin做到这一点,但这将重写整个 pom.xml 而不是仅仅替换project.version

As I want to use the jgitver-maven-plugin I can't use the resolveCiFriendliesOnly mode of the flatten plugin.因为我想使用jgitver-maven-plugin所以我不能使用 flatten 插件的resolveCiFriendliesOnly模式。

Is there an easy way to replace only one variable in the whole pom.xml?有没有一种简单的方法可以只替换整个 pom.xml 中的一个变量? Is there another (hidden) option in the flatten plugin to do this? flatten 插件中是否有另一个(隐藏)选项可以执行此操作?

You can use the maven release plugin, specifically the update-versions goal.您可以使用 maven 发布插件,特别是 update-versions 目标。

ex: mvn --batch-mode release:update-versions -DdevelopmentVersion=1.2.0-SNAPSHOT例如: mvn --batch-mode release:update-versions -DdevelopmentVersion=1.2.0-SNAPSHOT

https://maven.apache.org/maven-release/maven-release-plugin/examples/update-versions.html https://maven.apache.org/maven-release/maven-release-plugin/examples/update-versions.html

There is something close to what you want to achieve, but may be good enough.有一些接近您想要实现的目标,但可能已经足够好了。 However this will resolve all variables in POM sections of your choice.但是,这将解析您选择的 POM 部分中的所有变量。

I used resolveCiFriendliesOnly , as it's the only mode that kind of preserves the original POM.我使用了resolveCiFriendliesOnly ,因为它是唯一一种保留原始 POM 的模式。

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.2.2</version>
                <configuration>
                    <flattenMode>resolveCiFriendliesOnly</flattenMode>
                    <pomElements>
                        <dependencyManagement>resolve</dependencyManagement>
                        <!-- add more sections to resolve -->
                    </pomElements>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Now you can add more sections under <pomElements> where you want variables resolved.现在,您可以在<pomElements>下添加更多要解析变量的部分。 As a side-effect of resolveCiFriendliesOnly , variables ${revision} , ${sha1} and ${changelist} will be resolved in the whole POM file.作为resolveCiFriendliesOnly的副作用,变量${revision}${sha1}${changelist}将在整个 POM 文件中解析。

Keep in mind that flatten-maven-plugin will still rewrite your POM, ie you may get sections reordered, lose comments, indentation, etc.请记住, flatten-maven-plugin仍然会重写您的 POM,即您可能会重新排序部分,丢失评论,缩进等。

Here's a list of sections you can configure: https://www.mojohaus.org/flatten-maven-plugin/apidocs/org/codehaus/mojo/flatten/FlattenDescriptor.html以下是您可以配置的部分列表: https : //www.mojohaus.org/flatten-maven-plugin/apidocs/org/codehaus/mojo/flatten/FlattenDescriptor.html

If resolveCiFriendliesOnly still messes up your POM, you can remove flattenMode , but then it will go wild and you must add nearly all possible sections to <pomElements> with keep option, eg如果resolveCiFriendliesOnly仍然弄乱了你的 POM,你可以删除flattenMode ,但它会变得疯狂,你必须使用keep选项将几乎所有可能的部分添加到<pomElements> ,例如

<pomElements>
    <dependencyManagement>resolve</dependencyManagement>
    <!-- keep everything else -->
    <parent>keep</parent>
    <build>keep</build>
    <distributionManagement>keep</distributionManagement>
    <repositories>keep</repositories>
    <pluginRepositories>keep</pluginRepositories>
    <profiles>keep</profiles>
    ...
</pomElements>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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