简体   繁体   English

Maven:如何找到依赖项

[英]Maven: how to find dependencies

Maybe I am misunderstanding Maven's dependency principles but this is my question:也许我误解了 Maven 的依赖原则,但这是我的问题:

I have a little Java program that requires these imports我有一个需要这些导入的小 Java 程序

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;

Now instead of importing these at the top of the code, I would just go into the POM file of my Maven project and add the dependencies.现在不是在代码顶部导入这些,而是将 go 放入我的 Maven 项目的 POM 文件中并添加依赖项。 But on https://mvnrepository.com/ how do I find the correct imports?但是在https://mvnrepository.com/我如何找到正确的进口? Is there another way besides looking on that site?除了在那个网站上看还有其他方法吗?

Thank you.谢谢你。

Now instead of importing these at the top of the code, I would just go into the POM file of my Maven project and add the dependencies.现在不是在代码顶部导入这些,而是将 go 放入我的 Maven 项目的 POM 文件中并添加依赖项。

No. You are conflating two different things:不,你把两个不同的东西混为一谈:

  • Managing dependencies (downloading and placing libraries within your project)管理依赖项(在项目中下载和放置库)
  • Using dependencies (calling the library's classes and methods from within your code)使用依赖项(从代码中调用库的类和方法)

Managing dependencies管理依赖项

To use a library, you need to obtain a physical copy, a file, usually a .jar file.要使用库,您需要获取物理副本,文件,通常是.jar文件。 You can manually download a copy.您可以手动下载副本。 Or you can use Maven or Gradle to download a copy on your behalf.或者您可以使用MavenGradle代您下载副本。 The Maven or Gradle approach is generally recommended over the manual approach.通常建议使用 Maven 或 Gradle 方法而不是手动方法。

Once downloaded, you need to place the file where it can be found within your project.下载后,您需要将文件放在项目中可以找到的位置。 Again, you can do this manually, or you can use Maven or Gradle to make the file available to your project.同样,您可以手动执行此操作,也可以使用 Maven 或 Gradle 使文件可用于您的项目。 Again, the Maven or Gradle approach is generally recommended over the manual approach.同样,通常建议使用 Maven 或 Gradle 方法而不是手动方法。

Using dependencies使用依赖项

After having obtained and placed a copy of the library, you are ready to access its classes and methods.在获得并放置库的副本后,您就可以访问它的类和方法了。

The catch is that the authors of that library may have named some of their classes and methods coincidentally with the same name as found in another library.问题是该库的作者可能将他们的一些类和方法命名为与另一个库中的相同名称

Imagine you want to use a class named Source , but two of your libraries have such a class:想象一下,您想使用一个名为Source的 class,但是您的两个库有这样的 class:

  • javax.xml.transform.Source
  • com.example.awesome.Source

If you write in your code:如果您在代码中编写:

Source s = new Source() ;

… how does the compiler know which of the two classes you meant? ...编译器如何知道您指的是两个类中的哪一个?

To resolve the mystery, you either:要解开这个谜团,您可以:

  • Write a fully-qualified class name.写一个完全限定的class 名称。
    javax.xml.transform.Source s = new javax.xml.transform.Source();
  • Write an import statement.编写import语句。
    import javax.xml.transform.Source;

The second approach, writing an import statement, usually makes for less typing and easier reading than the first approach of using fully-qualified names.第二种方法,编写一个import语句,通常比使用完全限定名称的第一种方法更少打字,更容易阅读。

The word import is a bit of a misnomer, and was used for legacy historical reasons. import这个词有点用词不当,是出于遗留的历史原因而使用的。 Its use here does not involve any moving of anything anywhere.它在这里的使用涉及在任何地方移动任何东西。 A better name would have been namespace , as in, specifying a defined domain of known names.更好的名称应该是namespace ,例如,指定已知名称的定义域。

When reading this:阅读本文时:

import javax.xml.transform.Source ;

… think this: ……想想这个:

namespace javax.xml.transform.Source ;

… meaning: “Any use below of the word “Source” can be assumed to mean the Source class from the library whose package is javax.xml.transform ... 含义:“以下任何使用“源”一词都可以假定是指 package 为javax.xml.transform的库中的Source class。

In its effort to find the class of that package you named, the Java Virtual Machine at runtime automatically looks through all the libraries you obtained and placed.为了找到您命名的 package 的 class, Java 虚拟机在运行时会自动查看您获得和放置的所有库。

You can also search Maven library by full class name by fc operator at search.maven.org您还可以在search.maven.org上按fc运算符的完整 class 名称搜索 Maven 库

eg.例如。

fc:javax.xml.transform.stream.StreamResult

https://search.maven.org/search?q=fc:javax.xml.transform.stream.StreamResult https://search.maven.org/search?q=fc:javax.xml.transform.stream.StreamResult

Of course one class can be found in many library...当然,在许多图书馆中都可以找到一个 class ...

There is something called java standard library - this means that a lot of things are automatically avaibable to you and you don't have to add anything to pom file.有一个叫做java standard library的东西——这意味着你可以自动获得很多东西,你不必在pom文件中添加任何东西。

Maven is used for adding external libraries that are not included and shipped with your java. Maven 用于添加不包含在 java 中的external libraries

Easiest way to find out if you need to add anything to pom file is to use good IDE (for example Intellij ) that provides support for maven.确定是否需要向pom文件添加任何内容的最简单方法是使用良好的IDE (例如Intellij ),它为 maven 提供支持。 It should mark any libraries that are missing - then add those to your pom file.它应该标记任何丢失的库 - 然后将它们添加到您的pom文件中。

And you still need import everything you need to use for each *.java file.而且您仍然需要为每个 *.java 文件导入您需要使用的所有内容。

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

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