Maven 中的依赖

发布于 2024-12-19 04:01:08 字数 525 浏览 0 评论 0原文

我对maven真的很陌生。我对依赖功能有点困惑。我知道我可以像这样在 pom 文件中添加依赖项

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.1</version>
</dependency>

这实际上意味着什么?这是否意味着我不需要将 slf4j jar 文件导入到我的项目中?如果是这样,我的项目如何访问这些库?

我已经从 maven site 阅读了有关依赖项的信息,但没有帮助我很喜欢。

有人可以用更简单的方式解释一下吗?

谢谢

I am really new to maven. I am bit confused about the dependency feature. I know that I can add dependency in the pom file like this

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.1</version>
</dependency>

What does this actually mean? Does it mean that I dont need to import the slf4j jar files into my project? If so how does my project get access to those libraries?

I have read about dependency from maven site but didnt help me much.

Can some one explain it in a simpler way.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

著墨染雨君画夕 2024-12-26 04:01:08

简而言之:这意味着您的项目依赖于 slf4j 版本 1.6.1。

此外:

  • 如果您使用 Maven 构建项目(或者您的 IDE 支持 Maven),则无需执行任何其他操作即可使用 slf4j。 (除了正常的源代码考虑因素,例如合理的 import 语句等)
  • slf4j v.1.6.1 将从默认 Maven 存储库检索到本地存储库,这意味着
  • ...... .~/.m2/repository 是您的存储库。 slf4j 将被放入 $M2_HOME/org/slf4j/$(artifactId}/1.6.1 中,并且通常会包含一个 jar 文件、一个 pom 文件和一个哈希文件。Slf4j
  • 依赖项也将被下载到您的本地存储库中。
  • 这些依赖项的依赖项将被无限下载/令人作呕(来源)。如果有很多依赖项,“第一次使用库就会下载互联网”,这就是笑话;而 slf4j 则不然。)这就是“传递依赖管理”——Maven 的最初目的之一。

Nutshell: It means your project has a dependency on slf4j, version 1.6.1.

Furthermore:

  • If you build your project with Maven (or your IDE is Maven-aware), you don't have to do anything else in order to use slf4j. (Aside from normal source-code considerations, like a reasonable import statement, etc.)
  • slf4j v. 1.6.1 will be retrieved from a default Maven repository to your local repository, meaning...
  • ... ~/.m2/repository is your repository. slf4j will be put in $M2_HOME/org/slf4j/$(artifactId}/1.6.1 and will include (in general) a jar file, a pom file, and a hash file.
  • Slf4j's dependencies will be downloaded into your local repository as well.
  • Dependencies of those dependencies will be downloaded ad infinitum/ad nauseum. (The source of "first use of a library downloads the internet" jokes if there are a lot of dependencies; not the case for slf4j.) This is "transitive dependency management"--one of Maven's original purposes.
东风软 2024-12-26 04:01:08

如果您没有使用 Maven,则需要手动下载并使用项目所需的依赖项。您可能会将它们放在 lib 文件夹中,并在 IDE 和构建工具中指定此位置。

maven 为您管理这些依赖项。你以规定的格式指定你的项目需要的依赖关系,maven会为你从互联网上下载它们并管理它们。当构建项目时,maven 知道它在哪里放置了这些依赖项并使用它们。大多数 IDE 在发现这是一个 Maven 项目时也知道这些依赖项在哪里。

为什么这很重要?通常,大多数开源库都会定期发布新版本。如果您的项目使用这些,那么每次需要更新版本时,您都需要手动下载并管理它。更重要的是,每个依赖项又可能具有其他依赖项(称为传递依赖项)。如果您不使用 Maven,您还需要识别、下载和管理这些传递依赖项。

您的项目使用的此类依赖项越多,它就会变得复杂。两个依赖项最终可能会使用它们共同的依赖项的不同版本。

If you were not using maven, you would manually download and use the dependencies that you needed for your project. You would probably place them in a lib folder and specify this location in your IDE as well as your build tool.

maven manages these dependencies for you. You specify the dependency your project needs in the prescribed format and maven downloads them for you from the internet and manages them. When building your project, maven knows where it has placed these dependencies and uses them. Most IDEs also know where these dependencies are, when they discover that it is a maven project.

Why is this a big deal? Typically most open source libraries release newer versions on a regular basis. If your project uses these, then each time a newer version is needed, you would need to manually download it and manage it. More importantly, each dependency, in turn may have other dependencies (called transitive dependency). If you do not use maven, you would need to identify, download and manage these transitive dependencies as well.

It becomes complex the more such dependencies that your project uses. It is possible that two dependencies end up using different versions of a dependency common to them.

久伴你 2024-12-26 04:01:08

编译项目时,Maven 将从存储库(通常是中央存储库)下载相应的 .jar 文件(您可以配置不同的存储库,用于镜像或​​用于中央存储库上不可用的您自己的库)。

如果您的 IDE 了解 Maven,它将解析 pom 并自行下载依赖项或要求 Maven 执行此操作。然后它将打开依赖项的 jar,这就是获得自动完成功能的方式:IDE 在幕后为您“导入”jar。

存储库不仅包含依赖项的“.jar”文件,还包含一个描述其依赖项的“.pom”文件。因此,maven 将递归下载其依赖项,您将获得编译软件所需的所有 jar。

然后,当您尝试运行软件时,您必须告诉 JVM 在哪里可以找到这些依赖项(即,您必须将它们放在类路径中)。

我通常所做的是将依赖项复制到 target/lib/ 目录,这样就可以轻松部署软件并启动它。为此,您可以使用在 中指定的 maven-dependency-plugin

<build>
  <plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}/lib</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
</build>

When compiling your project, Maven will download the corresponding .jar file from a repository, usually the central repository (you can configure different repositories, either for mirroring or for your own libraries which aren't available on the central repositories).

If your IDE know about Maven, it will parse the pom and either download the dependencies itself or ask Maven to do so. Then it will open the dependencies' jars, and this is how you get autocompletion: the IDE "imports" the jars for you behind the scenes.

The repository contains not only the ".jar" file for the dependency, but also a ".pom" file, which describes its dependencies. So, maven will recursively download its dependencies, and you will get all the jars you need to compile your software.

Then, when you will try to run your software, you will have to tell the JVM where to find these dependencies (ie, you have to put them on the class path).

What I usually do is copy the dependencies to a target/lib/ directory, so it is easy to deploy the software and to launch it. To do so, you can use the maven-dependency-plugin, which you specify in the <build>:

<build>
  <plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}/lib</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
</build>
感情洁癖 2024-12-26 04:01:08

Internet 上有各种托管工件(jar)的服务器,您可以下载这些工件作为 Maven 构建的一部分。您可以像上面所示添加依赖项来描述构建代码所需的 jar。当maven去构建时,它会联系这些服务器之一,并将jar下载到您的计算机上,并将其放置在本地存储库中,通常

${user_home}/.m2/repository

部分下进行配置

<repositories>
    <repository>
    </repository>
</repositories>

maven联系的服务器必须在您的maven项目pom文件中的“原型服务器” 可以在 repo1.maven.org 看到

maven 的好处是,如果需要你列出的 jar,它会拉的不仅如此jar,但该 jar 需要的任何 jar。显然,由于您将 jar 拉到您的计算机上,因此它只会在您的计算机上找不到它们时才下载它们,因此不会每次都会减慢您的构建速度(只是第一次)。

There are a variety of servers on the internet that host artifacts (jars) that you can download as part of a maven build. You can add dependencies like you show above to describe what jars you need in order to build your code. When maven goes to build, it will contact one of these servers and download the jar to your computer and place it in a local repository usually

${user_home}/.m2/repository

The servers that maven contacts must be configured in your maven project pom file, under a section like

<repositories>
    <repository>
    </repository>
</repositories>

The prototypical server can be seen at repo1.maven.org

The nice thing about maven is that if a jar you list is needed, it will pull not only that jar, but any jars that that jar needs. Obviously, since you are pulling the jars to your machine, it only downloads them when it can't find them on your machine, thus not slowing down your build everytime (just the first time).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文