diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..730378a --- /dev/null +++ b/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + dev.vaziak.mavendd + MavenDependencyDownloader + 1.0.0 + + + 8 + 8 + + + + + org.projectlombok + lombok + 1.18.22 + + + \ No newline at end of file diff --git a/src/main/java/dev/vaziak/mavendd/HttpUtil.java b/src/main/java/dev/vaziak/mavendd/HttpUtil.java new file mode 100644 index 0000000..10a2dc2 --- /dev/null +++ b/src/main/java/dev/vaziak/mavendd/HttpUtil.java @@ -0,0 +1,31 @@ +package dev.vaziak.mavendd; + +import lombok.experimental.UtilityClass; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.channels.Channels; + +@UtilityClass +public class HttpUtil { + public boolean existsOnWeb(URL url) throws IOException { + HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); + + httpURLConnection.setRequestProperty("User-Agent", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"); + + return httpURLConnection.getResponseCode() == 200; + } + + /** + * @author sim0n + */ + public void saveToFile(URL url, File file) throws IOException { + FileOutputStream outputStream = new FileOutputStream(file); + + outputStream.getChannel().transferFrom(Channels.newChannel(url.openStream()), 0L, Long.MAX_VALUE); + } +} diff --git a/src/main/java/dev/vaziak/mavendd/MavenDownloader.java b/src/main/java/dev/vaziak/mavendd/MavenDownloader.java new file mode 100644 index 0000000..f0a468a --- /dev/null +++ b/src/main/java/dev/vaziak/mavendd/MavenDownloader.java @@ -0,0 +1,70 @@ +package dev.vaziak.mavendd; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.net.URL; + +@Builder(builderMethodName = "of") +@AllArgsConstructor +public class MavenDownloader { + private final File pomFile; + private final File exportDirectory; + private boolean downloadJavaDocs; + private boolean downloadSources; + + public void download() { + ParsedPom parsedPom; + + try { + parsedPom = new XmlParser().parseXml(pomFile); + } catch (IOException | SAXException | ParserConfigurationException e) { + e.printStackTrace(); + return; + } + + if (!exportDirectory.exists()) { + if (!exportDirectory.mkdirs()) { + throw new RuntimeException("Failed to create export directory folder."); + } + } + + parsedPom.getDependencies().forEach(dependency -> { + String jarNameFormatted = String.format("%s-%s.jar", + dependency.getArtifactId(), dependency.getVersion()); + + String formattedContent = String.format("%s/%s/%s/%s", + dependency.getGroupId().replaceAll("\\.", "/"), + dependency.getArtifactId(), + dependency.getVersion(), + jarNameFormatted); + + ParsedPom.Repository repository = parsedPom.getRepositories() + .stream() + .filter(repo -> { + try { + return HttpUtil.existsOnWeb(new URL(String.format("%s/%s", + repo.getBaseUrl(), formattedContent))); + } catch (IOException ignored) { + return false; + } + }).findFirst().orElse(null); + + if (repository == null) { + System.err.println("Could not find repository for dependency " + dependency.getArtifactId()); + return; + } + + try { + HttpUtil.saveToFile(new URL(String.format("%s/%s", repository.getBaseUrl(), formattedContent)), + new File(exportDirectory, jarNameFormatted)); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } +} diff --git a/src/main/java/dev/vaziak/mavendd/ParsedPom.java b/src/main/java/dev/vaziak/mavendd/ParsedPom.java new file mode 100644 index 0000000..8c73e3c --- /dev/null +++ b/src/main/java/dev/vaziak/mavendd/ParsedPom.java @@ -0,0 +1,38 @@ +package dev.vaziak.mavendd; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +@Getter +public class ParsedPom { + private final List repositories = new ArrayList<>(); + private final List dependencies = new ArrayList<>(); + + public ParsedPom() { + // default maven repository + addRepository(new Repository("https://repo1.maven.org/maven2")); + } + + public void addRepository(Repository repository) { + repositories.add(repository); + } + + public void addDependency(Dependency dependency) { + dependencies.add(dependency); + } + + @Getter + @RequiredArgsConstructor + public static class Repository { + private final String baseUrl; + } + + @Getter + @RequiredArgsConstructor + public static class Dependency { + private final String groupId, artifactId, version; + } +} diff --git a/src/main/java/dev/vaziak/mavendd/XmlParser.java b/src/main/java/dev/vaziak/mavendd/XmlParser.java new file mode 100644 index 0000000..fcb5d5e --- /dev/null +++ b/src/main/java/dev/vaziak/mavendd/XmlParser.java @@ -0,0 +1,47 @@ +package dev.vaziak.mavendd; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; + +public class XmlParser { + public ParsedPom parseXml(File pomFile) throws IOException, SAXException, ParserConfigurationException { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.parse(pomFile); + + ParsedPom parsedPom = new ParsedPom(); + + NodeList repositoryNodes = document.getDocumentElement().getElementsByTagName("repositories"); + NodeList dependencyNodes = document.getDocumentElement().getElementsByTagName("dependencies"); + + if (repositoryNodes != null) { + for (int i = 0; i < repositoryNodes.getLength(); i++) { + Element repositoryElement = (Element) repositoryNodes.item(i); + String url = repositoryElement.getElementsByTagName("url").item(0).getTextContent(); + + parsedPom.addRepository(new ParsedPom.Repository(url)); + } + } + + if (dependencyNodes != null) { + for (int i = 0; i < dependencyNodes.getLength(); i++) { + Element dependencyElement = (Element) dependencyNodes.item(i); + String groupId = dependencyElement.getElementsByTagName("groupId").item(0).getTextContent(); + String artifactId = dependencyElement.getElementsByTagName("artifactId").item(0).getTextContent(); + String version = dependencyElement.getElementsByTagName("version").item(0).getTextContent(); + + parsedPom.addDependency(new ParsedPom.Dependency(groupId, artifactId, version)); + } + } + + return parsedPom; + } +}