💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Maven - POM 文件 > 原文: [https://howtodoinjava.com/maven/maven-pom-files/](https://howtodoinjava.com/maven/maven-pom-files/) POM(项目对象模型)是一个 XML 文件,其中包含有关项目的信息以及 Maven 用于构建项目的配置详细信息,例如源代码位置,项目依赖项等。此文件必须命名为`pom.xml`,并放置在项目的根文件夹下 。 执行任务或目标时,maven 读取 POM,获取所需的配置信息,然后执行目标。 ```java Table of Contents Super POM Minimal POM Configuration Default POM Configuration POM Hierarchy ``` ## 超级 POM POM 文件在它们之间保持父子关系。 您为项目创建的任何 pom 文件最终都将扩展此超级 pom 文件。 从 maven 2.2 开始,这是**超级 pom 文件内容**。 ```java <project> <modelVersion>4.0.0</modelVersion> <name>Maven Default Project</name> <repositories> <repository> <id>central</id> <name>Maven Repository Switchboard</name> <layout>default</layout> <url>http://repo1.maven.org/maven2</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>Maven Plugin Repository</name> <url>http://repo1.maven.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> <releases> <updatePolicy>never</updatePolicy> </releases> </pluginRepository> </pluginRepositories> <build> <directory>target</directory> <outputDirectory>target/classes</outputDirectory> <finalName>${artifactId}-${version}</finalName> <testOutputDirectory>target/test-classes</testOutputDirectory> <sourceDirectory>src/main/java</sourceDirectory> <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> </testResources> </build> <reporting> <outputDirectory>target/site</outputDirectory> </reporting> <profiles> <profile> <id>release-profile</id> <activation> <property> <name>performRelease</name> </property> </activation> <build> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <updateReleaseInfo>true</updateReleaseInfo> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project> ``` 值得注意的是: * 默认仓库是 Maven 仓库。 * 默认执行目标是“ jar”。 * 默认源代码位置为`src/main/java`。 * 默认测试代码位置为`src/test/java`。 ## 最小的 POM 配置 任何 Maven 项目的最小 POM 配置都非常简单,如下所示: ```java <project> <modelVersion>4.0.0</modelVersion> <groupId>com.howtodoinjava</groupId> <artifactId>MavenExample</artifactId> <version>1.0.0</version> </project> ``` 这个需要: * `project`根 * `modelVersion` – 应设置为 4.0.0 * `groupId` – 项目组的 ID。 * `artifactId` – 工件的 ID(项目) * `version` – 指定组下工件的版本 您需要在此最小 pom 的基础上添加项目特定的配置。 对于其余配置,如果未指定,则 maven 将使用超级 pom 文件中的默认设置。 #### 默认 POM 配置 这取决于您选择的原型类型。 例如,对于快速启动项目,这是默认生成的`pom.xml`文件。 ```java <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.howtodoinjava.demo</groupId> <artifactId>MavenExamples</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>MavenExamples</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> ``` ## POM 层次结构 如前所述,POM 文件在它们之间保持父子关系。 子 POM 文件从其父 POM 文件继承所有配置元素。 这就是 Maven 坚持其设计理念的方式,即相对于配置而言是*约定*。 项目使用的 pom 文件是通过合并项目 pom 文件,父 pom 文件(如果有)和超级 pom 文件而获得的。 此 pom 文件称为**有效 pom** 文件。 要获取项目使用的有效 pom 文件,请在项目的根文件夹中输入以下命令: ```java $ mvn help:effective-pom ``` 例如,有效的 pom 文件默认快速启动项目为: ```java <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.howtodoinjava.demo</groupId> <artifactId>MavenExamples</artifactId> <version>0.0.1-SNAPSHOT</version> <name>MavenExamples</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> </pluginRepository> </pluginRepositories> <build> <sourceDirectory>c:\temp\MavenExamples\src\main\java</sourceDirectory> <scriptSourceDirectory>c:\temp\MavenExamples\src\main\scripts</scriptSourceDirectory> <testSourceDirectory>c:\temp\MavenExamples\src\test\java</testSourceDirectory> <outputDirectory>c:\temp\MavenExamples\target\classes</outputDirectory> <testOutputDirectory>c:\temp\MavenExamples\target\test-classes</testOutputDirectory> <resources> <resource> <directory>c:\temp\MavenExamples\src\main\resources</directory> </resource> </resources> <testResources> <testResource> <directory>c:\temp\MavenExamples\src\test\resources</directory> </testResource> </testResources> <directory>c:\temp\MavenExamples\target</directory> <finalName>MavenExamples-0.0.1-SNAPSHOT</finalName> <pluginManagement> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> </plugin> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.3.2</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>default-clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>default-testResources</id> <phase>process-test-resources</phase> <goals> <goal>testResources</goal> </goals> </execution> <execution> <id>default-resources</id> <phase>process-resources</phase> <goals> <goal>resources</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <executions> <execution> <id>default-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>default-testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> <executions> <execution> <id>default-test</id> <phase>test</phase> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-install</id> <phase>install</phase> <goals> <goal>install</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>default-deploy</id> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.3</version> <executions> <execution> <id>default-site</id> <phase>site</phase> <goals> <goal>site</goal> </goals> <configuration> <outputDirectory>c:\temp\MavenExamples\target\site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </execution> <execution> <id>default-deploy</id> <phase>site-deploy</phase> <goals> <goal>deploy</goal> </goals> <configuration> <outputDirectory>c:\temp\MavenExamples\target\site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </execution> </executions> <configuration> <outputDirectory>c:\temp\MavenExamples\target\site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </plugin> </plugins> </build> <reporting> <outputDirectory>c:\temp\MavenExamples\target\site</outputDirectory> </reporting> </project> ``` 将我的问题放在评论部分。 学习愉快!