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. 👍
Here's an example of the xml file
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.
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.

