父项目POM定义

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>privs.nb.study.maven</groupId>
    6. <artifactId>ParentModule</artifactId>
    7. <version>0.0.1-SNAPSHOT</version>
    8. <name>ParentModule</name>
    9. <description>ParentModule parent</description>
    10. <packaging>pom</packaging>
    11. <properties>
    12. <java.version>8</java.version>
    13. </properties>
    14. <modules>
    15. <module>SubModule</module>
    16. </modules>
    17. <build>
    18. <plugins>
    19. <plugin>
    20. <groupId>org.apache.maven.plugins</groupId>
    21. <artifactId>maven-compiler-plugin</artifactId>
    22. <version>3.8.1</version>
    23. <configuration>
    24. <source>1.8</source>
    25. <target>1.8</target>
    26. <encoding>UTF-8</encoding>
    27. </configuration>
    28. </plugin>
    29. </plugins>
    30. </build>
    31. </project>

    子项目POM定义

    • 使用parent标签引入parent项目的pom文件

      1. <?xml version="1.0" encoding="UTF-8"?>
      2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      4. <modelVersion>4.0.0</modelVersion>
      5. <groupId>privs.nb.study.maven</groupId>
      6. <artifactId>SubModule</artifactId>
      7. <version>0.0.1-SNAPSHOT</version>
      8. <name>SubModule</name>
      9. <description>ParentModule parent</description>
      10. <parent>
      11. <groupId>privs.nb.study.maven</groupId>
      12. <artifactId>ParentModule</artifactId>
      13. <version>0.0.1-SNAPSHOT</version>
      14. </parent>
      15. <properties>
      16. <java.version>8</java.version>
      17. </properties>
      18. </project>
    • 使用import引入parent项目文件

      1. <?xml version="1.0" encoding="UTF-8"?>
      2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      4. <modelVersion>4.0.0</modelVersion>
      5. <groupId>privs.nb.study.maven</groupId>
      6. <artifactId>SubModule</artifactId>
      7. <version>0.0.1-SNAPSHOT</version>
      8. <name>SubModule</name>
      9. <properties>
      10. <java.version>8</java.version>
      11. </properties>
      12. <dependencyManagement>
      13. <dependencies>
      14. <dependency>
      15. <groupId>privs.nb.study.maven</groupId>
      16. <artifactId>ParentModule</artifactId>
      17. <version>0.0.1-SNAPSHOT</version>
      18. <type>pom</type>
      19. <scope>import</scope>
      20. </dependency>
      21. </dependencies>
      22. </dependencyManagement>
      23. </project>

    Parent标签和import的在于使用import时父pom中由dependencyManagement引用的依赖无法引入子项目