我正在尝试编写一个正则表达式模式来解析 pom 文件中的 Maven 坐标。
[groupId]:[artifactId]:[type]:[?optional_field]:[version]:[compile]
1. org.eclipse.aether:aether-impl:jar:0.9.0.M2:compile
2. com.google.code.findbugs:annotations:jar:3.0.0:compile
3. org.sonatype.sisu:sisu-guice:jar:no_aop:3.1.0:compile
上面是 Maven 坐标的一些示例,请注意,1 和 2 有一个共同的模式,但 3 有一个额外的可选坐标
我需要一个正则表达式模式来仅提取 groupId、artifactId 和版本
任何人都可以建议一种适用于所有三种情况的适当模式
最佳答案
我会使用aether-api
库,因为它是 Maven 部分所基于的库。它可以安全地解析坐标并且经过充分测试。
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>1.1.0</version>
</dependency>
例如
public static void main(String[] args) {
DefaultArtifact sisuJar = new DefaultArtifact("org.sonatype.sisu:sisu-guice:jar:no_aop:3.1.0");
System.out.println(sisuJar.getGroupId());
System.out.println(sisuJar.getArtifactId());
System.out.println(sisuJar.getVersion());
System.out.println(sisuJar.getClassifier());
System.out.println(sisuJar.getExtension());
}
将输出:
org.sonatype.sisu
sisu-guice
3.1.0
no_aop
jar
有关 eclipse aether 的更多信息 here .
PS:您提供的坐标无效。如果 Maven 尝试解析例如org.sonatype.sisu:sisu-guice:jar:no_aop:3.1.0:compile
它说:
java.lang.IllegalArgumentException: Bad artifact coordinates
org.sonatype.sisu:sisu-guice:jar:no_aop:3.1.0:compile,
expected format is
<groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
at org.eclipse.aether.artifact.DefaultArtifact.<init>(DefaultArtifact.java:68)
at org.eclipse.aether.artifact.DefaultArtifact.<init>
关于java - 解析 Maven 坐标的正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48858179/