繁体   English   中英

Maven pom 文件,它在做什么?

[英]Maven pom file, what is it doing?

我刚刚开始以新手身份使用 Maven,只是想了解一些有关依赖项的内容。 我正在尝试使用 iText 和 pdf 输出功能构建一个微型网络服务。 所以我的第一步是看看我是否可以从一个非常简单的 Java 程序中获得 pdf 输出。

在我的 pom 文件中,我有以下依赖项:

<!-- iText Core -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext7-core</artifactId>
    <version>${itext.version}</version>
    <type>pom</type>
</dependency>

<!-- iText pdfHTML add-on -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</artifactId>
    <version>2.1.6</version>
  </dependency>

在阅读了 Maven 站点上的信息后,pom 文件应该完成获取依赖项的所有繁重工作,这是我有点困惑的一点。 pom 文件是否会在应用程序启动时将文件物理下载到应用程序位置,以便应用程序可以使用这些文件?

如果是这样的话,它似乎没有这样做,所以我是否在 pom 文件中遗漏了一些东西来启用它?

完整的 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>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>my-app</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>
    <itext.version>RELEASE</itext.version>
  </properties>

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

    <!-- iText Core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>${itext.version}</version>
        <type>pom</type>
    </dependency>

    <!-- iText pdfHTML add-on -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>2.1.6</version>
      </dependency>   
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

任何帮助表示赞赏。 谢谢

当maven build执行时,Maven会自动将所有的依赖jar包下载到本地仓库中。

Maven 的本地存储库是开发人员机器上的一个文件夹位置,所有项目工件都存储在本地。

通常这个文件夹被命名为.m2

以下是此文件夹的默认路径 - 基于操作系统:

Windows : C:\\Users\\User_Name\\ .m2

Linux :/home/User_Name/.m2

Mac :/用户/用户名/.m2

https://www.baeldung.com/maven-local-repository

Maven 确实将依赖项下载到本地 m2 存储库。 但这更多的是用于构建应用程序,而不是用于运行。

你想要的(复制输出jar旁边的依赖项)可以通过目标依赖来实现:copy-dependencies

请参阅此博客文章:

https://technology.amis.nl/2017/02/09/download-all-directly-and-indirectly-required-jar-files-using-maven-install-dependencycopy-dependencies/

管理依赖项是 Maven 的关键特性之一。

依赖管理:可以定义对其他项目的依赖。 在构建过程中,Maven 构建系统会解析依赖关系,并在需要时构建依赖项目。

解决依赖关系意味着它会下载本地系统中所有指定的 jar。

Maven 工具读取 pom 文件并解析项目的依赖项。 Maven 验证本地存储库中是否有所需的组件。 本地存储库位于用户主目录的 .m2/repository 文件夹中。

请注意,.m2/ 是一个隐藏文件夹。 如果您使用的是 Linux,将是这个路径 /home/someuser/.m2

读这个

然而,如果它根本没有下载 jars 或创建 .m2 目录,那么要么你没有正确构建项目,要么你没有连接到互联网。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM