Home > Mobile >  Download jar files from Maven Repositories
Download jar files from Maven Repositories

Time:05-16

Is there any way to download a jar file from a maven repository using java, or any other language?

In a Maven project, when I add a dependency it usually downloads the jar files from a remote repository if it does not exist on the local system.

Is there any way to do that, without using Maven, like in a library, or construct a URL and then fetch the jar?

CodePudding user response:

You can easily download any jar or pom files from public repos, for example

https://repo1.maven.org/maven2/...
<!-- Example -->
https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-starter/2.6.7/

Downloading file in Java is pretty straightforward, there numerous ways to do so when you have URL.

CodePudding user response:

Usually you will find the information related to the downloads directly in the repository web page, most of the time that information is in maven website too, maven was built precisely to tackle errors with downloaded jars though

CodePudding user response:

Navigate in a browser to https://mvnrepository.com/ then you can enter the library you are looking for and then click on the version you want. After clicking on the version there is a table with a row named Files which contains the links to the .pom and .jar files.

A Java application to download a file from an URL:

    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;
    import java.net.URL;
    import java.net.MalformedURLException;
    public class Download {
        public static void main(String[] args) throws MalformedURLException, IOException{
            String url = args[0];
            String fileName = url.substring(url.lastIndexOf('/')   1, url.length());
            try( InputStream in = new URL(args[0]).openStream()) {
                Files.copy(in, Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING);
            }  
        }
    }
$ java Download.java https://repo1.maven.org/maven2/org/xerial/sqlite-jdbc/3.36.0.3/sqlite-jdbc-3.36.0.3.jar
$ ls sqlite-jdbc-3.36.0.3.jar 
sqlite-jdbc-3.36.0.3.jar
$ 

Other than Java everything else works as fine for the download, i.e. download via your browser or via a command line tool like curl:

$ curl https://repo1.maven.org/maven2/org/json/json/20220320/json-20220320.jar --output json-20220320.jar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 70939  100 70939    0     0   339k      0 --:--:-- --:--:-- --:--:--  348k

CodePudding user response:

Find the jar file's URL using this: How To Download Jars From Maven Repository Then use this code for downloading jar file from the URL:

try (BufferedInputStream in = new BufferedInputStream(new URL(FILE_URL).openStream());
  FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME)) {
    byte dataBuffer[] = new byte[1024];
    int bytesRead;
    while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
        fileOutputStream.write(dataBuffer, 0, bytesRead);
    }
} catch (IOException e) {
    // handle exception
}

Download a File From an URL in Java

  • Related