How to add local jars folder in class path using POM - java

There are almost 10 local jars (these jars are required to build my application) in my Java application. I want them to add to the classpath using POM. How can I do that?

You can add the dependency as system scope and refer to it by its full path. Consider that the JAR is located in /lib. Then add the dependency in your pom.xml file as following:
<dependency>
<groupId>com.abc.pqr</groupId>
<artifactId>sample-app</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/your_jar.jar</systemPath>
</dependency>
${basedir} represents the directory containing pom.xml.

Related

How maven works in Jar downloading?

I'm working first time with Maven project. Just created new Maven project in eclipse and in pom.xml I've added below configuration. Like Spring, log4j's jar version etc
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.LearnJavaSpring</groupId>
<artifactId>TalendJavaSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.1.4.RELEASE</spring.version>
</properties>
</project>
The moment I save my pom.xml file, It automatically creates folder at C:\Users\trisha\.m2 location with respective jars in it, the one I've mentioned in pom.xml.
1) My doubt is, From where maven got these jars automatically in above folder location ? Does maven downloads the dependencies automatically ?
2) Is it correct to think in this way that, Maven parse the pom.xml file when we save that and download all the dependencies or jars mentioned in pom.xml file ?
3) What if I want maven to download jars from the different location from where Maven does ? Does maven automatically downloads the latest jars ?
4) Is that possible to change this default folder location C:\Users\trisha\.m2 to other location ? If yes, How..?
1) My doubt is, From where maven got these jars automatically in above
folder location ? Does maven downloads the dependencies automatically
?
In pom.xml you mention something like this:
<groupId>xx</groupId>
<artifactId>yyy</artifactId>
<version>1.1</version>
It will download the xx version 1.1 library automatically.
Search xx in Maven local repository.
Search xx in Maven central repository.
Search xx in Maven remote repository (if defined in pom.xml).
2) Is it correct to think in this way that, Maven parse the pom.xml
file when we save that and download all the dependencies or jars
mentioned in pom.xml file ?
Yes, exactly.
Reference
3) What if I want maven to download jars from the different location
from where Maven does ? Does maven automatically downloads the latest
jars ?
yes , see the above 2 answers combined. You can also ad jars manually by building path and then selecting the required jars. Better suggestion is always to use maven.
To get the latest Jar there are some parenthesis tweaks which you can find here
4) Is that possible to change this default folder location
C:\Users\trisha.m2 to other location ? If yes, How..?
yes, it is possible.
In your settings.xml change the below lines:<localRepository>C:\Users\me\.m2\repo</localRepository> to point to your desired folder.

Specific spring-core jar to be included in project

Ive included following lines in my pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
The specific spring-core JAR version I want is 4.1.6, but whenever I do an mvn clean install, the dependencies folder under my project in netbeans shows a downgraded version (currently 3.0.7.RELEASE).
I want to know how I can force maven to put 4.1.6.RELEASE jar in my dependencies folder. I've done
mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false
And I've the folder 4.1.6.RELEASE with correct jar in my local /.m2/repositories folder. I need to access org.springframework.util.MimeType class which is not available in spring-core 3.0.7.RELEASE jar.
See the result of mvn dependency:tree to know if another jar bring back the version 3.0.7. and exclude from this dependency the spring-core
See the same result to know if the right version is present
Add the pom.xml and the result of mvn dependency:tree in the question
Please follow these steps:
Check in .m2 local repository where the Spring 3.0.7 jar is downloaded(It should be under folder org->springframework)
Delete the folder present inside springframework, so that mvn will try to fetch required dependencies.
Let me know if it helps
The issue is that spring-core is a mandatory dependenecy of most spring artifacts. Most likely you are using a dependent artifact e.g spring-context which is of version 3.0.7. Then Maven is fetching spring-core of the same version as the dependent artifact using its transitive dependency mechanism.
To control the version of spring-core you need to move its declaration from the dependencies section to the dependency management section of your pom.xml as follows
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6</version>
</dependency>
</dependencies>
</dependencyManagement>

component-scan not working on jar file

springmvc-context.xml configuration file
Jar is placed in WEB-INF/lib.
Its giving 404 while trying to access the controller. How to find out its being initiating or not even and further work.
Try to place:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
this dependency in pom.xml file for the spring-context jars.
But before this have you placed your jar file from WEB-INF/lib to the build path. If not the first place the jar in the build path of the project.
This might help. Thanks!!!

How do you reference a maven profile in a dependency?

I have a jar file that is built only as a profile. How do I reference that in a <dependency/> block? Let's say it's groupId is com.mycompany, artifactId is test-jar, version is 2.0 and profile is customBuild.
From the project you are building run mvn install, which will install the jar locally in your .m2 directory. Then you can reference it as a regular dependency using
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>test-jar</artifactId>
<version>2.0</version>
</dependency>
in your dependent projects.

Maven POM configuration & Tomcat shared/lib dependencies

I'm new to Maven and I created a new web app to "migrate" an old application and to start using Maven 3. This application uses some libraries (jars) and most of them are under the shared/lib folder in Tomcat (5.5) directory.
How can add these libs to Maven POM? Should I add them at all?
I forgot to mention that some of these jars cannot be found in Maven repository since are more like utility libraries that are common to most of the projects.
In the <dependencies/> section of the POM, you can declare the shared jar as a "system" scoped dependency. This scope recognizes a <systemPath/> child element, allowing you to point to the filesystem location on the local (build) filesystem.
See Maven "System" dependencies
example:
<project>
…
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>someDependency</artifactId>
<version>1.0.1</version>
<scope>system</scope>
<systemPath>${tomcatHome}/shared/lib/someDependency-1.0.1.jar</systemPath>
</dependency>
</dependencies>
…
</project>
Use
<dependency>
tags to specify the libraries. And if some of the libraries are not found in the maven repository. Specify the repository using
<configuration>

Categories

Resources