Skip to content

Commit ff1e5e6

Browse files
committed
docs: added JavaDocs
1 parent 032384e commit ff1e5e6

File tree

9 files changed

+208
-24
lines changed

9 files changed

+208
-24
lines changed

src/main/java/org/codejive/jpm/Jpm.java

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
import java.nio.file.Path;
55
import java.util.*;
66
import org.codejive.jpm.json.AppInfo;
7-
import org.codejive.jpm.util.FileUtils;
8-
import org.codejive.jpm.util.ResolverUtils;
9-
import org.codejive.jpm.util.SearchUtils;
10-
import org.codejive.jpm.util.SyncStats;
7+
import org.codejive.jpm.util.*;
118
import org.eclipse.aether.artifact.Artifact;
129
import org.eclipse.aether.resolution.DependencyResolutionException;
1310

11+
/** The class implementing all the jpm command actions. */
1412
public class Jpm {
1513
private final Path directory;
1614
private final boolean noLinks;
@@ -20,41 +18,81 @@ private Jpm(Path directory, boolean noLinks) {
2018
this.noLinks = noLinks;
2119
}
2220

21+
/**
22+
* Create a new {@link Builder} instance for the {@link Jpm} class.
23+
*
24+
* @return A new {@link Builder} instance.
25+
*/
2326
public static Builder builder() {
2427
return new Builder();
2528
}
2629

30+
/** Builder class for the {@link Jpm} class. */
2731
public static class Builder {
2832
private Path directory;
2933
private boolean noLinks;
3034

3135
private Builder() {}
3236

37+
/**
38+
* Set the target directory to use for the jpm commands.
39+
*
40+
* @param directory The target directory.
41+
* @return The builder instance for chaining.
42+
*/
3343
public Builder directory(Path directory) {
3444
this.directory = directory;
3545
return this;
3646
}
3747

48+
/**
49+
* Set whether to create symbolic links or not.
50+
*
51+
* @param noLinks Whether to create symbolic links or not.
52+
* @return The builder instance for chaining.
53+
*/
3854
public Builder noLinks(boolean noLinks) {
3955
this.noLinks = noLinks;
4056
return this;
4157
}
4258

59+
/**
60+
* Builds the {@link Jpm} instance.
61+
*
62+
* @return A {@link Jpm} instance.
63+
*/
4364
public Jpm build() {
4465
return new Jpm(directory, noLinks);
4566
}
4667
}
4768

69+
/**
70+
* Copies the given artifacts to the target directory.
71+
*
72+
* @param artifactNames The artifacts to copy.
73+
* @param sync Whether to sync the target directory or not.
74+
* @return An instance of {@link SyncStats} containing the statistics of the copy operation.
75+
* @throws IOException If an error occurred during the copy operation.
76+
* @throws DependencyResolutionException If an error occurred during the dependency resolution.
77+
*/
4878
public SyncStats copy(String[] artifactNames, boolean sync)
4979
throws IOException, DependencyResolutionException {
5080
List<Path> files = ResolverUtils.resolveArtifactPaths(artifactNames);
5181
return FileUtils.syncArtifacts(files, directory, noLinks, !sync);
5282
}
5383

84+
/**
85+
* Searches for artifacts matching the given pattern.
86+
*
87+
* @param artifactPattern The pattern to search for.
88+
* @param count The maximum number of results to return.
89+
* @return An array of artifact names matching the given pattern.
90+
* @throws IOException If an error occurred during the search.
91+
*/
5492
public String[] search(String artifactPattern, int count) throws IOException {
5593
List<Artifact> artifacts = new ArrayList<>();
5694
int max = count <= 0 || count > 200 ? 200 : count;
57-
SearchUtils.SearchResult result = SearchUtils.findArtifacts(artifactPattern, max);
95+
SearchResult result = SearchUtils.findArtifacts(artifactPattern, max);
5896
while (result != null) {
5997
artifacts.addAll(result.artifacts);
6098
result = count <= 0 ? SearchUtils.findNextArtifacts(result) : null;
@@ -66,6 +104,17 @@ private static String artifactGav(Artifact artifact) {
66104
return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();
67105
}
68106

107+
/**
108+
* Installs the given artifacts to the target directory while also registering them as
109+
* dependencies in the app.json file in the current directory. If no artifacts are given, all
110+
* dependencies in the app.json file will be installed. NB: "installation" in this context
111+
* basically means sync-copying the artifacts to the target directory.
112+
*
113+
* @param artifactNames The artifacts to install.
114+
* @return An instance of {@link SyncStats} containing the statistics of the install operation.
115+
* @throws IOException If an error occurred during the install operation.
116+
* @throws DependencyResolutionException If an error occurred during the dependency resolution.
117+
*/
69118
public SyncStats install(String[] artifactNames)
70119
throws IOException, DependencyResolutionException {
71120
AppInfo appInfo = AppInfo.read();
@@ -88,6 +137,15 @@ public SyncStats install(String[] artifactNames)
88137
}
89138
}
90139

140+
/**
141+
* Returns the paths of the given artifacts. If no artifacts are given, the paths for all
142+
* dependencies in the app.json file will be returned instead.
143+
*
144+
* @param artifactNames The artifacts to get the paths for.
145+
* @return A list of paths.
146+
* @throws DependencyResolutionException If an error occurred during the dependency resolution.
147+
* @throws IOException If an error occurred during the operation.
148+
*/
91149
public List<Path> path(String[] artifactNames)
92150
throws DependencyResolutionException, IOException {
93151
AppInfo appInfo = AppInfo.read();

src/main/java/org/codejive/jpm/Main.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import picocli.CommandLine.Option;
2222
import picocli.CommandLine.Parameters;
2323

24+
/** Main class for the jpm command line tool. */
2425
@Command(
2526
name = "jpm",
2627
mixinStandardHelpOptions = true,
@@ -204,6 +205,11 @@ private static void printStats(SyncStats stats) {
204205
stats.copied, stats.updated, stats.deleted);
205206
}
206207

208+
/**
209+
* Main entry point for the jpm command line tool.
210+
*
211+
* @param args The command line arguments.
212+
*/
207213
public static void main(String... args) {
208214
new CommandLine(new Main()).execute(args);
209215
}

src/main/java/org/codejive/jpm/json/AppInfo.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,35 @@
1010
import java.util.Map;
1111
import java.util.TreeMap;
1212

13+
/**
14+
* Represents the contents of an app.json file. There are methods for reading and writing instances
15+
* from/to files.
16+
*/
1317
public class AppInfo {
1418
private Map<String, Object> json;
1519
public Map<String, String> dependencies = new TreeMap<>();
1620

21+
/** The official name of the app.json file. */
1722
public static final String APP_INFO_FILE = "app.json";
1823

24+
/**
25+
* Returns the dependencies as an array of strings in the format "groupId:artifactId:version".
26+
*
27+
* @return An array of strings
28+
*/
1929
public String[] getDependencyGAVs() {
2030
return dependencies.entrySet().stream()
2131
.map(e -> e.getKey() + ":" + e.getValue())
2232
.toArray(String[]::new);
2333
}
2434

35+
/**
36+
* Reads the app.json file in the current directory and returns its content as an AppInfo
37+
* object.
38+
*
39+
* @return An instance of AppInfo
40+
* @throws IOException if an error occurred while reading or parsing the file
41+
*/
2542
public static AppInfo read() throws IOException {
2643
Path prjJson = Path.of(APP_INFO_FILE);
2744
AppInfo appInfo = new AppInfo();
@@ -41,6 +58,12 @@ public static AppInfo read() throws IOException {
4158
return appInfo;
4259
}
4360

61+
/**
62+
* Writes the AppInfo object to the app.json file in the current directory.
63+
*
64+
* @param appInfo The AppInfo object to write
65+
* @throws IOException if an error occurred while writing the file
66+
*/
4467
public static void write(AppInfo appInfo) throws IOException {
4568
Path prjJson = Path.of(APP_INFO_FILE);
4669
try (Writer out = Files.newBufferedWriter(prjJson)) {

src/main/java/org/codejive/jpm/util/FileUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@
99
import java.util.List;
1010
import java.util.Set;
1111

12+
/** Utility class for file operations. */
1213
public class FileUtils {
14+
15+
/**
16+
* Synchronizes a list of artifacts with a target directory.
17+
*
18+
* @param artifacts list of artifacts to synchronize
19+
* @param directory target directory
20+
* @param noLinks if true, copy artifacts instead of creating symbolic links
21+
* @param noDelete if true, do not delete artifacts that are no longer needed
22+
* @return An instance of {@link SyncStats} with statistics about the synchronization
23+
* @throws IOException if an error occurred during the synchronization
24+
*/
1325
public static SyncStats syncArtifacts(
1426
List<Path> artifacts, Path directory, boolean noLinks, boolean noDelete)
1527
throws IOException {

src/main/java/org/codejive/jpm/util/ResolverUtils.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,42 @@
1818
import org.eclipse.aether.resolution.DependencyResult;
1919
import org.eclipse.aether.util.artifact.JavaScopes;
2020

21+
/** Utility class for resolving Maven artifacts. */
2122
public class ResolverUtils {
23+
/**
24+
* Resolves the paths of the given artifacts. Handles parsing and resolving of artifacts and
25+
* extracts their paths.
26+
*
27+
* @param artifactNames the artifacts to resolve as an array of strings in the format
28+
* "groupId:artifactId:version"
29+
* @return the paths of the resolved artifacts
30+
* @throws DependencyResolutionException if an error occurs while resolving the artifacts
31+
*/
2232
public static List<Path> resolveArtifactPaths(String[] artifactNames)
2333
throws DependencyResolutionException {
2434
List<Artifact> artifacts = parseArtifacts(artifactNames);
2535
List<ArtifactResult> resolvedArtifacts = resolveArtifacts(artifacts);
2636
return resolvedArtifacts.stream().map(ar -> ar.getArtifact().getFile().toPath()).toList();
2737
}
2838

39+
/**
40+
* Parses the given artifact names into a list of {@link Artifact} instances.
41+
*
42+
* @param artifactNames the artifact names to parse as an array of strings in the format
43+
* "groupId:artifactId:version"
44+
* @return a list of {@link Artifact} instances
45+
*/
2946
public static List<Artifact> parseArtifacts(String[] artifactNames) {
3047
return Arrays.stream(artifactNames).map(DefaultArtifact::new).collect(Collectors.toList());
3148
}
3249

50+
/**
51+
* Resolves the given artifacts.
52+
*
53+
* @param artifacts the artifacts to resolve as a list of {@link Artifact} instances
54+
* @return the resolved artifacts as a list of {@link ArtifactResult} instances
55+
* @throws DependencyResolutionException if an error occurs while resolving the artifacts
56+
*/
3357
public static List<ArtifactResult> resolveArtifacts(List<Artifact> artifacts)
3458
throws DependencyResolutionException {
3559
List<Dependency> dependencies =
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.codejive.jpm.util;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import org.eclipse.aether.artifact.Artifact;
6+
7+
/** Hold the result of a search while also functioning as a kind of bookmark for paging purposes. */
8+
public class SearchResult {
9+
/** The artifacts that matched the search query. */
10+
public final List<? extends Artifact> artifacts;
11+
12+
/** The search query that produced this result. */
13+
public final String query;
14+
15+
/** The index of the first artifact in this result relative to the total result set. */
16+
public final int start;
17+
18+
/** The maximum number of results to return */
19+
public final int count;
20+
21+
/** The total number of artifacts that matched the search query. */
22+
public final int total;
23+
24+
/**
25+
* Create a new search result.
26+
*
27+
* @param artifacts The artifacts that matched the search query.
28+
* @param query The search query that produced this result.
29+
* @param start The index of the first artifact in this result relative to the total result set.
30+
* @param count The maximum number of results to return.
31+
* @param total The total number of artifacts that matched the search query.
32+
*/
33+
public SearchResult(
34+
List<? extends Artifact> artifacts, String query, int start, int count, int total) {
35+
this.artifacts = Collections.unmodifiableList(artifacts);
36+
this.query = query;
37+
this.start = start;
38+
this.count = count;
39+
this.total = total;
40+
}
41+
}

src/main/java/org/codejive/jpm/util/SearchUtils.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,41 @@
66
import java.io.InputStream;
77
import java.io.InputStreamReader;
88
import java.net.URLEncoder;
9-
import java.util.Collections;
109
import java.util.List;
1110
import org.apache.http.client.methods.CloseableHttpResponse;
1211
import org.apache.http.client.methods.HttpGet;
1312
import org.apache.http.impl.client.CloseableHttpClient;
1413
import org.apache.http.impl.client.HttpClients;
15-
import org.eclipse.aether.artifact.Artifact;
1614
import org.eclipse.aether.artifact.DefaultArtifact;
1715

16+
/** Utility class for searching Maven artifacts. */
1817
public class SearchUtils {
1918

20-
public static class SearchResult {
21-
public final List<? extends Artifact> artifacts;
22-
public final String query;
23-
public final int start;
24-
public final int count;
25-
public final int total;
26-
27-
public SearchResult(
28-
List<? extends Artifact> artifacts, String query, int start, int count, int total) {
29-
this.artifacts = Collections.unmodifiableList(artifacts);
30-
this.query = query;
31-
this.start = start;
32-
this.count = count;
33-
this.total = total;
34-
}
35-
}
36-
19+
/**
20+
* Find artifacts matching the given pattern. This will return the first page of results. If the
21+
* pattern to search for is a simple name (there are no colons in the string), the search will
22+
* match any part of an artifact's group or name. If there's a single colon, the search will
23+
* match any part of the group id and artifact id separately. If there are two colons, the
24+
* search will match the group id and artifact id exactly, and will return the artifact's
25+
* versions.
26+
*
27+
* @param artifactPattern The pattern to search for.
28+
* @param count The maximum number of results to return.
29+
* @return The search result as an instance of {@link SearchResult}.
30+
* @throws IOException If an error occurred during the search.
31+
*/
3732
public static SearchResult findArtifacts(String artifactPattern, int count) throws IOException {
3833
return select(artifactPattern, 0, count);
3934
}
4035

36+
/**
37+
* Find the next page of artifacts. This takes a {@link SearchResult} returned by a previous
38+
* call to {@link #findArtifacts(String, int)} and returns the next page of results.
39+
*
40+
* @param prevResult The previous search result.
41+
* @return The next search result as an instance of {@link SearchResult}.
42+
* @throws IOException If an error occurred during the search.
43+
*/
4144
public static SearchResult findNextArtifacts(SearchResult prevResult) throws IOException {
4245
if (prevResult.start + prevResult.count >= prevResult.total) {
4346
return null;
@@ -51,10 +54,14 @@ private static SearchResult select(String query, int start, int count) throws IO
5154
String[] parts = query.split(":", -1);
5255
String finalQuery;
5356
if (parts.length >= 3) {
57+
// Exact group/artifact match for retrieving versions
5458
finalQuery = "g:%s AND a:%s".formatted(parts[0], parts[1]);
5559
} else if (parts.length == 2) {
60+
// Partial group/artifact match, we will filter the results
61+
// to remove those that match an inverted artifact/group
5662
finalQuery = "%s AND %s".formatted(parts[0], parts[1]);
5763
} else {
64+
// Simple partial match
5865
finalQuery = query;
5966
}
6067
String searchUrl =
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package org.codejive.jpm.util;
22

3+
/** Utility class for keeping track of synchronization statistics. */
34
public class SyncStats {
5+
/** The number of new artifacts that were copied. */
46
public int copied;
7+
8+
/** The number of existing artifacts that were updated. */
59
public int updated;
10+
11+
/** The number of existing artifacts that were deleted. */
612
public int deleted;
713
}

0 commit comments

Comments
 (0)