the difference between the maven dependency and the manual addition of jars in WEB-INF / lib

advertisements

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.

One thing I do not understand however, is the dynamics of including external jars. Many tutorials, and Q&A's here on SO claims that it is enough to add the jar to WEB-INF/lib. other explain how I need to add them as dependencies in 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. 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? These refers to all of the jars I have tagged as dependencies in pom.xml. 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.


There is not any difference at all. Maven's pom.xml helps to download all jar files at .m2/repository location. 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. In pom.xml we have to set dependencies like given below or whatever version of jars you want to download. 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.

    <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.

Maven give re-usability of jar files in multiple project. You need not to have deep copy of all jar files in WEB-INF/lib folder.

EDIT 1

In very first attempt, any project will download all jar files from internet and will store all those jar files at .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). If pom.xml file finds new entry then it will connect to internet and then download at .m2/repository

EDIT 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.

For Ex.

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