乐趣区

关于maven:聊聊如何解析pom文件

本文次要钻研一下如何解析 pom 文件

maven-model

maven 提供了 maven-model 的类库能够间接解析

        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-model</artifactId>
            <version>3.9.4</version>
        </dependency>

应用

        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

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

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,能够用来疾速剖析依赖等。

退出移动版