这次弄一下maven 多模块项目,用vscode新建一下,便于管理项目

发布时间 2023-10-31 19:09:35作者: 吃了上顿吃下顿

首先 创建一个mvn项目, 直接在命令行执行, 原型生成:

mvn archetype:generate

选一个maven quick start的template, 然后删除src和target文件夹

在pom.xml里面version 下面加上<packing>pom</packing>

在此目录中再次执行mvn archetype:generate, 构件artifactId选为child1, 完成后自动在mvnparent目录的Pom中添加了<modules> 节点

再次在mvnparent目录执行mvn archetype:generate 生成child2项目, 会在mvnparent的pom.xml中添加child2的module, 把里面没用的build 节点都删掉

<?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>mvnparent</artifactId>
    <groupId>org.caloch</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>child2</artifactId>
  <packaging>jar</packaging>

  <name>child2</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>



  
</project>

mvnchild1引用了child2项目的pom.xml, 里面删除groupid和version两个,它们会继承parent, packaging改为jar

<?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>mvnparent</artifactId>
    <groupId>org.caloch</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>mvnchild1</artifactId>
  <packaging>jar</packaging>

  <name>mvnchild1</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.caloch</groupId>
      <artifactId>child2</artifactId>
      <version>${project.version}</version>
    </dependency>


  </dependencies>

  
</project>

 

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>org.caloch</groupId>
  <artifactId>mvnparent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>mvnparent</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

<modules>  <module>mvnchild1</module>
    <module>child2</module>
  </modules>
</project>