简体   繁体   English

Maven依赖与在WEB-INF / lib中手动添加jar之间的区别

[英]difference between maven dependency and manually adding jars in WEB-INF/lib

I am new to programming web applications, and so far I am working with a eclipse/maven-setup with a Jetty server, to develop java web-apps. 我是Web应用程序编程的新手,到目前为止,我正在使用Jetty服务器的eclipse / maven-setup开发Java Web应用程序。

One thing I do not understand however, is the dynamics of including external jars. 但是,我不了解的一件事是包含外部jar的动态。 Many tutorials, and Q&A's here on SO claims that it is enough to add the jar to WEB-INF/lib. 关于SO的许多教程和问答均声称将jar添加到WEB-INF / lib就足够了。 other explain how I need to add them as dependencies in pom.xml. 其他解释了我如何需要将它们添加为pom.xml中的依赖项。 I have found that some times the first solution works, while some times I also need to add the dependency. 我发现有时候第一个解决方案可行,而有时候我还需要添加依赖项。 I am not sure why it is this way. 我不知道为什么会这样。 In addition, when adding dependencies, I have manually copied the external .jars to folders matching the already excisting repository resources. 此外,添加依赖项时,我已将外部.jar手动复制到与现有存储库资源匹配的文件夹中。 I can't believe that this is the right way to do it, but it has been working out for me. 我不敢相信这是正确的方法,但是它一直在为我解决。

So my questions are: 所以我的问题是:

  1. Am I correct that the jars need to be double referenced, or am I making a work around for a possible other problem? 我是否需要将罐子双重引用,还是对其他可能的问题进行解决?
  2. What is the difference between the two methods of import/referral? 两种导入/引荐方法有什么区别?

Bonus Question: Why are the errors below showing up in eclipse? 奖励问题:为什么下面的错误会在Eclipse中显示? These refers to all of the jars I have tagged as dependencies in pom.xml. 这些引用了我在pom.xml中标记为依赖项的所有jar。 The web-app runs as it should, although with seemingly random different runtime errors after a period of time. 该网络应用程序应按预期运行,尽管一段时间后看似随机的不同运行时错误。 Restart of the server fixes it at the moment. 重新启动服务器可以解决此问题。

Description Resource    Path    Location    Type
Missing artifact standard:standard:jar:1.1.2    pom.xml /WebApp line 1  Maven Dependency Problem
Missing artifact jstl:jstl:jar:1.2  pom.xml /WebApp line 1  Maven Dependency Problem
Missing artifact junit:junit:jar:4.8.2  pom.xml /WebApp line 1  Maven Dependency Problem
ArtifactDescriptorException: Failed to read artifact descriptor for javax.servlet:javaee-web-api:jar:6.0: ArtifactResolutionException: Failure to transfer javax.servlet:javaee-web-api:pom:6.0 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact javax.servlet:javaee-web-api:pom:6.0 from/to central (http://repo.maven.apache.org/maven2): connection timed out to http://repo.maven.apache.org/maven2/javax/servlet/javaee-web-api/6.0/javaee-web-api-6.0.pom pom.xml /WebApp line 1  Maven Dependency Problem
Missing artifact com4j:com4j:jar:1.0    pom.xml /WebApp line 1  Maven Dependency Problem
    WebApp      Unknown Validation Message
    WebApp      Unknown Validation Message
    WebApp      Unknown Validation Message

(Yes there is a total of 8 errors, whereof 3 is completely empty in eclipse. (是的,总共有8个错误,其中3个完全是空的。

There is not any difference at all. 没有任何区别。 Maven's pom.xml helps to download all jar files at .m2/repository location. Maven的pom.xml有助于在.m2/repository位置下载所有jar文件。 These jars can be used for different projects. 这些罐子可以用于不同的项目。 You need not to manually download. 您无需手动下载。 But for the very first time you need internet connection to download all jar files. 但是,这是您第一次需要互联网连接才能下载所有jar文件。 In pom.xml we have to set dependencies like given below or whatever version of jars you want to download. pom.xml我们必须设置如下所示的依赖项或您要下载的任何版本的jar。 If later point in time you want to change the version ( in following case ${spring.version} then you have to change only one place and everywhere get reflected. 如果以后要更改版本(在以下情况下${spring.version}则只需更改一个位置,则所有地方都会得到反映。

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

If you don't use pom.xml then while transfering project you will have to transfer all jar files over the network many times. 如果您不使用pom.xml则在传输项目时,您将不得不通过网络多次传输所有jar文件。

Maven give re-usability of jar files in multiple project. Maven在多个项目中提供了jar文件的可重用性。 You need not to have deep copy of all jar files in WEB-INF/lib folder. 您无需在WEB-INF/lib文件夹中具有所有jar文件的深层副本。

EDIT 1 编辑1

In very first attempt, any project will download all jar files from internet and will store all those jar files at .m2/repository . 首次尝试时,任何项目都将从互联网下载所有jar文件,并将所有这些jar文件存储在.m2/repository Second time in another project, when you try to use other pom.xml file then maven will try to find jars at local (your computer) .m2/repository whatever dependencies defined in your new project's pom.xml if those files are there in .m2/repository then maven will not try to connect to internet to download (because everything is downloaded at .m2/repository ). 第二次在另一个项目中,当您尝试使用其他pom.xml文件时,maven将尝试在本地(您的计算机) .m2/repository找到jar,无论新项目的pom.xml定义的依赖项是否存在.m2/repository则maven不会尝试连接到Internet进行下载(因为所有内容都在.m2/repository下载)。 If pom.xml file finds new entry then it will connect to internet and then download at .m2/repository 如果pom.xml文件找到新条目,则它将连接到Internet,然后从.m2/repository下载

EDIT 2 编辑2

Maven first checks dependency at local repository ( .m2/repository ), if not found then maven central repository (needs internet connection) and even if maven is not able to find at central repository then we have to tell those thing into the pom.xml file to download it from some other location. Maven首先检查本地存储库( .m2/repository )的依赖关系,如果未找到,则进行maven中央存储库 (需要Internet连接),即使maven无法在中央存储库中找到,我们也必须将这些内容告知pom.xml文件从其他位置下载。

For Ex. 对于前

<repositories>
    <repository>
        <id>java.net</id>
        <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
</repositories>

maven is a build tool, which also manages your project dependencies. Maven是一个构建工具,还可以管理您的项目依赖项。 It helps you build your project outside Eclipse as well. 它还可以帮助您在Eclipse之外构建项目。 You should either use maven or directly manage your project dependencies in (or outside) Eclipse on your own. 您应该使用maven或直接在Eclipse中(或外部)直接管理项目依赖项。 In the latter case, possibly you don't need maven. 在后一种情况下,可能不需要Maven。

As for the bonus question, looks like there are some connection issues which is resulting in artifacts not getting downloaded ( connection timed out ) - maybe your system is behind a proxy server. 至于红利问题,似乎存在一些连接问题,这导致无法下载工件( connection timed out )-也许您的系统位于代理服务器之后。

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

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