If you use maven in your java project, you should use pom.xml to add dependencies that your java project required. But sometimes you may need to add the third-party library jar files directly in your java project instead of using maven pom.xml.
This article will tell you another method that does not use maven to download jar files but download the jar files from the maven repository directly. It will also tell you how to add the download jar files in your eclipse java project java build path.
1. Download Jar From Maven Central Repository Steps.
- Open a web browser and browse http://mvnrepository.com/ to go to the maven central repository website.
- Input the jars maven groups, artifacts, or categories name in the search box on the page top area and click the Search button. Then it will list all the related library links.
- For example, if we want to download the google Gson library jar files in the maven central repository, we type Gson and click Search.
- Click the Gson link to go to the library detail page.
- Click the version number that you need, in this example, we click version link 2.8.5 to go to that version detail page.
- In the Gson version detail page, on the Files row, you can see a link whose link text is something like jar (235 KB ). This is just the Gson jar file download link.
- Click the above link to download the Gson version 2.8.5 jar file.
2. Download Jar From maven.org.
- There is another maven repository that you can download jar files from.
- Open a web browser and browse the URL maven.org or search.maven.org.
- Input the jar library group name or artifact name in the search box, it will list all matched jar libraries.
- If you know the jar library group name, artifact name, and version, you can input something like g:com.dolphindb AND a:jdbc AND v:1.10.2 in the search box, this can make the search more accurate.
- Now it will list the matched maven jar library on the page, click the Download link at the end of the row, then you can select download jar, javadoc.jar, pom, or sources.jar file.
- But if you browse into some artifact detail webpage ( for example https://search.maven.org/artifact/com.google.code.gson/gson-parent/2.8.6/pom ), when you click the Download link at the top right corner of the page, it will only list one item pom.
- In such a case, you can search the artifact name in the search box, and click each listed artifact’s Download link, and you can find some links that can download the jar item and some links that can only download the pom.
3. How To Download Maven Artifact From Remote To Local Repository In Command-Line.
- I write a java library jar and use maven to distribute it to users.
- But some of my java library users do not use maven to build their java projects, so they do not use pom.xml in their projects.
- So I want to find a command line that can help them to download the java library artifacts to the folder ~/.m2/repository on their local computer so that they can use it locally.
- And that is a good way for them to use maven later if they want.
- So I googled and get the command line like below. The below command line will use the maven install plugin to install the jar file I think.
mvn install:install-file -DrepositoryId=java.net -Durl=http://download.java.net/maven/2/ -Dfile=test.jar -DpomFile=test.pom -DgroupId=test -DartifactId=test -Dversion=1-SNAPSHOT -Dpackaging=jar
- But when I run the above command line, I find it does not download the java library jar file from the remote repository to the local repository folder, instead it just copies the files that are built locally to the folder ~/.m2/repository on the local computer.
- How to download the java library artifact from the remote repository to the local repository in the command line? Thanks a lot.
- You can use the maven dependency plugin’s dependency:get goal to implement your task.
- But you need to make sure the dependency plugin version is the correct version.
- And you should provide the fully qualified name (test:test:1.2-SNAPSHOT) of your java artifact when you use the above dependency plugin.
- Below is an example of using the maven dependency plugin to download the artifact to your local repository.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \ -DrepoUrl=http://download.java.net/maven/2/ \ -Dartifact=test:test:1.2-SNAPSHOT
- When you run the above command, it will download the remote artifact to the local maven repository ~/.m2/repository.
- If you want to download the remote java artifact to another local directory, you can specify it using the argument -Ddest=/user/local/test.jar. Then it will download and save the remote java artifact to the /user/local/test.jar file.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \ -DrepoUrl=http://download.java.net/maven/2/ \ -Dartifact=test:test:1.2-SNAPSHOT \ -Ddest=/usr/local/test.jar
4. Add Download Jars Into Java Project.
- After downloading jar files from the maven central repository, before you can use them, you need to add them to your java project (2022/05/23).
- Below are the steps to add the jar to your java project using eclipse.
- Right-click the java project name in eclipse, and click the Properties menu item in the popup menu list.
- Click Java Build Path menu item in the left panel of the popup window, click the Libraries tab in the right panel, and click Add External JARs button below the Libraries tab.
- Browse to the folder where the downloaded jar file is saved and click the Open or Save button to add it to the java build path.
- Then click Apply and Close button to add the jars to the java project.
- Now you can use the Java classes contained in the jar files in your java source code.