本文介绍了如何在构建时在maven中设置项目版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我们的项目中实现语义版本控制。我测试了maven semver插件,但这并没有帮助我,所以请不要问我为什么。我终于结束了使用maven groovy。它的作用就像一个魅力,但是,当我安装或部署maven项目时,版本库中的版本是变量名称。



尽管所有的人工制品和jar文件都使用正确的版本进行打包。



看看我的pom.xml

 <?xml version =1.0encoding =UTF-8?> 
< project xmlns =http://maven.apache.org/POM/4.0.0
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd\"> ;

< modelVersion> 4.0.0< / modelVersion>
< groupId> com.mytest.test< / groupId>
< artifactId> test-tag< / artifactId>
< version> $ {revision}< / version>


< description>测试< / description>

<属性>
< ChangeType> TO_BE_SET< / ChangeType>
< / properties>


< build>
< plugins>
< plugin>
< groupId> org.codehaus.gmaven< / groupId>
< artifactId> gmaven-plugin< / artifactId>
< version> 1.5< / version>
<执行次数>
<执行>
<阶段>验证< /阶段>
<目标>
< goal>执行< / goal>
< /目标>
<配置>
< providerSelection> 2.0< / providerSelection>
<属性>
< script> git describe --abbrev = 0 --tags< / script>
< / properties>
< source>
def tagIt ='git tag -a vXXXX -mAuto tagged'
def changeType = project.properties.ChangeType
def command = project.properties.script
def process = command.execute()
process.waitFor()
def describe = process.in.text.trim()
println将修订设置为:+ describe

if(!describe.startsWith(v)){
describe =1.0.1
} else {
describe = describe.substring(1)
}

project.properties.setProperty('revision',describe)


< / source>
< / configuration>
< /执行>
< /执行次数>
< / plugin>


< plugin>
< artifactId> maven-compiler-plugin< / artifactId>
<执行次数>
<执行>
< id> default-compile< / id>
<阶段>无< /阶段>
< /执行>
<执行>
< id> default-testCompile< / id>
<阶段>无< /阶段>
< /执行>
< /执行次数>
< / plugin>

< plugin>
< artifactId> maven-resources-plugin< / artifactId>
<执行次数>
<执行>
< id> default-testResources< / id>
<阶段>无< /阶段>
< /执行>
< /执行次数>
< / plugin>

< plugin>
< artifactId> maven-jar-plugin< / artifactId>
<执行次数>
<执行>
< id> default-jar< / id>
<阶段>包< /阶段>
< /执行>
< /执行次数>
< / plugin>

< plugin>
< artifactId> maven-surefire-plugin< / artifactId>
<执行次数>
<执行>
< id> default-test< / id>
<阶段>无< /阶段>
< /执行>
< /执行次数>
< / plugin>
< / plugins>
< / build>

< / project>

版本是$ {revision}一个在groovy脚本中设置的变量名称。什么groovy代码是从GIT获取最后一个标记,然后将其设置为属性'revision'。

最终的jar文件具有正确的版本,但是在安装时进入存储库,文件夹名称和jar名称如下所示:

我试图默认'revision'使用以下值:

 < properties> 
< revision> 1.0.1< / revision>
< / properties>

但是然后groovy代码设置的值没有效果。

我也为maven groovy插件尝试了不同的阶段,没有运气。我错过了什么?任何人都可以请帮助我吗?



我想提及,因为vatbub和StefanHeimberg提到我可以使用版本:设置版本,但这需要我做一个额外的承诺,我试图避免GIT,想知道如果我可以通过编写一个Maven插件来实现这一点?

解决方案

使用Maven,您可以在构建时使用

  mvn版本:set -DnewVersion = $ {bamboo.inject.version} 

as vatbub已在您的问题中发表评论。



除此之外,我还编写了一个Shell脚本,可用于构建管道以根据maven项目版本,并从构建服务器添加构建号。





脚本读取maven项目版本,添加内部版本号和写一个文本文件,其中包含所有可用的新数字。



下一步是将此文本文件注入到Build Pipeline中,并如上所述调用versions插件



pom.xml

类似于

 < project> 

< groupId> ch.stefanheimberg.example< / groupId>
< artifactId> your-awesome-app< / artifactId>
< version> 5.1.2-SNAPSHOT< / version>

< / project>

 <项目> 

< groupId> ch.stefanheimberg.example< / groupId>
< artifactId> your-awesome-app< / artifactId>
< version> 5.1-SNAPSHOT< / version>

< / project>

第1步

  ./ generate_version_txt.sh $ {bamboo.buildNumber} 



第二步:在构建系统中注入生成的version.txt,所有的属性都可以在所有的任务/插件中使用,如下所示: etc ...在我的例子中,Bamboo CI准备好了version.txt文件,并在竹下声明了文件的内容作为环境变量。注入。前缀。



例如 $ {bamboo.inject.long_version}



第3步:

更新Maven Project版本

  mvn版本:set -DnewVersion = $ {bamboo.inject.version} 

第4步



运行Maven Build

  mvn clean verify 

步骤5 :运行Docker build

xample使用它也作为码头标签版本。等等...

  docker build --build-arg version = $ {bamboo.inject.version} --tag your- awesome-app:$ {bamboo.inject.version}。 

示例Dockerfile:

  FROM jboss / wildlfy 
ARG版本
添加target / your-awesome-app - $ {version} .war / opt / jboss / wildfly / standalone / deployments /

我知道这对您的grovy脚本来说可能是一个问题/不可能。但是对于你的问题,这是另一种看法。也可能是另一个解决方案。



(对不起我的英语,但我希望我的意思是可以理解的)


I am trying to implement semantic versioning in our project. I tested maven semver plugin but that didn't help me so please don't ask me why. I finally ended up using maven groovy. It works like a charm, however, when I install or deploy the maven project the version in repository is the variable name.

This is despite the fact that all the artefacts and jar files are packaged with correct version.

So please look at my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mytest.test</groupId>
    <artifactId>test-tag</artifactId>
    <version>${revision}</version>


    <description>Test</description>

    <properties>
        <ChangeType>TO_BE_SET</ChangeType>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <providerSelection>2.0</providerSelection>
                            <properties>
                                <script>git describe --abbrev=0 --tags</script>
                            </properties>
                            <source>
                                def tagIt = 'git tag -a vXXXX -m "Auto tagged"'
                                def changeType = project.properties.ChangeType
                                def command = project.properties.script
                                def process = command.execute()
                                process.waitFor()
                                def describe = process.in.text.trim()
                                println "Setting revision to: " + describe

                                if(!describe.startsWith("v")) {
                                    describe = "1.0.1"
                                } else {
                                    describe = describe.substring(1)
                                }

                                    project.properties.setProperty('revision', describe)


                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>default-testCompile</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-testResources</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-test</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

the version is ${revision} a variable name that is being set in groovy script. What groovy code does is getting the last tag from GIT and and then set it to the property 'revision'.

The final jar file has the correct version extracted but when installed into repository, the folder name and jar name are like:

I tried to default 'revision' to a value using:

<properties>
  <revision>1.0.1</revision>
</properties>

but then groovy code setting the value has no effect.

I also tried different phase for the maven groovy plugin, no luck. Have I missed anything? Can anyone please help me on this?

I'd like to mention that as vatbub and StefanHeimberg mentioned I can use versions:set to set the version but this requires me to do an extra commit to GIT which I am trying to avoid, wondering if I can achieve this by writing a maven plugin instead?

解决方案

With Maven you can set the version at build time with

mvn versions:set -DnewVersion=${bamboo.inject.version}

as vatbub already commented in your question.

In addition tho this i wrote a Shell script that can be used in build pipeline to generate the version according to the maven project version and add the build number from the build server.

https://gist.github.com/StefanHeimberg/c19d7665e8df087845c036fe8b88c4f2

The Script reads the maven project version, add a the build number and writes a text file with all the new numbers that can be used.

The next step is to inject this text file in the Build Pipeline and call the versions plugin as stated above

pom.xml:

something like

<project>

    <groupId>ch.stefanheimberg.example</groupId>
    <artifactId>your-awesome-app</artifactId>
    <version>5.1.2-SNAPSHOT</version>

</project>

or

<project>

    <groupId>ch.stefanheimberg.example</groupId>
    <artifactId>your-awesome-app</artifactId>
    <version>5.1-SNAPSHOT</version>

</project>

Step 1:

./generate_version_txt.sh ${bamboo.buildNumber}

Step 2:

Inject generated version.txt in the build system that all the properties can be used in all tasks / plugins, etc...

In my case Bamboo CI ready the version.txt file and declares the content of the file as environment variables under the bamboo.inject. prefix.

For example ${bamboo.inject.long_version}

Step 3:

Update Maven Project version

mvn versions:set -DnewVersion=${bamboo.inject.version}

Step 4:

Run Maven Build

mvn clean verify

Step 5:

Run Docker build

for example use it also as docker tag version. etc...

docker build --build-arg version=${bamboo.inject.version} --tag your-awesome-app:${bamboo.inject.version} .

Example Dockerfile:

FROM jboss/wildlfy
ARG version
ADD target/your-awesome-app-${version}.war /opt/jboss/wildfly/standalone/deployments/

I know that can be a problem / not possible in your case with the grovy script. but perhabs it is an other view at your problem. and possibly also another solution for it.

(sorry for my english. but i hope it is understandable what i mean)

这篇关于如何在构建时在maven中设置项目版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:28