Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式获取项目的Maven版本_Java_Maven - Fatal编程技术网

Java 以编程方式获取项目的Maven版本

Java 以编程方式获取项目的Maven版本,java,maven,Java,Maven,如何以编程方式获得项目的Maven版本 换言之: static public String getVersion() { ...what goes here?... } 例如,如果我的项目将生成jarCalculatorApp-1.2.3.jar,我希望getVersion()在src/main/resources中返回1.2.3创建文件version.prop,内容如下: version=${project.version} 将以下内容添加到项目的pom中: <build&g

如何以编程方式获得项目的Maven版本

换言之:

static public String getVersion()
{
    ...what goes here?...
}

例如,如果我的项目将生成jar
CalculatorApp-1.2.3.jar
,我希望
getVersion()
src/main/resources
中返回
1.2.3

创建文件
version.prop
,内容如下:

version=${project.version}
将以下内容添加到项目的pom中:

<build>
...
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/version.prop</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/version.prop</exclude>
            </excludes>
        </resource>
    </resources>
...
</build>

p、 对于jar文件,您可以将MANIFEST.MF作为默认位置来放置实现版本。Maven支持像这样构建jar文件

另见

你是指插件还是应用本身?你打算对maven版本做什么?如果需要在构建过程中将信息包含在某个文件中,那么您可以利用提供maven版本的。如果你想得到你项目的版本(而不是像我之前评论的maven版本),那就看一看。内容包括:抱歉,我做了几次搜索,但没有找到那个。还投票以dup形式关闭。为什么第二个资源定义的筛选设置为false?@demaniak第一个只复制version.properties并对其进行筛选,第二个复制version.properties以外的所有内容,而不进行筛选。
public String getVersion()
{
    String path = "/version.prop";
    InputStream stream = getClass().class.getResourceAsStream(path);
    if (stream == null)
        return "UNKNOWN";
    Properties props = new Properties();
    try {
        props.load(stream);
        stream.close();
        return (String) props.get("version");
    } catch (IOException e) {
        return "UNKNOWN";
    }
}