在代码中从maven的pom.xml中检索版本号的最简单方法是什么,即以编程方式?
当前回答
有时,当编写与项目版本相关的脚本时,Maven命令行就足够了,例如,通过URL从存储库检索工件:
mvn help:evaluate -Dexpression=project.version -q -DforceStdout
使用的例子:
VERSION=$( mvn help:evaluate -Dexpression=project.version -q -DforceStdout )
ARTIFACT_ID=$( mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout )
GROUP_ID_URL=$( mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout | sed -e 's#\.#/#g' )
curl -f -S -O http://REPO-URL/mvn-repos/${GROUP_ID_URL}/${ARTIFACT_ID}/${VERSION}/${ARTIFACT_ID}-${VERSION}.jar
其他回答
还有一种方法描述了使用Maven显示你的应用版本号:
将此添加到pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>test.App</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
然后用这个:
App.class.getPackage().getImplementationVersion()
我发现这个方法更简单。
公认的答案可能是将版本号静态地放入应用程序的最佳和最稳定的方法,但实际上并没有回答最初的问题:如何从pom.xml检索工件的版本号?因此,我想提供一个替代方案,展示如何在运行时动态地做这件事:
您可以使用Maven本身。更确切地说,您可以使用Maven库。
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.3.9</version>
</dependency>
然后在Java中这样做:
package de.scrum_master.app;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.FileReader;
import java.io.IOException;
public class Application {
public static void main(String[] args) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
System.out.println(model.getId());
System.out.println(model.getGroupId());
System.out.println(model.getArtifactId());
System.out.println(model.getVersion());
}
}
控制台日志如下:
de.scrum-master.stackoverflow:my-artifact:jar:1.0-SNAPSHOT
de.scrum-master.stackoverflow
my-artifact
1.0-SNAPSHOT
更新2017-10-31:为了回答Simon Sobisch的后续问题,我修改了这个例子:
package de.scrum_master.app;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Application {
public static void main(String[] args) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model;
if ((new File("pom.xml")).exists())
model = reader.read(new FileReader("pom.xml"));
else
model = reader.read(
new InputStreamReader(
Application.class.getResourceAsStream(
"/META-INF/maven/de.scrum-master.stackoverflow/aspectj-introduce-method/pom.xml"
)
)
);
System.out.println(model.getId());
System.out.println(model.getGroupId());
System.out.println(model.getArtifactId());
System.out.println(model.getVersion());
}
}
引言:因为我记得在几年前回答了这个问题后,我经常提到这个问题,展示了一个动态访问Maven POM信息的动态版本(例如,也在测试期间),今天我发现了一个类似的问题,涉及从另一个模块B访问模块a的Maven信息。
I thought about it for a moment and spontaneously had the idea to use a special annotation, applying it to a package declaration in package-info.java. I also created a multi-module example project on GitHub. I do not want to repeat the whole answer, so please see solution B in this answer. The Maven setup involves Templating Maven Plugin, but could also be solved in a more verbose way using a combination of resource filtering and adding generated sources directory to the build via Build Helper Maven. I wanted to avoid that, so I simply used Templating Maven.
假设你正在使用Java,你可以:
Create a .properties file in (most commonly) your src/main/resources directory (but in step 4 you could tell it to look elsewhere). Set the value of some property in your .properties file using the standard Maven property for project version: foo.bar=${project.version} In your Java code, load the value from the properties file as a resource from the classpath (google for copious examples of how to do this, but here's an example for starters). In Maven, enable resource filtering. This will cause Maven to copy that file into your output classes and translate the resource during that copy, interpreting the property. You can find some info here but you mostly just do this in your pom: <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
你还可以获取其他标准属性,如project.name, project.description,甚至是你放在pom <properties>中的任意属性,等等。资源过滤与Maven概要文件相结合,可以在构建时为您提供可变的构建行为。当您在运行时使用-PmyProfile指定配置文件时,可以启用属性,然后可以在构建中显示这些属性。
为了补充@kieste所发布的内容,我认为如果使用Spring-boot,这是在代码中提供Maven构建信息的最佳方式:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info上的文档非常有用。
您只需要激活执行器,并在应用程序中添加所需的属性。属性或application.yml
Automatic property expansion using Maven
You can automatically expand info properties from the Maven project using resource filtering. If you use the spring-boot-starter-parent you can then refer to your Maven ‘project properties’ via @..@ placeholders, e.g.
project.artifactId=myproject
project.name=Demo
project.version=X.X.X.X
project.description=Demo project for info endpoint
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@
推荐文章
- JavaBean和POJO之间的区别是什么?
- 注释在Java中如何使用,在哪里使用?
- 如何在Ubuntu下安装JDK 11 ?
- Spring Boot -无法确定数据库类型为NONE的嵌入式数据库驱动程序类
- 如何转换/解析从字符串到字符在java?
- Java编译器级别与安装的Java项目facet的版本不匹配
- 如何在Android中动态更改菜单项文本
- 如何比较两个没有时间部分的日期?
- 如何在Java中找到给定类的所有子类?
- 匿名类的访问构造函数
- 从Java访问Kotlin扩展函数
- 解析LocalDateTime时无法从TemporalAccessor获取LocalDateTime (Java 8)
- 以AM/PM的12小时格式显示当前时间
- 求两个集合的差值
- Junit @Rule如何工作?