开发者

Maven中dependency和plugins的继承与约束

目录
  • Maven的父子项目
  • 父项目配置
  • 子项目定义
  • dependencies
  • dependencyManagement
  • pluginManagement
  • 总结

Maven的父子项目

父子项目核心点是在于通过将一个大项目拆分为若干子模块,每个模块以子项目的形式存在,不同的子项目共享父项目的设置与约束。

所以,父项目承担的角色是建立各个子项目的约束和一致的基础。

父项目配置

<project XMLns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.parent.project</groupId>
    <artifactId>project-artifiact</artifactId>
    <version>0.0.1.7-SNAPSHOT</version>
    <packaging>pom</packaging>
 
   <!-- dependency in inheritance -->
   <depdencies>
     <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifacpythontId>
          <version>3.8.1</version>
      </dependency>
   </dependencies>

  <!-- depdency constraints -->
   </dependencies>
   <dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactid>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>log4j</grouhttp://www.devze.compId&编程客栈gt;
     http://www.devze.com <artifactid>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
  </dependencies>
</dependencyManagement>

<!-- plugin constraints -->
<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <attach>true</attach>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</pluginManagement>

<modules>
   <module>A</module>
   <module>B</module>
</modules>

在父项目中,其packaging值设置为pom,表示其定义为定义和描述项目的结构,而非真实的项目。

子项目定义

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>parent-project</artifactId>
        <groupId>com.parent.project</groupId>
        <version>0.0.1.7-SNAPSHOT</version>
    </parent>
    <artifactId>child-project</artifactId>
    <version>0.0.1.7-SNAPSHOT</version>
    <packaging>jar</packaging>
    ...........

dependencies

在上述示例中,定义了dependencies的节点,这个节点中定义的dependency将被其子项目继承,可以在子项目默认加载进来。

dependencyManagement

在此节点中定义的dependency对于子项目而言,有版本上的约束,在子项目中,如果有指定版本,则默认使用父项目中约定的版本。

示例:

 <dependency>
  <groupId>junit</groupId>
  <artifactid>junit</artifactId>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactid>log4j</artifactId>
</dependency>

其版本号默认使用父项目的版本号

pluginManagement

在此节点中定义的dependency对于子项目而言,有版本上的约束,在子项目中,如果有指定版本,则默认使用父项目中约定的版本。

示例:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-sour编程客栈ce-plugin</artifactId>
  </plugin>
</plugins>

其默认使用父项目中规定的版本号。当然在子项目中也可以覆盖父项目中的版本约定,自行指定所需要的版本号。

总结

在父子开发者_JS开发项目结构中,子项目以modules的形式在父项目中注册,子项目实施具体的实现功能。

对于不同的子项目共享父项目中的设置与约束,方便团队开发。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜