How to add jar file dependency in pom.xml - java

I have an application that depends on 2 jar file :
OperatorInterface.jar
Operator.jar
I want to build my project using Maven structure. I've followed the Maven site tutorial to create the structure like so:
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=source.app -DartifactId=project
I've put my source file FileProcess.java in E:\project\src\main\java\source\app.
FileProcess has dependency in 2 external .jar files but I don't know how to define this dependency in pom.xml.
Here is the content of the pom.xml so far:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>source.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Can anyone help me?

First thing you need to install your jars in your maven local repository. You can follow the official tutorial here:
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
During the installation you will have to choose your own artifactId, groupId and version. You can choose whatever value you want but remember them because you will need those to include your jars in the pom.
After that you can include them in your pom.xml adding these lines under the tag dependencies for each library to include:
<dependency>
<groupId>your_group_id</groupId>
<artifactId>your_artifact_id</artifactId>
<version>your_version</version>
</dependency>

In the case when the JARs in question are your own code rather than third-party, the best approach is to "mavenize" the projects that build them as well. After you do that, running mvn install on those projects will place them in your Maven local repository where they will be available to other local Maven projects who declare them as dependencies.

Avoid adding them to yourpom.xml file straight. When you add .jars using this process, they will automatically reflect in your pom.xml
Steps are -
1. Right click on your project in the file explorer in your eclipse.
2. Go to build Path option.
3. Select configure build path
4. Chose the Libraries tab in the window that appears.
5. Add your .jar file externally.
6. Click ok and come back to your project interface
Update your maven project by pressing Alt+F5 and restart eclipse. Your problem should be solved.

I would take the following route since this is for corporate use. This is the hard and ultimately portable way that sets you up for future Maven usage as it is intended to be done.
1) make those dependent jars Maven projects (because then you can easily version-manage them too using Maven)
2) use a local repository manager and deploy your own projects to it using Maven release management through either the mvn:release plugin, or use a build server such as Hudson to automate the release process with a simple button press which I can highly recommend setting up.
https://maven.apache.org/repository-management.html
http://maven.apache.org/maven-release/maven-release-plugin/
3) mvn:release the dependency jars to your local repository manager so they will be available for other Maven projects
4) you're actually done, when you have a local repository where your deploy your own snapshot and release artifacts to, then your maven build can find your own maven modules and include them in the application dependencies - if you don't forget to configure the repository in the project's pom of course. And your build server if you have one can find them too.
The easy/lazy route is as suggested to manually install the jars in your local .m2 folder where Maven caches dependencies that it downloads, but it is absolutely not a portable solution that will stand the test of time. It won't work when somebody else needs to work on this project until they too install the jars locally. Or if its only you, you need to redo it every time you checkout the project on another computer / as another user. Also you need to update the jars each and every time you make changes to them, everywhere the project is checked out. You may need to do specific setup steps to get it working in an IDE, should you inevitably choose to start to use one.
However if you are having a time-pressure problem, then I would certainly go ahead and do that as a temporary workaround solution to be able to get going.

Related

Add project repo for an edited jar in Eclipse Maven project

I have a legacy project that I'd like to convert to a Maven project for dependency management.
The problem is, that I have one jar (fop-1.1.jar) that I had to edit. It differs from the one that is publicly available and I only have it locally. But I need it this way.
What I tried to do, following several similar how-to's, it to create a fake Maven repo inside the project (local repo is no good, because several people work on that project and the solution has to be self-contained on Git) and reference this repo from the pom.xml. Sounds like the way to go for me, but it doesn't work. Eclipse show the project repo grayed-out :(
What am I missing?
BTW: this is what I tried to follow: https://devcenter.heroku.com/articles/local-maven-dependencies
Let me suggest another way: When we need to "edit" a jar, we give it a special version number like 1.1-edited instead of 1.1.. Then we can easily upload it to our normal Maven repository and use it. Maven even makes sure that you do not accidentally load both versions in the same project because the edit is only in the version number.
I guess what you need is a private maven server(I guess it exists), and then execute command to deploy jar( before deploy, check your account has privileges)
mvn deploy:deploy-file -Dfile=${jarFilePath} -DgroupId=${groupID} -DartifactId=${artifactId} -Dversion=${version} -Durl=${privateServerURL} -Dpackaging=jar -DrepositoryId=${privateServerURLInYourMavenSettings.xml}
,
after deploy successfully, add maven dependency
<dependency>
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</dependency>

Importing Java owlexplanation libraries in Eclipse [duplicate]

I don't know how to use Maven at all. I've been developing for a couple years with Eclipse and haven't yet needed to know about it. However, now I'm looking at some docs that suggest I do the following:
"To include it within your project, just add this maven dependency to your build."
<repository>
<id>jboss</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
...
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>1.1.GA</version>
</dependency>
How do I do this with my Eclipse project?
Please assume I know nothing about Maven. I just figured out it might be installed on my computer by typing mvn on the command line, but that's seriously the extent of my knowledge. I would be happy to continue knowing nothing about Maven if there is an equivalent, non-Maven way of following these instructions with Eclipse.
On the top menu bar, open Window -> Show View -> Other
In the Show View window, open Maven -> Maven Repositories
In the window that appears, right-click on Global Repositories and select Go Into
Right-click on "central (http://repo.maven.apache.org/maven2)" and select "Rebuild Index"
Note that it will take very long to complete the download!!!
Once indexing is complete, Right-click on the project -> Maven -> Add Dependency and start typing the name of the project you want to import (such as "hibernate").
The search results will auto-fill in the "Search Results" box below.
In fact when you open the pom.xml, you should see 5 tabs in the bottom. Click the pom.xml, and you can type whatever dependencies you want.
You need to be using a Maven plugin for Eclipse in order to do this properly. The m2e plugin is built into the latest version of Eclipse, and does a decent if not perfect job of integrating Maven into the IDE. You will want to create your project as a 'Maven Project'. Alternatively you can import an existing Maven POM into your workspace to automatically create projects. Once you have your Maven project in the IDE, simply open up the POM and add your dependency to it.
Now, if you do not have a Maven plugin for Eclipse, you will need to get the jar(s) for the dependency in question and manually add them as classpath references to your project. This could get unpleasant as you will need not just the top level JAR, but all its dependencies as well.
Basically, I recommend you get a decent Maven plugin for Eclipse and let it handle the dependency management for you.
Open the pom.xml file.
under the project tag add <dependencies> as another tag, and google for the Maven dependencies. I used this to search.
So after getting the dependency create another tag dependency inside <dependencies> tag.
So ultimately it will look something like this.
<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>doc-examples</groupId>
<artifactId>lambda-java-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lambda-java-example</name>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Hope it helps.
I have faced the similar issue and fixed by copying the missing Jar files in to .M2 Path,
For example: if you see the error message as Missing artifact tws:axis-client:jar:8.7 then you have to download "axis-client-8.7.jar" file and paste the same in to below location will resolve the issue.
C:\Users\UsernameXXX.m2\repository\tws\axis-client\8.7(Paste axis-client-8.7.jar).
finally, right click on project->Maven->Update Project...Thats it.
happy coding.
I have faced same problem with maven dependencies, eg: unfortunetly your maven dependencies deleted from your buildpath,then you people get lot of exceptions,if you follow below process you can easily resolve this issue.

Maven dependency management benefits

I am not sure I really understand the strongest feature of Maven - Dependency Management.
When I am developing a project I need to add all dependencies while I am writing the code. Therefore when building the project with Maven I already have all dependencies downloaded.
What is the point of the Dependency Management then?
Thanks to Maven you do not need to download the dependencies and put it into the lib on Tomcat for instance. It is done automatically by maven. You can see directly in the pom.xml file all you dependencies and also other settings.
Dependencies management is just one of the main feature of Maven. I think you should ask yourself what are the addotional tasks (external libs, deployment, documentation etc...) you are performing in your project and see if Maven can help you on these tasks with its plugins.
Let's assume For your local you have downloaded all the necessary jars and kept in lib.Now you want to move your code into many machines.
So now you have to transfer all the files to another machine.
By mistake if you miss some jars while sending or let's say in future you want newer version of jar.So Do you like to send it to all the machines the jar and remove the older version from lib or write a configuration that automatically does all the things in one go.
Once the project gets bigger you will feel the real essence of Maven.It's just the pom.xml you need to take care all the jar management.
Maven's dependency management makes most sense in an environment where you work on multiple projects, across multiple dev machines and more than one dev.
Imagine the simplest of scenarios:
Project_1
\lib
\log4j.jar
Project_2
\lib
\log4j.jar
Project_3
\lib
\log4j.jar
While you are developing you will need to copy paste the log4j.jar file to all of those projects, which translates to extra disk space used locally and on any SCM you may use, and need to go into each project and define it as a library (add it to the classpath). If you want to change the version of the jar you need to repeat the process.
If you use Maven, all you need to do is
define an online repository (example Maven online repo)
create a pom for your project:
<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.MY.COMPANY</groupId>
<artifactId>NAME_OF_PROJECT</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
Most IDE's already have seamless integration with Maven and no further hassle is needed. And the above steps are only executed once.
Some additional references:
Why maven? What are the benefits?
Why should we use Maven?
Maven Dependency Plugin Example

How to build the jar artifact for a Spring Boot project in IntelliJ IDEA

I'm using IntelliJ IDEA 2016.2.5 and I've used Spring Initializr to setup a Spring Boot project. Everything works fine and I can run and test my project in the IDE.
Right now, when I want to build the jar file, I run the following command from command prompt:
$ mvn package
where my pom.xml is located and the jar file is created perfectly (with a caveat that I'm going to tell you about it later). The thing is, I'm looking for a way to do the same from within the IDE.
So far I've tried to create a new jar artifact in Project Structure window (Alt+Ctrl+Shift+s) and include the project's output while for the maven dependencies I use Extract into Output Root. This way, the artifact can be built and run but there are lots of error messages given out and some of the functionalities are not working.
Does anyone know how to build a jar file for Spring Boot project from within IntelliJ IDEA?
The reason why I need to build my jar file in IDE is that my project is composed of several maven projects and I've imported them all as modules into one project. When I run the main module in the IDE, IntelliJ IDEA is smart enough to consider the module as the dependencies for the main one. But when I use the command prompt mvn, this is not happening (which is totally understandable since maven only knows of main project and not the rest). If only I can build from within the IDE..
To clarify my situation better, I've got a "Main" project and a "Library" which of course Library is a dependency of Main. Since I don't have maven repository (and at this point, I'm not looking for one), the only way I know of to make it possible for the maven to compile my code is to name Library in Main's pom.xml like this:
<dependency>
<groupId>org.example</groupId>
<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/library-1.0-SNAPSHOT.jar</systemPath>
</dependency>
which as far as I know is highly discouraged. This is in the case that if I name the Library as an ordinary dependency:
<dependency>
<groupId>org.example</groupId>
<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
since a neighboring module (Library) outputs the exact same artifact, IntelliJ IDEA is capable of matching the dependency with that module and compile the code. But in case of mvn, since there's no module involved, this can not be done and the dependency is not satisfied. In other words, I can only config my environment to work with mvn or IntelliJ IDEA, but not both!
Go to the project root folder such as springbootdemo and run below commands:
mvnw clean package (This will create the target folder and generate the jar inside it)
mvnw spring-boot:run (This will run the spring boot application).
In my case application name is springbootdemo and its location is C:\Users\springbootdemo and I ran both commands at this location.
From within IDEA, you can go in the tool window "Maven Project", and here double click on Package.

Add a dependency in Maven

How do I take a jar file that I have and add it to the dependency system in maven 2? I will be the maintainer of this dependency and my code needs this jar in the class path so that it will compile.
You'll have to do this in two steps:
1. Give your JAR a groupId, artifactId and version and add it to your repository.
If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:
mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ....
2. Update dependent projects to reference this JAR.
Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:
<dependencies>
...
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
You can also specify a dependency not in a maven repository. Could be usefull when no central maven repository for your team exist or if you have a CI server
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>commons-utils</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-utils.jar</systemPath>
</dependency>
Actually, on investigating this, I think all these answers are incorrect. Your question is misleading because of our level of understanding of maven. And I say our because I'm just getting introduced to maven.
In Eclipse, when you want to add a jar file to your project, normally you download the jar manually and then drop it into the lib directory. With maven, you don't do it this way. Here's what you do:
Go to mvnrepository
Search for the library you want to add
Copy the dependency statement into your pom.xml
rebuild via mvn
Now, maven will connect and download the jar along with the list of dependencies, and automatically resolve any additional dependencies that jar may have had. So if the jar also needed commons-logging, that will be downloaded as well.
I'd do this:
add the dependency as you like in your pom:
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
run mvn install it will try to download the jar and fail. On the process, it
will give you the complete command of installing the jar with the error message. Copy that command and run it! easy huh?!
I'll assume that you're asking how to push a dependency out to a "well-known repository," and not simply asking how to update your POM.
If yes, then this is what you want to read.
And for anyone looking to set up an internal repository server, look here (half of the problem with using Maven 2 is finding the docs)

Categories