【发布时间】:2018-04-20 08:59:25
【问题描述】:
我正在尝试在我们的项目中实现语义版本控制。我测试了 maven semver 插件,但这对我没有帮助,所以请不要问我为什么。我最终最终使用了 maven groovy。它就像一个魅力,但是,当我安装或部署 maven 项目时,存储库中的版本是变量名。
尽管所有人工制品和 jar 文件都以正确的版本打包。
所以请看我的 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>
版本是 ${revision} 一个在 groovy 脚本中设置的变量名。 groovy 代码所做的是从 GIT 中获取最后一个标签,然后将其设置为属性 'revision'。
最终的 jar 文件已提取正确的版本,但安装到存储库时,文件夹名称和 jar 名称如下:
m2\repository\com\mytest\test\test-tag\${revision}\test-tag-${revision}.jar
我尝试使用以下方法将“修订”默认为一个值:
<properties>
<revision>1.0.1</revision>
</properties>
但是设置值的常规代码没有效果。
我还为 maven groovy 插件尝试了不同的阶段,但没有运气。我错过了什么吗?谁能帮我解决这个问题?
我想提一下,正如 vatbub 和 StefanHeimberg 提到的那样,我可以使用 versions:set 来设置版本,但这需要我对 GIT 进行额外的提交,这是我试图避免的,想知道我是否可以通过而是写一个 Maven 插件?
【问题讨论】:
-
你试过版本的maven插件吗?它有一个
set目标,可以修改项目版本。 (more info) 但是,afaik,maven 解析 pom,并且在构建过程中对 pom 所做的更改仅在后续构建中考虑。在这种情况下,您需要调用versions:set,然后第二次调用maven 来执行实际的构建。 -
@vatbub 感谢您的及时回复。 groovy 代码决定了版本,所以我需要执行 groovy-maven-plugin,然后获取输出,然后运行 versions:set,然后再运行 mvn install。不知道我该怎么做!
-
正如我所说,Maven 在处理构建期间发生的 pom 更改方面非常糟糕。我想最好的解决方案是创建一个轻量级的命令行工具,它只将所需的版本输出到控制台并将输出通过管道传输到
versions:set -
我最终使用了版本:如你所说的设置。实际上我创建了一个新的插件关闭版本:设置并自定义它以获得最新的标签和其他信息,然后自动设置版本
-
不是 OP 想要的,但这是 possible with Gradle。