前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊如何解析pom文件

聊聊如何解析pom文件

原创
作者头像
code4it
发布2023-08-23 09:23:54
2120
发布2023-08-23 09:23:54
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下如何解析pom文件

maven-model

maven提供了maven-model的类库可以直接解析

代码语言:javascript
复制
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-model</artifactId>
			<version>3.9.4</version>
		</dependency>

使用

代码语言:javascript
复制
        MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
        Model model = xpp3Reader.read(new ByteArrayInputStream(data));
        Properties properties = model.getProperties();

使用MavenXpp3Reader可以直接读取pom文件,之后就可以得到Model

Model

maven-model-3.9.4-sources.jar!/org/apache/maven/model/Model.java

代码语言:javascript
复制
public class Model extends ModelBase implements Serializable, Cloneable {
    private String modelVersion;
    private Parent parent;
    private String groupId;
    private String artifactId;
    private String version;
    private String packaging = "jar";
    private String name;
    private String description;
    private String url;
    private String childProjectUrlInheritAppendPath;
    private String inceptionYear;
    private Organization organization;
    private List<License> licenses;
    private List<Developer> developers;
    private List<Contributor> contributors;
    private List<MailingList> mailingLists;
    private Prerequisites prerequisites;
    private Scm scm;
    private IssueManagement issueManagement;
    private CiManagement ciManagement;
    private Build build;
    private List<Profile> profiles;
    private String modelEncoding = "UTF-8";
    private File pomFile;

    //......
}    

Model继承了ModelBase

ModelBase

maven-model-3.9.4-sources.jar!/org/apache/maven/model/ModelBase.java

代码语言:javascript
复制
public class ModelBase implements Serializable, Cloneable, InputLocationTracker {
    private List<String> modules;
    private DistributionManagement distributionManagement;
    private Properties properties;
    private DependencyManagement dependencyManagement;
    private List<Dependency> dependencies;
    private List<Repository> repositories;
    private List<Repository> pluginRepositories;
    private Object reports;
    private Reporting reporting;
    private Map<Object, InputLocation> locations;
    private InputLocation location;
    private InputLocation modulesLocation;
    private InputLocation distributionManagementLocation;
    private InputLocation propertiesLocation;
    private InputLocation dependencyManagementLocation;
    private InputLocation dependenciesLocation;
    private InputLocation repositoriesLocation;
    private InputLocation pluginRepositoriesLocation;
    private InputLocation reportsLocation;
    private InputLocation reportingLocation;

    //......
}    

ModelBase定义了诸如properties、dependencyManagement、dependencies等

小结

maven提供了maven-model可以直接解析pom,它内置了对pom文件的model,可以用来快速分析依赖等。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • maven-model
  • 使用
    • Model
      • ModelBase
      • 小结
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档