1 -import static java.
lang.
System.
out;
5 -import java.
net.
http.
*;
6 -import java.
net.
http.
HttpResponse.
*;
7 -import java.
nio.
file.
*;
9 -import java.
util.
concurrent.
ConcurrentSkipListSet;
12 - public static void main(
final String[] command_line_arguments) {
14 - var current_working_directory
= Paths.get(
System.getProperty(
"user.dir"));
15 - var download_directory
= current_working_directory.resolve(
"downloaded_artifacts");
16 - var maven_repository
= "https://repo.maven.apache.org/maven2";
17 - var expected_count_of_arguments
= 3;
18 - if (contains(command_line_arguments,
"-d")) {
19 - expected_count_of_arguments
= expected_count_of_arguments
+ 2;
21 - var given_download_directory
= command_line_arguments[index_of(command_line_arguments,
"-d")
+ 1];
22 - download_directory
= current_working_directory.resolve(given_download_directory);
23 - }
catch (
IndexOutOfBoundsException exception) {
24 - out.println(
"The -d option requires an argument.");
29 - if (contains(command_line_arguments,
"-r")) {
30 - expected_count_of_arguments
= expected_count_of_arguments
+ 2;
32 - var given_maven_repository
= command_line_arguments[index_of(command_line_arguments,
"-r")
+ 1];
33 - maven_repository
= given_maven_repository;
34 - }
catch (
IndexOutOfBoundsException exception) {
35 - out.println(
"The -r option requires an argument.");
40 - if (command_line_arguments.length
!= expected_count_of_arguments) {
44 - var maven_coordinates_reversed
= new String[
3];
45 - for (
var index
= 2; index
>= 0;
--index) {
46 - maven_coordinates_reversed[index]
= command_line_arguments[expected_count_of_arguments
- index
- 1];
48 - var group_id
= maven_coordinates_reversed[
2];
49 - var artifact_id
= maven_coordinates_reversed[
1];
50 - var version
= maven_coordinates_reversed[
0];
52 - if (
Files.exists(download_directory)
&& Files.isRegularFile(download_directory)) {
53 - fail(
"The given download directory path %s already points to a file!%n", download_directory.toString());
54 - }
else if (
!Files.exists(download_directory)) {
55 - out.printf(
"Creating directory at %s%n.", emphasize_global(download_directory.toString()));
57 - Files.createDirectories(download_directory);
58 - }
catch (
IOException __) {
59 - fail(
"Unable to create directory at %s", emphasize_global(download_directory.toString()));
63 - var expected_artifact_base_url
= guess_maven_artifact_url(maven_repository, group_id, artifact_id, version);
64 - var client
= HttpClient.newHttpClient();
65 - var request
= HttpRequest.newBuilder().uri(
URI.create(expected_artifact_base_url)).build();
66 - var target_directory
= download_directory;
67 - client.sendAsync(request,
BodyHandlers.ofString()).thenApply(
HttpResponse::statusCode).thenAccept(status
-> {
69 - fail(
"Could not find maven artifact at %s.%n", expected_artifact_base_url);
71 - download_ressource(client, target_directory,
72 - build_ressource_url(expected_artifact_base_url, artifact_id, version,
".jar"));
73 - download_ressource(client, target_directory,
74 - build_ressource_url(expected_artifact_base_url, artifact_id, version,
"-sources.jar"));
77 - var processed_cache
= new ConcurrentSkipListSet<
String>();
78 - process_pom(client, processed_cache,
79 - build_ressource_url(expected_artifact_base_url, artifact_id, version,
".pom"));
83 - out.printf(
"download directory:\t%s%nmaven repository:\t%s%nmaven coordinates:\t%s:%s:%s%n",
84 - emphasize_global(download_directory.toString()), emphasize_global(maven_repository), emphasize_global(group_id),
85 - emphasize_global(artifact_id), emphasize_global(version));
89 - static Collection<
String>
process_pom(
HttpClient client,
Set<
String> processed,
String pom_url) {
90 - if (processed.contains(pom_url))
91 - return Collections.emptyList();
92 - var request
= HttpRequest.newBuilder().uri(
URI.create(pom_url)).build();
93 - return client.sendAsync(request,
BodyHandlers.ofInputStream()).thenApply(input
-> {
95 - processed.add(pom_url);
98 - return List.<
String>of();
102 - static void download_ressource(
HttpClient client,
Path target_directory,
String ressource_url) {
103 - var request
= HttpRequest.newBuilder().uri(
URI.create(ressource_url)).build();
104 - var ressource_name
= ressource_url.substring(ressource_url.lastIndexOf(
'/')
+ 1);
105 - var target_ressource_path
= target_directory.resolve(ressource_name);
106 - client.sendAsync(request,
BodyHandlers.ofFile(target_ressource_path)).join();
109 - static String build_ressource_url(
String base,
String artifact_id,
String version,
String suffix) {
110 - var url
= new StringBuilder(base);
111 - if (
!base.endsWith(
"/"))
113 - url.append(artifact_id);
115 - url.append(version);
116 - url.append(suffix);
117 - return url.toString();
120 - static String guess_maven_artifact_url(
String base,
String group_id,
String artifact_id,
String version) {
121 - var url
= new StringBuilder(base);
122 - if (
!base.endsWith(
"/"))
124 - url.append(group_id.replaceAll(
"\\.",
"\\/"));
126 - url.append(artifact_id);
128 - url.append(version);
130 - return url.toString();
133 - static class Dependency {
141 - static <T>
int index_of(
T[] array,
T item) {
142 - var length
= array.length;
145 - for (
var index
= 0; index
< length;
++index) {
146 - var element
= array[index];
147 - if (
Objects.equals(element, item))
154 - static <T>
boolean contains(
T[] array,
T... items) {
155 - if (array.length
== 0)
157 - if (items.length
== 0)
159 - for (
var element
: array) {
160 - for (
var argument
: items) {
161 - if (element.equals(argument))
168 - static void help() {
169 - out.printf(
"Downloads an artifact from a %s repository with its %s.%n", emphasize_local(
"maven"),
170 - emphasize_local(
"transitive dependencies"));
173 - static void usage() {
174 - out.printf(
"%s: java download_jar.java <group id> <artifact id> <version>%n", emphasize_global(
"Usage"));
177 - static void exit() {
178 - out.printf(
"%s!%n", emphasize_local(
"Done"));
182 - static String emphasize_global(
String message) {
183 - return "\033[4m" + message
+ "\033[0m";
186 - static String emphasize_local(
String message) {
187 - return "\033[3m" + message
+ "\033[0m";
190 - static void fail(
String message,
Object... arguments) {
191 - out.printf(message, arguments);
195 - static void fail() {
1 +import static java.
lang.
System.
out;
5 +import java.
net.
http.
*;
6 +import java.
net.
http.
HttpResponse.
*;
7 +import java.
nio.
file.
*;
9 +import java.
util.
concurrent.
ConcurrentSkipListSet;
12 + public static void main(
final String[] command_line_arguments) {
14 + var current_working_directory
= Paths.get(
System.getProperty(
"user.dir"));
15 + var download_directory
= current_working_directory.resolve(
"downloaded_artifacts");
16 + var maven_repository
= "https://repo.maven.apache.org/maven2";
17 + var expected_count_of_arguments
= 3;
18 + if (contains(command_line_arguments,
"-d")) {
19 + expected_count_of_arguments
= expected_count_of_arguments
+ 2;
21 + var given_download_directory
= command_line_arguments[index_of(command_line_arguments,
"-d")
+ 1];
22 + download_directory
= current_working_directory.resolve(given_download_directory);
23 + }
catch (
IndexOutOfBoundsException exception) {
24 + out.println(
"The -d option requires an argument.");
29 + if (contains(command_line_arguments,
"-r")) {
30 + expected_count_of_arguments
= expected_count_of_arguments
+ 2;
32 + var given_maven_repository
= command_line_arguments[index_of(command_line_arguments,
"-r")
+ 1];
33 + maven_repository
= given_maven_repository;
34 + }
catch (
IndexOutOfBoundsException exception) {
35 + out.println(
"The -r option requires an argument.");
40 + if (command_line_arguments.length
!= expected_count_of_arguments) {
44 + var maven_coordinates_reversed
= new String[
3];
45 + for (
var index
= 2; index
>= 0;
--index) {
46 + maven_coordinates_reversed[index]
= command_line_arguments[expected_count_of_arguments
- index
- 1];
48 + var group_id
= maven_coordinates_reversed[
2];
49 + var artifact_id
= maven_coordinates_reversed[
1];
50 + var version
= maven_coordinates_reversed[
0];
52 + if (
Files.exists(download_directory)
&& Files.isRegularFile(download_directory)) {
53 + fail(
"The given download directory path %s already points to a file!%n", download_directory.toString());
54 + }
else if (
!Files.exists(download_directory)) {
55 + out.printf(
"Creating directory at %s%n.", emphasize_global(download_directory.toString()));
57 + Files.createDirectories(download_directory);
58 + }
catch (
IOException __) {
59 + fail(
"Unable to create directory at %s", emphasize_global(download_directory.toString()));
63 + var expected_artifact_base_url
= guess_maven_artifact_url(maven_repository, group_id, artifact_id, version);
64 + var client
= HttpClient.newHttpClient();
65 + var request
= HttpRequest.newBuilder().uri(
URI.create(expected_artifact_base_url)).build();
66 + var target_directory
= download_directory;
67 + client.sendAsync(request,
BodyHandlers.ofString()).thenApply(
HttpResponse::statusCode).thenAccept(status
-> {
69 + fail(
"Could not find maven artifact at %s.%n", expected_artifact_base_url);
71 + download_ressource(client, target_directory,
72 + build_ressource_url(expected_artifact_base_url, artifact_id, version,
".jar"));
73 + download_ressource(client, target_directory,
74 + build_ressource_url(expected_artifact_base_url, artifact_id, version,
"-sources.jar"));
77 + var processed_cache
= new ConcurrentSkipListSet<
String>();
78 + process_pom(client, processed_cache,
79 + build_ressource_url(expected_artifact_base_url, artifact_id, version,
".pom"));
83 + out.printf(
"download directory:\t%s%nmaven repository:\t%s%nmaven coordinates:\t%s:%s:%s%n",
84 + emphasize_global(download_directory.toString()), emphasize_global(maven_repository), emphasize_global(group_id),
85 + emphasize_global(artifact_id), emphasize_global(version));
89 + static Collection<
String>
process_pom(
HttpClient client,
Set<
String> processed,
String pom_url) {
90 + if (processed.contains(pom_url))
91 + return Collections.emptyList();
92 + var request
= HttpRequest.newBuilder().uri(
URI.create(pom_url)).build();
93 + return client.sendAsync(request,
BodyHandlers.ofInputStream()).thenApply(input
-> {
95 + processed.add(pom_url);
98 + return List.<
String>of();
102 + static void download_ressource(
HttpClient client,
Path target_directory,
String ressource_url) {
103 + var request
= HttpRequest.newBuilder().uri(
URI.create(ressource_url)).build();
104 + var ressource_name
= ressource_url.substring(ressource_url.lastIndexOf(
'/')
+ 1);
105 + var target_ressource_path
= target_directory.resolve(ressource_name);
106 + client.sendAsync(request,
BodyHandlers.ofFile(target_ressource_path)).join();
109 + static String build_ressource_url(
String base,
String artifact_id,
String version,
String suffix) {
110 + var url
= new StringBuilder(base);
111 + if (
!base.endsWith(
"/"))
113 + url.append(artifact_id);
115 + url.append(version);
116 + url.append(suffix);
117 + return url.toString();
120 + static String guess_maven_artifact_url(
String base,
String group_id,
String artifact_id,
String version) {
121 + var url
= new StringBuilder(base);
122 + if (
!base.endsWith(
"/"))
124 + url.append(group_id.replaceAll(
"\\.",
"\\/"));
126 + url.append(artifact_id);
128 + url.append(version);
130 + return url.toString();
133 + static class Dependency {
141 + static <T>
int index_of(
T[] array,
T item) {
142 + var length
= array.length;
145 + for (
var index
= 0; index
< length;
++index) {
146 + var element
= array[index];
147 + if (
Objects.equals(element, item))
154 + static <T>
boolean contains(
T[] array,
T... items) {
155 + if (array.length
== 0)
157 + if (items.length
== 0)
159 + for (
var element
: array) {
160 + for (
var argument
: items) {
161 + if (element.equals(argument))
168 + static void help() {
169 + out.printf(
"Downloads an artifact from a %s repository with its %s.%n", emphasize_local(
"maven"),
170 + emphasize_local(
"transitive dependencies"));
173 + static void usage() {
174 + out.printf(
"%s: java download_jar.java <group id> <artifact id> <version>%n", emphasize_global(
"Usage"));
177 + static void exit() {
178 + out.printf(
"%s!%n", emphasize_local(
"Done"));
182 + static String emphasize_global(
String message) {
183 + return "\033[4m" + message
+ "\033[0m";
186 + static String emphasize_local(
String message) {
187 + return "\033[3m" + message
+ "\033[0m";
190 + static void fail(
String message,
Object... arguments) {
191 + out.printf(message, arguments);
195 + static void fail() {