简体   繁体   English

Java程序以在gradle构建文件中获取版本号

[英]Java program to get the version number in the gradle build file

I am writing a small java program which will parse the Gradle build file to get the version. 我正在编写一个小的Java程序,它将解析Gradle构建文件以获取版本。

Needed the java regex pattern to match the line starting with "version 'xxxxxxxxx'" in the Gradle build file. 需要Java regex模式以匹配Gradle构建文件中以“版本'xxxxxxxxx'”开头的行。 The following is the sample Gradle build content and I need to extract the "'1.0-SNAPSHOT'" version detail 以下是示例Gradle构建内容,我需要提取“'1.0-SNAPSHOT'”版本详细信息

group 'com.shan.sample.gradle'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'maven'

sourceCompatibility = 1.8


mainClassName = 'com.shan.sample.gradle.SampleProgram'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

I cannot yet post comments so: 我还不能发表评论,所以:

Be aware that shakhawat answer can lead to small mismatches, it will match any version '123abc' in the file so if it happens that you have 请注意,shakhawat答案可能会导致较小的不匹配,它将与文件中的任何版本“ 123abc”匹配,因此,如果碰巧

somethingversion '456xyz'
version '123abc'

it will take the first one (assuming you'd only take first found match). 它将获得第一个(假设您只会进行第一个找到的比赛)。 To counter this in some part you can add ^\\s* in the beggining of regex like this: 为了在某​​种程度上解决这个问题,您可以像这样在正则表达式的开头添加^ \\ s *:

^\s*(version)\s*([A-Z0-9.'/-]+)

^ matches the start of line ^与行首匹配

\\s matches any whitespace character \\ s匹配任何空格字符

* is greedy matching so it will continue to match all whitespace characters in this case *是贪婪匹配,因此在这种情况下它将继续匹配所有空白字符

so you can have as much whitespaces between version and version number and between start of line and version string in case of some lost whitespaces or similiar names put before. 因此,如果之前丢失了一些空格或类似名称,则版本和版本号之间以及行首和版本字符串之间可以有尽可能多的空格。

Also, using gradle API ( https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html ) you could create gradle simple gradle plugin that can report version via 另外,使用gradle API( https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html ),您可以创建gradle简单的gradle插件,该插件可以通过以下方式报告版本

project.version

which you can apply for each project and then run it from outside. 您可以申请每个项目,然后从外部运行它。

I would still recommend to parse the file by line, and pick the second word of the line, where the first word is version . 我仍然建议按行分析文件,并选择该行的第二个单词,其中第一个单词是version

However, this below implementation will serve your purpose in the regex way. 但是,下面的这种实现将以正则表达式的方式满足您的目的。 The regex (version ) ([A-Z0-9.'/-]+) has two groups, first one will capture the word version and the second group will capture the version number. 正则表达式(version ) ([A-Z0-9.'/-]+)有两个组,第一个将捕获单词version ,第二个将捕获版本号。 So, just get the version number. 因此,只需获取版本号。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {

        String gradleFileContent = "group 'com.shan.sample.gradle'\n" +
                "version '1.0-SNAPSHOT'\n" +
                "\n" +
                "apply plugin: 'java'\n" +
                "apply plugin: 'application'\n" +
                "apply plugin: 'maven'\n" +
                "\n" +
                "sourceCompatibility = 1.8\n" +
                "\n" +
                "\n" +
                "mainClassName = 'com.shan.sample.gradle.SampleProgram'\n" +
                "\n" +
                "dependencies {\n" +
                "    testCompile group: 'junit', name: 'junit', version: '4.12'\n" +
                "}\n";

        Pattern p = Pattern.compile("(version )([A-Z0-9.'/-]+)");
        Matcher m = p.matcher(gradleFileContent);

        if (m.find()) {
            System.out.println(m.group(2));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM