-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>dev.vaziak.mavendd</groupId> | ||
<artifactId>MavenDependencyDownloader</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.22</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Repository> repositories = new ArrayList<>(); | ||
private final List<Dependency> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |