Package types for projects: POM, JAR, WAR

Specify the package type to use the label, which defaults to jar type.

  • Pom: Both parent types are POM types
<packaging>pom</packaging>
Copy the code
  • Jar: Internal invocation or for use as a service
<packaging>jar</packaging>
Copy the code
  • War: Package the project for deployment on containers (Tomcat, Jetty, etc.)
<packaging>war</packaging>  
Copy the code

Here’s an example of a package type of POM:

The project directory structure is as follows:

~ / Desktop $tree - L 4 ├ ─ ─ MyProject │ ├ ─ ─ pom. The XML │ ├ ─ ─ SubProject1 │ │ └ ─ ─ pom. The XML │ ├ ─ ─ SubProject2 │ │ └ ─ ─ pom. The XML │ ├ ─ ├ ─ sci-1.txt...Copy the code

There are three module projects SubProject1, SubProject2, and SubProject3 under MyProject. So we can write the common parts of the three module projects in the MyProject project’s pom.xml file, and then inherit it in the module project’s pom.xml, so that the module project can use the common parts. The MyProject project’s pom.xml is what we call the parent type, and its package type should be written as POM, as in:

    <project .>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.wong.tech</groupId>
      <artifactId>myproject</artifactId>
      <packaging>pom</packaging>
      <version>1.0.0</version>
      <name>myproject</name>
      <url>http://maven.apache.org</url>
      <! Modules (sometimes called subprojects) are built as part of a project. Each module element listed is a relative path to the directory pointing to that module -->
      <modules>
            <module>SubProject1</module>
            <module>SubProject2</module>
            <module>SubProject3</module>
      </modules>.</project>
Copy the code

Pom.xml under MyProject specifies the relative path of the subproject through the tag. This allows you to execute the MVN command directly in MyProject and build all the modules at once. Of course, it’s ok to go to the directory of each module and run the MVN command and build each module individually.

Pom.xml under submodule (subproject) inherits pom.xml under MyProject with tags, such as pom.xml under SubProject1:

    <project
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
            xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <modelVersion>4.0.0</modelVersion>
            <artifactId>myproject-subproject1</artifactId>
            <packaging>jar</packaging>
            <name>myproject-subproject1</name>
            <version>1.0.0</version>
            <url>http://maven.apache.org</url>
            <parent>
                    <groupId>com.wong.tech</groupId>
                    <artifactId>myproject</artifactId>
                    <version>1.0</version>
                    <relativePath>../pom.xml</relativePath>
            </parent>.</project>
Copy the code

Other subprojects follow suit.

Thanks for reading.