之前我发布了一个关于如何从命令行更改Maven项目版本的问题,这引出了一个新问题.
以前我能够获得版本号,因为版本存储为易于grep并从命令行解析的属性(bash).现在pom.xml元素已用于此,它不再是唯一的,因为所有依赖项和其他一些也使用它.我认为没有办法使用bash脚本获取当前版本号,而无需外部工具来解析xml或一些非常上下文的sed命令.
我认为最干净的解决方案是让Maven分发这个版本信息.我正在考虑编写一个自定义maven插件来检索不同的属性,但我想我先问这里.
那么,有没有简单的方法来获得${project.version}命令行的价值?提前致谢.
解
感谢您的帮助.我不得不cd手动到目录,但这可以很容易地完成.在我的bash脚本中我有
version=`cd $project_loc && mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | sed -n -e '/^\[.*\]/ !{ /^[0-9]/ { p; q } }'`
Run Code Online (Sandbox Code Playgroud)
这给了我当前版本,然后我可以推进.Grepping可能更简单,但我认为我想尽可能健壮,所以我对以数字开头的第一行感到满意,并尝试将其作为版本号处理.
# Advances the last number of the given version string by one.
function advance_version () {
local v=$1
# Get the last number. First remove any suffixes (such as '-SNAPSHOT').
local cleaned=`echo $v | sed -e 's/[^0-9][^0-9]*$//'`
local last_num=`echo $cleaned | sed -e 's/[0-9]*\.//g'`
local next_num=$(($last_num+1))
# …Run Code Online (Sandbox Code Playgroud) 我有一个使用Spring Framework和Git的Java项目,我想显示一个内部版本号.我找到了Build Number Maven插件.使用Git,内部版本号是一个Git哈希.我不喜欢这个,我认为约会更有表现力.
我发现这篇优秀的博客文章解释了如何使用不同的SVN和Git配置文件编号插件.由于我只使用Git,而不是创建新的配置文件,我只是在我的构建标记中复制了插件部分.
当我运行"mvn package"时,它告诉我:
[INFO] --- buildnumber-maven-plugin:1.0:create (default) @ sherd ---
[INFO] Storing buildNumber: 2011-08-04_21-48_stivlo at timestamp: 1312487296631
Run Code Online (Sandbox Code Playgroud)
哪个看起来不错,但我想知道,它存放在哪里?"git status"没有检测到任何新文件,它似乎不在目标/中(目标/在我的.gitignore中).
也许我要更改配置以将内部版本号存储在文件中?如何使用内部版本号值?
感谢Michael-O的提示,我在Maven入门指南中阅读了有关如何过滤资源文件的章节.我在src/main/resources/properties/application.properties中创建了一个文件application.properties,其中包含以下内容:
# application properties
application.name=${pom.name}
application.version=${pom.version}
application.build=${buildNumber}
Run Code Online (Sandbox Code Playgroud)
我在构建部分中添加了以下XML代码段:
<resources>
<resource>
<directory>src/main/resources/properties</directory>
<filtering>true</filtering>
</resource>
</resources>
Run Code Online (Sandbox Code Playgroud)
现在,当我从命令行"mvn package"调用时,此属性文件将保存在target/classes/properties/application.properties中,例如具有以下内容:
# application properties
application.name=Sherd Control Panel
application.version=1.0.1-SNAPSHOT
application.build=2011-08-05_05-55_stivlo
Run Code Online (Sandbox Code Playgroud)
从命令行一切正常,但是,叹息,m2eclipse给出了Build错误:
05/08/11 6.05.03 CEST: Build errors for obliquid-cp;
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal
org.codehaus.mojo:buildnumber-maven-plugin:1.0:create (default) on project
sherd: Cannot get the branch information from …Run Code Online (Sandbox Code Playgroud) 这是一种将项目版本编写到文件中的简单过滤方法.
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<includes>
<include>**/*.version</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
Run Code Online (Sandbox Code Playgroud)
这是项目结构(省略了无关紧要的部分)
????src
? ????main
? ????java
? ? ???? [...]
? ????webapp
? ????META-INF
? ????WEB-INF
? ????cfg
? ????portal.version
???? pom.xml
Run Code Online (Sandbox Code Playgroud)
的内容 portal.version
${project.version}
Run Code Online (Sandbox Code Playgroud)
这应该替换为神器版本pom.xml,但遗憾的是没有任何反应.怎么了?先感谢您
如何在运行Tomcat时获取软件包版本?
我尝试getClass().getPackage().getImplementationVersion()但这总是返回null.我想这是因为我的战争尚未打包,Tomcat执行.classes(又名爆炸战争).
META-INF\MANIFEST.MF在最后的战争中存在,但不在target\<project.name>\META-INF文件夹中
包对象包含有关Java包的实现和规范的版本信息.检索此版本控制信息,并由加载类的ClassLoader实例提供.通常,它存储在随类分发的清单中.
UPDATE.在我已经为war build添加配置之前.但是当我从Eclipse运行Tomcat时,我得到了null.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
<!-- this actually moves classes from \WEB-INF\classes to new jar
<archiveClasses>true</archiveClasses>
-->
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud) 想出一个有意义的标题有点困难,希望解释后它会变得足够清楚。我在 SO 上搜索了许多 Q 和 As,它们都非常接近我遇到的问题,但仍然不够接近。
一般来说,我想要完成的是通过@project.version@从.csvLiquibase 脚本加载的文件访问 maven 属性来将项目版本存储在 DB 中。我的 Maven 项目结构如下所示:
parentModule
pom.xml
|
---moduleA
|__pom.xml
---moduleB
|__pom.xml
---moduleC
|__pom.xml
...
Run Code Online (Sandbox Code Playgroud)
Pom.xml 定义为:
**PARENT POM**
<project ...>
<groupId>com.parent</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>parent</name>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.1.RELEASE</version>
<relativePath />
</parent>
<properties>
<java.version>8</java.version>
</properties>
<modules>
<module>moduleA</module>
<module>moduleB</module>
<module>moduleC</module>
...
</modules>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
----------------------------------------------------------------------
**CHILD POM**
<project ...>
<artifactId>moduleC</artifactId>
<name>moduleC</name>
<parent>
<groupId>com.parent</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent> …Run Code Online (Sandbox Code Playgroud) 我在github上维护一个开源项目,其中包含README.md包含当前版本项目的安装说明的文件1.0.0-SNAPSHOT.
将新版本发布到maven central时,是否可以自动更新此中包含的版本号README.md?
版本执行时maven-release-plugin,pom.xml中的版本已更新,但我在文档中找不到任何正确更新此外部文件的内容.
例:
README.md文件目前在1.0.0-SNAPSHOT.它位于maven源/资源之外,但它是在git上管理的.on mvn release:prepare,它应该更新为1.0.0,然后maven应该在标记新版本之前提交/推送更改.然后,mvn release:perform它应该转到下一个开发版本1.0.1-SNAPSHOT.
(链接到项目)
我在我的maven构建中使用antrun插件来替换某些JSP文件中的令牌@ version @和应用程序版本.这就是我在做的事情:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<target>
<echo>${displayVersion}</echo>
<replace file="src/main/webapp/admin/decorators/default.jsp" token="@version@" value="${displayVersion}"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我将displayVersion作为参数传递给maven
mvn clean install -DdisplayVersion="Version-1.1"
这是Antrun插件的控制台输出
[INFO] [antrun:run {execution: default}]
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
main:
[echo] 9.4_70
[INFO] Executed tasks
Run Code Online (Sandbox Code Playgroud)
尽管该属性正在被正确回显,但它并没有在我的JSP中替代.在@ @版本令牌被替换{} displayVersion,而不是它的实际值.
maven ×5
java ×3
maven-2 ×2
build ×1
command-line ×1
eclipse ×1
liquibase ×1
manifest.mf ×1
maven-plugin ×1
tomcat ×1