2014-11-03 59 views
0

如何解析maven文件名到工件和版本中?解析Maven文件名

的文件名看起来像这样:

test-file-12.2.2-SNAPSHOT.jar 
test-lookup-1.0.16.jar 

我需要得到

test-file 
12.2.2-SNAPSHOT 
test-lookup 
1.0.16 

所以artifactId的是一个破折号和一个号码和版本的第一个实例之前文本的文本在数字的第一个实例之后达到.jar。

我大概可以做到这一点与分裂和几个循环和检查,但它感觉应该有一个更简单的方法。

编辑:

实际上,正则表达式并不复杂,因为我想!

new File("test").eachFile() { file -> 
    String fileName = file.name[0..file.name.lastIndexOf('.') - 1] 
    //Split at the first instance of a dash and a number 
    def split = fileName.split("-[\\d]") 
    String artifactId = split[0] 
    String version = fileName.substring(artifactId.length() + 1, fileName.length()) 

    println(artifactId) 
    println(version) 
    } 

编辑2:嗯。它失败上的例子,如本:

http://mvnrepository.com/artifact/org.xhtmlrenderer/core-renderer/R8 
core-renderer-R8.jar 

回答

1

基本上其只是本^(.+?)-(\d.*?)\.jar$
在多行模式中使用,如果有多于一个的线。

^
(.+?) 
- 
(\d .*?) 
\. jar 
$ 

输出:

** Grp 0 - (pos 0 , len 29) 
test-file-12.2.2-SNAPSHOT.jar 
** Grp 1 - (pos 0 , len 9) 
test-file 
** Grp 2 - (pos 10 , len 15) 
12.2.2-SNAPSHOT 

-------------------------- 

** Grp 0 - (pos 31 , len 22) 
test-lookup-1.0.16.jar 
** Grp 1 - (pos 31 , len 11) 
test-lookup 
** Grp 2 - (pos 43 , len 6) 
1.0.16 
+0

让我想起了什么,我需要做的。谢谢! – opticyclic 2014-11-03 21:40:22