2014-11-03 58 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