我正在尝试解析 Maven 依赖项列表并以更简单的格式显示它。具体来说,我正在尝试转换以下标准 maven xml:
<dependency>
<groupId>groupId1</groupId>
<artifactId>artifactId1</artifactId>
<version>version1</version>
</dependency>
对于这样的事情:
groupId1:artifactId1:version1
我正在使用的 poms 按 sortpom-maven-plugin 排序,因此我可以假设顺序始终为 groupId、artifactId、version;但我不能假设该版本将始终存在,或者不会有其他依赖项配置。
这意味着类似以下的事情是可能的:
<dependency>
<groupId>groupId1</groupId>
<artifactId>artifactId1</artifactId>
<version>version1</version>
</dependency>
<dependency>
<groupId>groupId2</groupId>
<artifactId>artifactId2</artifactId>
</dependency>
<dependency>
<groupId>groupId3</groupId>
<artifactId>artifactId3</artifactId>
<version>version3</version>
</dependency>
<dependency>
<groupId>groupId4</groupId>
<artifactId>artifactId4</artifactId>
<version>version4</version>
<exclusions>
<exclusion>
<groupId>groupId4</groupId>
<artifactId>artifactId4</artifactId>
</exclusion>
</exclusions>
</dependency>
我一直在使用 Notepad++ 尝试用正则表达式解析这些依赖项,我尽最大努力得出了以下结果:
将搜索模式设置为正则表达式并选择匹配换行符时,我搜索以下内容:
.*?<dependency>.*?<groupId>(.*?)</groupId>.*?<artifactId>(.*?)</artifactId>.*?<version>(.*?)</version>.*?</dependency>.*?\R*
并替换为:
\1:\2:\3
不幸的是,这不包括省略版本的情况,因此以下
<dependency>
<groupId>groupId2</groupId>
<artifactId>artifactId2</artifactId>
</dependency>
<dependency>
<groupId>groupId3</groupId>
<artifactId>artifactId3</artifactId>
<version>version3</version>
</dependency>
将转换为:
groupId1:artifactId1:version2
理想情况下,我希望看到它转换成这样的东西:
groupId1:artifactId1
groupId2:artifactId2:version2
有人对改进我的正则表达式有建议吗?它也不一定是单一的常规快车。如果运行顺序表达式最终会将依赖项转换为所需的格式,那就可以了。
最佳答案
第一次替换
查找内容:
<([^>]+)>([^<]+)</\1>(?:(?!\s*</)\s*)?
替换为:
\2:
- 这将导致几乎您想要的结果:
groupId1:artifactId1:version1:
第二次替换
要删除尾随的“:
”,请查找内容:
:\s*?$
替换为:
{leave empty}
关于regex - 如何使用 Notepad++ 解析有或没有声明版本的 Maven 依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484697/