Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Import dependencies from maven repository at runtime

I don't really like writing apis, I just hard-code shit for proof of concept and I'm gonna leave it up to you to use it however you want. 👍

Code
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class Main {

    public static void main(String[] args) {
        importMavenDependencies();
        String test = FileUtils.getUserDirectoryPath();
        System.out.println(test);
    }

    public static void importMavenDependencies() {
        SAXBuilder builder = new SAXBuilder();
        File dir = new File("./deps/");
        if(!dir.exists())
            dir.mkdir();
        try {
            Document document = (Document) builder.build(Main.class.getClassLoader().getResource("maven.xml"));
            Element rootNode = document.getRootElement();
            List<Element> dependencies = rootNode.getChildren("dependency");
            dependencies.forEach(d -> {
                String group = d.getChild("groupId").getValue();
                String artifact = d.getChild("artifactId").getValue();
                String version = d.getChild("version").getValue();
                File file = new File(dir, String.format("%s-%s-%s.jar", group, artifact, version));
                if(!file.exists())
                    downloadDependency(file, group, artifact, version);
                addToClasspath(file);
            });
        } catch (IOException | JDOMException e) {
            e.printStackTrace();
        }
    }

    public static void addToClasspath(File file) {
        try {
            URL url = file.toURI().toURL();
            URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
            Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
            method.setAccessible(true);
            method.invoke(classLoader, url);
        } catch(IOException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void downloadDependency(File file, String group, String artifact, String version) {
        try {
            String url = getDependencyURL(group, artifact, version);
            URL website = new URL(url);
            ReadableByteChannel rbc = Channels.newChannel(website.openStream());
            FileOutputStream fos = new FileOutputStream(file);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
            rbc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getDependencyURL(String group, String artifact, String version) {
        String url = String.format("http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=%s&a=%s&v=%s", group, artifact, version);
        HttpURLConnection conn = null;
        try {
            URL obj = new URL(url);
            conn = (HttpURLConnection) obj.openConnection();
            int status = ((HttpURLConnection) conn).getResponseCode();
            boolean redirect = status != HttpURLConnection.HTTP_OK && (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER);
            if (redirect)
                url = conn.getHeaderField("Location");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null)
                conn.disconnect();
        }
        return url;
    }
}

Here's an example of the xml file
Code
<dependencies>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>

You must already have the jar added as a dependency to your project, then export it and then once you've exported you can open the jar with an archive manager and delete those packages.
Pretty cool. I guess you could use this to download dependencies before you compile certain classes. Like if you didn't have ftp access and also had a way to create .java files. Like a simple text input which writes to a .java file.

Would be a filthy hack and potentially massively exploitable lol. But would be cool if you implemented before you lost access to the server directly.
(Jul 27, 2017 12:58 PM)sexyPudding Wrote:  Pretty cool. I guess you could use this to download dependencies before you compile certain classes. Like if you didn't have ftp access and also had a way to create .java files. Like a simple text input which writes to a .java file.

Would be a filthy hack and potentially massively exploitable lol. But would be cool if you implemented before you lost access to the server directly.

Could make it more secure by comparing the SHA hash of both the local and repository dependency


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by john ronaldsadJul 20, 2022 06:49 PM 4 238 Jul 20, 2022 06:49 PM
Last Post: john ronaldsad
[Java] by MavenGradleMar 15, 2021 07:14 AM 9 520 Mar 15, 2021 07:14 AM
Last Post: SIM-2501407
[Java] by LightCraftAug 27, 2020 07:48 PM 1 111 Aug 27, 2020 07:48 PM
Last Post: MavenGradle