Where do Maven look for springMVC jars when working with eclipse(maven plugin installed) - eclipse

I am using m2e plugin.
Just stuck at one point when maven search for springMVC jar files where it would be searching.
In /web-inf/lib or in project build path.
Where should I put those jars
Thanks

Maven looks for jars in its repositories.
Therefore you don't need download jars manually when using Maven, it will automatically download jars from remote repository and put it to the local repository on your machine.

Maven uses "settings.xml" file to find out your project repositories. and resolve dependencies(every jar files that you need) from those repositories.
more about seetings.xml and also
this one .
After downloading required jar file for first time, maven puts it in your local repository in:
${user-home}/.m2/repository.

Related

Build failure in Spring-boot-starter project due to missing WebApp libraries

I have a Spring-Boot-Starter-project in Eclipse Oxygen 4.7.0 and is trying to build a .war for deployment on an EC2 instance. I have added two jars to allow JDBC connectivity to PostgresSQL DB server from my deployed war.
I have also added the src/main/webapp/WEB-INF/lib path to the project build path as shown below
However, I am getting the Compilation error when I am doing mvn compile with a build failure stating the reason as unavailability of the jar classes
Any help will be really appreciated
You have configured Project build path in your eclipse, and it will not impact anything when you are doing mvn compile. Maven will not include the jar from the lib folder as it does not know lib folder's presence. You should apply either of below two options:
a. If those jars are present in maven repository then update your pom to read from maven repository directly.
b. Install these jars in your local repository then refer it in your pom.xml, check https://stackoverflow.com/a/30209982/3530898 for details

Making Eclipse Maven repository inside the project

I'm a real beginner to Maven and Spring framework in eclipse so excuse me if this sounds trivial..
I have created a MVC web project in eclipse using the STS plugin on my windows computer, but my problem is that the location of the jar and configuration files that maven is set to by default is: C:\Users\MyName.m2\repository
This is a problem for me because eventually I'm going to make a WAR file out of my project and deploy it on a Linux machine, which does not have Maven installed on it.
My question is, is it possible to take all the jar files in my Maven repository, and make a local repository on the project itself rather than on an absolute path on the machine (sort of like a lib folder within the WEB-INF in the project that will hold all the maven jars and that maven will direct itself to it), and thus making the transition to the Linux machine smooth
I understand there is this classpath variable named M2_REPO, but doesn't seem to be editable or accept relative path rather than absolute.
Thanks in advance
Since you are using maven and you are adding your dependencies on your pom.xml, then all the dependencies with a scope of "compile" will be added to your final war file created by the maven war plugin under the WEB-INF/lib folder, this means that you should not worried about having maven in your linux server, since the war file that you are going to deploy will contain all the necessary dependencies. If you want to confirm this, just create a package of your project using "mvn clean package" and check the "target" folder generated by maven, you will see a "war" file, and you can unzip your file to check what contains.

Not able to understand How maven works with Spring MVC

I made a Hello word example in springMVC.Following are my steps.
1.Installed maven-eclipse integeration in eclipse.
2.Download maven and set environment variables,JAVA_HOME etc.
3.Check to see if maven is running via cmd and it is.
4.Create new maven project and in pom.xml,set spring core,web,web-mvc dependencies.
5.Create new Dynamic web project and under its properties->Deployment assembly I have added maven project.
6.Added spring jars core,mvc,web,context etc in dynamic web project lib folder.
It's all working now but I am not able to understand the working.
Before,I was trying to make it work without maven by copy and paste jars into lib,classpath,by makiing user library and adding jars to it and then add it to current project but every time it was giving some error.Sometimes it was classnotfound,sometimes it was context related.
Finally i tried this maven thing by searching some online tutorials and it works now.
But I am not able get it.Can anybody explain me in detail that how those spring jars getting picked during development and deployment.What exactly is maven doing that I was not doing before.I am in the state of total confusion.I know the flow of my mvc project(controllers,views etc.).
Maven searches the libraries (they are also called artifacts in the Maven language) in repositories. In the Maven Central Repository many libraries are hosted - http://search.maven.org/. After Maven finds the libraries in a repository, it caches them in your local repository. It is in ${userDir}/.m2 The next time, Maven will look in your local repository, instead of downloading again from the Central Repository.
P.S You can specify many other repositories different than the Central Repository. This is done in the <repositories> tag in your pom.xml. The Central Repository is always searched by default.
if you don't want use maven you have change your project setup. remove the pom.xml and copy all needed jar into the webapp/WEB-INF/libs folder.
Copy the jar from the a maven build(target folder). in this case you have all together without grabbing them one by one over then.

Local Project Dependencies and Maven

I'm converting an ant backed Netbeans project into an Maven project. I've got most of the third party libraries set up in the POM, however now I've run into problems with setting up the local dependencies.
With the previous Netbeans way of doing things, it just added a project reference [with links to the source and jar location, rebuilt the dependency if the depedency's source had been changed and hadn't been compiled]. However I'm not sure how to setup up Maven to emulate this behavior. Is it possible?
Example:
Projects/SharedLibrariesResource [Ant based project]
Projects/WebSite [this is a maven based project]
Projects/Client
In this example the website and client projects don't connect to each other, but they do share the SharedLibrariesResource. Website should compile to produce a War with links to the SharedLibrariesResource
The way I understand the question, the Website maven project depends on the ant SharedLibrariesResource project. When Website is built, it should include the SharedLibrariesResource artifacts. The assumption is SharedLibrariesResources produces a jar artifact.
One way to achieve this is to
install SharedLibrariesResource to your local maven repository each time ant builds it
specify this as a dependency in Website pom.
We can use maven ant tasks to achieve the first.

Eclipse doesn't find source attachements

When opening a class included through a Maven dependency, Eclipse tells me that the containing .jar file has got no source attachment.
But in the directory in which the .jar file resides, there is also as corresponding -sources.jar file.
Do you have any hints regarding the solution of this problem?
Is there a way to tell Eclipse where it should automatically look for the source attachment?
Edit: In the Eclipse .classpath file, the sourcepath value is set for external archives, but not for ours. But both jars and sources file reside in the same repo.
The property -Declipse.useProjectReferences=false is also set.
The problem was that there were already sources.jar-not-available files for certain projects in my local repository.
I've deleted them and now the attachment of source files is working. Obviously Eclipse respectively the Maven Eclipse plugin doesn't look for source attachments as long as these marker files exist.
If it hadn't been for the comment from K. Claszen I'd have never came up with this solution.
Check that you have downloadSources in the maven-eclipse-plugin section of your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
...
</configuration>
</plugin>
If you are using m2eclipse plugin for integrating Maven projects in Eclipse, you might need to enable downloading sources and javadocs in m2eclipse preferences:
Window > Preferences > Maven > Download Artifact Sources
For me the file was in place in local repo and eclipse (4.5) kept complaining there is no associated source jar (sources.jar-not-available did not existed). I ended up removing the directory from local repo, letting eclipse to download the sources again repolulating the repo, running mvn eclipse:eclipse and refreshing the project.
For me all these steps was necessary to get this working.
For those who are still having trouble downloading sources for your Maven dependencies (like me), you can try this alternate method:
Find the particular Maven dependency you wished to inspect Java source code for. You need to have the Maven artifact name.
Next, navigate to the Maven repository server where you download the Maven dependency from. For example, I have spring-security-core-5.5.1.jar as my Maven dependency. The repository host site is from mvnrepository.com, in which the repository server is from https://repo1.maven.org/maven2/.
Navigate to mvnrepository.com and search for your Maven dependency artifact. In my case, it would be spring-security-core, and I located the artifact information page here.
On this page, in the table row labeled Files, you want to click on View All, on the right of the JAR link with how many kilobytes displayed to the side. (Underlined)
Once you're viewing the list of all JARs provided with the Maven dependency, click and download the one that says, [artifact name]-[artifact version]-sources.jar. In my case, it's spring-security-core-5.5.1-sources.jar.
Once downloaded, on your local machine, use your File Explorer to navigate to the folder containing the artifact JAR which you have trouble downloading the source codes for. In my case, it would be $MAVEN_HOME/repository/org/springframework/security/spring-security-core/5.5.1.
Move the [artifact name]-[artifact version]-sources.jar into this folder.
Go back into Eclipse, and try attaching the sources. You should be able to pick up the ...-sources.jar file upon attaching it.
After attaching it, you will then be able to see the source codes for your Maven dependency.
Right click at project, select Maven, select Download Sources.
I download a maven project from online,use mvn eclipse:eclipse converted to eclipse project then I occurred the problem.
solution is remove all external lib that start with 'M2_REPO',it's work for me.