difference between maven dependency and manually adding jars in WEB-INF/lib - java

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:
Am I correct that the jars need to be double referenced, or am I making a work around for a possible other problem?
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>

maven is a build tool, which also manages your project dependencies. It helps you build your project outside Eclipse as well. You should either use maven or directly manage your project dependencies in (or outside) Eclipse on your own. In the latter case, possibly you don't need 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.

Related

Maven - Handling Snapshot Dependencies

I have the following maven dependency in my web application POM, which in turn should pull other dependencies:
<dependency>
<groupId>com.my.libraries</groupId>
<artifactId>my-libs</artifactId>
<type>pom</type>
<version>1.6-SNAPSHOT</version>
</dependency>
NOTE I had release 1.5 for my-libs artifact previously pulled during an older build. But I have later upgraded the version to 1.6-SNAPSHOT. mathlibrary artifact wasn't present in 1.5 release.
I am expecting some jars to be present as part of my-libs artifact verson 1.6-SNAPSHOT which wasn't present in 1.5 - the pOM is below:
<!-- all the usual POM descriptionsm for my-libs -->
<groupId>com.my.libraries</groupId>
<artifactId>my-libs</artifactId>
<version>1.6-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- continues... all normal stuff, nothing to see here :'/ -->
<dependency>
<groupId>com.my</groupId>
<artifactId>mathlibrary</artifactId>
<version>1.0</version>
</dependency>
<!-- continues -->
I expected that mathlibrary jar would be pulled if I do mvn package for my project. But when I looked at the lib folder for my webapplication the jar wasn't there. I then checked my .m2 directory contents and it seems that only version 1.5 is present, so 1.6-snapshot wasn't automatically installed. From here, I am thinking that if a release version is pulled before, it doesn't pull the next SNAPSHOT?
After I manually installed my-libs artifact using mvn clean install - on the next build for my web application it pulled the jars correctly. Does this mean that my package phase is defined not to automatically pull the SNAPSHOT dependencies? or is this something expected (i.e. I have to manually run install phase it if it's not a release version) ?
Use mvn package pulls all SNAPSHOT dependencies from your local repository (and also updates your local repository from a remote repository every 24 hours, if the artifact comes a remote one).
I do not really understand what you mean by "manually installed my-libs" because there is no automatic way to install artifacts (except for CI servers, that install/deploy on checkin). If you want to use an artifact (like my-libs), you need to build it and put it in the local repository -- and you do this by using mvn install.
Answer to your question depends on which packaging you are using in your pom.xml and on which plugins have executions tailored to the packge phase. When dependencies are processed, Maven usually resolves them, which means that corresponding jar files will be in your local repository.
Getting jar files in your target folder (as is, or unpacked) is a result of some plugin's work (such plugin would execute a mojo at prepare-package or package phase (usually). So, make sure you use proper packaging (such as war) or e.g. "assembly" plugin (which can repack dependencies for you).
To make your dependency artefacts available for resolution and further use, you should either deploy them to a remote repository, or install them to local repository.
After reading #JFMeier answer on this, I did the following:
1) Removed all release/snapshots from my .m2 directory.
2) Changed my web app POM to use 1.5 release for my-libs
3) Kicked off the build.
4) Once the build finishes, I confirm that mylibraries jar hasn't been deployed in my final web application directory in tomcat's WEB-INF\lib
5) Repeated steps 2-4 - BUT changed the version to 1.6-SNAPSHOT for my-libs.
6) Now I can see the expected result.
Maven is only getting into repository (either global or local) when you make a mvn install. With Maven Install, Maven checks if all the POM libraries are present in your project and downloads from repositories into your project if some are missing. If you do a Maven package, it only packages the libraries present in your project.

How can I tell Maven to bundle dependent jars

I want to use Jmathplot.jar
I tried to put it as a dependency
<dependency>
<groupId>jmathplot</groupId>
<artifactId>jmathplot</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jmathplot.jar</systemPath>
</dependency>
but when installing I get this error:
Some problems were encountered while building the effective model for com.NResearch:dable-start-tRisk:jar:0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for jmathplot:jmathplot:jar should not point at files within the project directory, ${project.basedir}/lib/jmathplot.jar will be unresolvable by dependent projects # line 44, column 19
How can I get around this please?
EDIT1:
I cannot change Maven to include all dependent jars into a single jar. As this is uploaded to a web project.
"Dependent" is using your project, "dependency" is used by your project.
The real error here is that jmathplot.jar is in a folder that can really only reliably be found by your project. Even though your dependents know how to find your artifact in the local repository, they won't know where the sources are for your artifact, hence won't be able to find lib/jmathplot.jar. You can fix that by changing the systemPath to an absolute path. It can still be parametrized, but then please use properties rather than implicit properties (such as ${project.basedir}.
It'd be better to get rid of the systemPath dependency, by installing jmathplot into a company repository, so it can be used alike 'normal' artifacts. But that may not be a useful option if you have to distribute your artifact out of the reach of your company repository. It would even be better if jmathplot would just get deployed to the Maven central repository.
As a last resort you may choose to bundle the dependencies (not the dependents). You can do this:
Using the Maven Shade Plugin. It lets you choose which packages to include which may be useful to bundle only jmathplot (and not other dependencies).
Using the Maven Assembly Plugin. It has a predefined descriptor for "JAR with dependencies" which would fit your use case. You could create your own descriptor off of that example and set dependencySets.dependencySet.scope=system to only include the system dependencies that are giving you trouble.
Best way is to install your dependency on your local repository. To do this:
1) using project source, install to local repository using mvn install
2) if you don't have source code, install to local repository using this
hope it's help
nota: you are spamming around this question, do you ? (see here: JMathPlot what is the Maven dependency code please )

Maven dependency issue - artifact not found in central repo

I'm trying to build the project from this site http://www.joptimizer.com/usage.html. I downloaded the sources jar file, unpacked it and ran maven package in the root folder. Maven fails at the last minute saying it couldn't resolve the dependency..
could not find artifact seventytwomiles:architecture-rules:jar:3.0.0-M1 in central repo - repo.maven.apache.org/maven2 ..
I have a feeling I might need to change something in the pom.xml file for this to work, but have no idea what. Googling for this missing dependency lead me no where. In general, how would one know what to do to handle such errors (and also please help with this specific case).
Specifically
According to the Building notes on http://www.joptimizer.com/usage.html:
JOptimizer is build on maven 3.0. Before building it, you must resolve
(in pom.xml) the external dependency on Colt and other dependencies
that aren't in public repositories. Please refer to the "Dependencies"
report for a complete treatment. For ease of use a boundle with
these external libraries is provided (visit "Download"): extract the
boundle in a folder and run the "maven-install.cmd" (translate it in
your own shell language), and you will get the artifacts in your local
repository.
To get the bundle for this, go to http://sourceforge.net/projects/cvxopt/files/, and download the appropriate version of joptimizer-3.X.X-dependencies.zip. Unzip in your own folder, and run mvn install:install-file -DgroupId=seventytwomiles -DartifactId=architecture-rules -Dversion=3.0.0-M1 -Dpackaging=jar -Dfile=architecture-rules-3.0.0-M1.jar -DpomFile=architecture-rules-3.0.0-M1.pom
Generally
Use a tool like http://mavenrepository.com to search for another version of the missing dependency and update your POM with the proper version. If MVNRepository doesn't know about it, you can install the dependency yourself. If you are working with a group of developers, as Eric Jablow mentions, an artifact repository like Nexus or Artifactory is great for sharing non-public dependencies. If it's just you, you can install the artifact in your local repo as described here: How to manually install an artifact in Maven 2?
You should add your own repository manager like Nexus or Artifactory. Then, find out where this dependency is kept; there are repositories other than central. If it's kept on another repository, have your repository mirror that too.
Otherwise, Nexus or Artifactory have commands to enter the dependency manually. Create a local repository called "Third-party" and add it there.
Finally, change your settings.xml file to refer everything to your repository manager.
The most common case for this is when a company refuses to license their products to be held at the central repository. For example, Microsoft won't let its sqljdbc.jar file be distributed through Central. So, you need to add it by hand.
Change the dependency as follows
<dependency>
<groupId>org.architecturerules</groupId>
<artifactId>architecture-rules</artifactId>
<version>3.0.0-rc1</version>
<scope>test</scope>
</dependency>
Add the repository in pom
<repositories>
<repository>
<id>architecturerules.googlecode.com</id>
<url>http://architecturerules.googlecode.com/svn/maven2/</url>
</repository>
</repositories>

Configure Maven To Create An Archive File With All Dependencies Defined in POM XML

I am working in a private network which doesn't have internet proxy.
I can not create a local repository as well which involves the bureaucracy, management won't allow it. I may be a long term fix but not the solution for the question I asked.
I can not keep maven as a build tool as it requires the direct or indirect internet connection.
I HAVE to use ANT for building the project hence using maven in offline mode also not an option for me.
But I still want to use the maven dependency management for collecting all the jars in a one archive smartly.
My plan is to generate a ZIP file containing dependencies resolved using maven. And then we will share this ZIP file to all developers working inside a private network which doesn't have internet connection.
To do so I will get a temporary access to a computer which is having internet connection and from there I will define a dummy POM with all the dependencies required.
Now the question is how do I generate a ZIP file ( not a single jar ) using maven which contains all the dependencies defined in POM.
while what youre suggesting is technically possible, it is (in my opinion) not the best solution to your problem.
your statement that
it requires the direct or indirect internet connection
is not accurate. what maven requires is a maven repository (or a set of them) to fetch stuff from. the best solution to your problem would be to install a local maven repository inside your organization's network. the 2 most popular choices for a loaclly-run maven repository seem to be nexus and artifactory - both offer free open source versions and paid supported pro versions.
once you set up a maven repository inside your organization's network and populate it with the artifacts you require you can simply configure all of your project's pom files to go to those repositories. for example, to configure maven to use your repo instead of maven central, you can do this:
<repositories>
<!-- override central -->
<repository>
<id>central</id>
<url>http://your.repo.location</url>
</repository>
</repositories>
you will need to map a plugin repository in a similar fashion.
its also possible to achieve this by configuring the maven settings.xml file in each user's home directory if you dont want this in the pom files but from my experience its less error-prone this way
use following command to build Maven project offline.
mvn -o package
Refer this and this for more information.
I ended up with a smart hack which lets me do dependency resolution and archiving!
I am creating a dummy maven web project with all the dependencies defined in pom xml.
Now the war packaging mode is used by default for web applications.
I simply install the maven project from internet facing machine.
I get all the dependencies and transitive dependencies in war file's "lib" directory with dependency naming version remaining unchanged !!!!
Copying and adding those files into an ANT project is a trivial task then..!

Error opening Maven POM file dependency hierarchy in Eclipse - "Project read error"

When I open a POM file and click on the "Dependency Hierarchy" tab at the bottom, it gives me the error, "Project read error". It works with other projects in the same workspace, just not with this one. Any ideas?
EDIT
In response to #Yhn's answer.
Running the compile and package phases outside of Eclipse from the command-line work as expected. It compiles the application and builds the final WAR file.
Eclipse is indeed pointing to the default location of the Maven settings.xml file, so it should be aware of the custom repositories that are defined in it (my company has its own Maven repository).
I can open and edit the POM file from Eclipse, so it must have read/write permissions to the file.
The project is not configured in Eclipse as a Maven project, so I cannot run the package phase from Eclipse (I can only run it from the command-line).
I wonder if it has anything to do with the fact that I was having trouble building the project with Maven 3 because apparently some of the transitive dependencies are configured for Maven 1, which Maven 3 does not support (this is my theory anyway, based on some of the error messages). I can build the project with Maven 2, but I still get messages such as the following:
Downloading: http://dist.codehaus.org/mule/dependencies/maven2/org/codehaus/xfie/bcprov-jdk14/133/bcprov-jdk14-133.pom
[INFO] Unable to find resource 'org.codehaus.xfire:bcprov-jdk14:pom:133' in repsitory mule (http://dist.codehaus.org/mule/dependencies/maven2)
It must be able to find these dependences however, because it downloaded the JARs just fine and can build the application. It seems like the problem is that the dependencies don't have POM files associated with them, which is maybe why they cannot be used with Maven 3. This might also be why I cannot view the Dependency Hierarchy in Eclipse.
EDIT 2
I converted the project to a Maven project by going to "Configure > Convert to Maven Project". When I open the POM file, I see the error:
ArtifactDescriptorException: Failed to read artifact descriptor for woodstox:wst (Click for 140 more)
(woodstox:wst is another transitive dependency of the project). An error appears in the "Markers" view for seemingly every depedency and transitive dependency in my project. However, I can successfully build the project by doing a "Run As > Maven build". (Edit: This might be because this project has no Java source code, but the JARs of the dependencies correctly appear in the final WAR.) The Dependency Hierarchy still gives the same error--"Project read error".
About the "Unable to find resource" messages--but this only appears for a handful of transitive dependencies. The project has many more transitive dependencies, but these messages do not appear for them. It seems like, because the dependencies do not have POM files, that Maven tries to search for them every time the project is built. Is this normal not to have POMs??
How might I go about getting a repo manager? Is this something that would have to be installed on the company's Maven repository or can you install it on your own workstation?
I had this problem with some non-maven jars that I needed to include in my maven project. I put the jars in my local repository using this maven command:
mvn install:install-file -Dfile=/test/gov.nist.xccdf-1.2.jar -DgroupId=gov.nist -DartifactId=xccdf -Dpackaging=jar -Dversion=1.2
Then I referred to them as dependencies in my pom.xml:
<dependency>
<groupId>gov.nist</groupId>
<artifactId>xccdf</artifactId>
<version>1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
This worked fine for a while, but I must have upgraded something in eclipse, and I started getting the error you describe.
The fix is to take them out of the local repository, and redo the install-file asking maven to generate a pom for you:
mvn -DgeneratePom=true install:install-file -Dfile=/test/gov.nist.xccdf-1.2.jar -DgroupId=gov.nist -DartifactId=xccdf -Dpackaging=jar -Dversion=1.2
The command will cause a pom to be generated in the same directory (within your local repo) where the jar is placed.
In recent versions of eclipse maven support, the maven build is still done with maven 2 (if you right-click your pom.xml, choose run as mvn package for example). The dependency analysis is now done with an embedded maven 3 plugin, which doesn't like a dependency that has no pom.xml.
I had the same problem. If you made your project a maven project, you should run:
Right Mouse Click on project | Maven | Update Dependencies or
Right Mouse Click on project | Maven | Update Project Configuration
That worked for me.
Given the information through the comments:
mvn compile/mvn package work through console (I'm assuming outside of eclipse). Based on that, the POM file should be correct and dependencies can be resolved.
However; it could be that eclipse's settings aren't correct. For example, if eclipse has some own maven properties (instead of the default ~/.m2/settings.xml file); I can imagine it to fail resolving dependencies configured in the settings.xml used by maven itself. The settings for this should be in the Eclipse Preferences # Maven > User Settings.
Also make sure that Eclipse can actually read the file (not locked, correct rights, etc).
Should this seem to be correct; can you try to run the mvn goal compile from eclipse? You can do this by right-clicking the project and selecting run as > maven package. That way it should run maven with the same settings as eclipse is using, and might show any additional errors in it's configuration.
--
In response to additional information:
Basically it tells you - when using Maven 2 - that it can't find a dependency resource (in this case bcprov-jdk14) in the given repository (codehaus/mule). When I search that maven module (bcprov-jdk14) I find it in the Maven central repo (through our local nexus) # repo1.maven.org/maven2. Maybe that causes the error you're getting with Maven2?
And as the Maven 3 doc says; for Maven 1.x repo's; you should proxy them through a repo manager that can serve it to you as a maven 2 repository (I believe we do that here too for some repo's through Sonatype Nexus OSS)
ps.: Enabling maven dependency management in eclipse should be enough to be able to use maven run targets; if you would want to test it.
--
Sound like it can be multiple problems... given that the normal maven build works fine, one would think that maven can at least get the artifacts it depends upon and their dependencies... Yet, eclipse doesn't seem to be able to.
Double check if your Eclipse internet connection settings are correct? Since you were talking about a company repo, I'm assuming there will probably also be a proxy (I had to set up my Eclipse to use that one too; though for plugins (see next point)).
Another possibility, from experience, can be a rule-based proxy block on *.pom url requests (our proxy here blocks that to force usage of the local repo manager, how evil and annoying that is :(). You might want to try and open the .pom file it tries to download manually in your browser to see if this is the case (I can't, for example...).
To get the repo set up, you would probably have to ask the one responsible for the repository to add it as a maven2 proxy.
At the very least, it seems Eclipse is having issues getting the dependency poms (which are needed to build the dependency tree) from the internet. You could always try to ask some ICT crew (if they are capable enough...) about it at your company, maybe they have some useful hints.
right click on the project, Maven->Update Project->Tick "Force Update of Snapshots/Releases"
I had the same problem when I added a new dependency to pom.xml without network connection. After this, I had so many problems such as "Missing artifacts", "Missing Descriptor", or "Project Read error". I solved this
delete the bad dependency just added from local repository.
rebuild local index.
re-add the dependency
<properties>
<!-- ********************** -->
<!-- Plugin's properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
this pom setting worked for me
Usually this happens if there is a typo in the pom that Maven cannot understand and then it throws Project read error. Like in my case I copied a maven dependency snippet and I by passed the version. Instead of typing the actual version # it was getting it dynamically ${asciidoctorj.version} and my pom didn't have this in the properties section. So I got this error. Once I removed that and put a version number it started working.
I had this after copying a local repository folder from another machine. The fix was to go into the repository folder I copied and delete
_remote.repositories along with the 'LastUpdated' files then refresh the Maven dependencies in Eclipse (Alt+F5)
I faced same situation today. In my case it's caused by dependency name in wrong case. E.g.
Project A -> Project B -> Project C
In project B's pm file, I mistakenly specified the dependency artifacts name with "c" in stead of "C".
As in mac os, the files system is case insensitive. so I can build it in command line without detecting this mistake.
In Eclipse it breaks, but it provides very bad error message. In the error list, it says "dependency problem", but the name of dependency is empty. It also can't identify which line in the POM causes the problem.
When try to open "Effective POM" in POM viewer, it will show project read error. In the pop up error dialog box, it will show:
Could not read maven project
java.nio.channels.OverlappingFileLockException
All the messages are useless and misleading.
I finally detected this problem by submit to Jenkins CI after wasted hours of time.
I did maven clean install and the errors were gone
I faced the same issue of "Project read error" while clicking on "Dependency Hierarchy" tab. In my project I had a parent pom and child pom.
Parent pom had a property <appVersion>4.5<appVersion> that was being used in child pom
<version>${appVersion}<version>.
I selected the main project, right click and choose run as maven clean. The console display showed warning that version is using expression but should be a constant. Replacing ${appVersion} with constant value of 4.5 fixed the issue.

Categories