Skip to content

Commit a085ca8

Browse files
committed
Experimental support for using project repositories for maven artifacts
This allows the Mavenizer to "download" local Forge builds from local file repositories. This CLI flag is EXPERIMENTAL and can change at any point in the near future. I'm pushing this commit early in order to test some essential changes in ForgeGradle 7.
1 parent 9e54e2c commit a085ca8

File tree

7 files changed

+127
-47
lines changed

7 files changed

+127
-47
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ tasks.named('jar', Jar) {
5858

5959
tasks.named('shadowJar', ShadowJar) {
6060
archiveClassifier = ''
61+
62+
minimize {
63+
exclude(dependency(libs.jver))
64+
}
6165
}
6266

6367
changelog {

settings.gradle

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,32 @@ gradle.beforeProject { Project project ->
1717
}
1818
}
1919

20-
dependencyResolutionManagement {
21-
//@formatter:off
22-
versionCatalogs.register('libs') {
23-
plugin 'licenser', 'net.minecraftforge.licenser' version '1.2.0'
24-
plugin 'gradleutils', 'net.minecraftforge.gradleutils' version '3.3.18'
25-
plugin 'gitversion', 'net.minecraftforge.gitversion' version '3.1.1'
26-
plugin 'changelog', 'net.minecraftforge.changelog' version '3.1.2'
27-
plugin 'shadow', 'com.gradleup.shadow' version '9.2.2'
20+
//formatter:off
21+
dependencyResolutionManagement.versionCatalogs.register('libs') {
22+
plugin 'licenser', 'net.minecraftforge.licenser' version '1.2.0'
23+
plugin 'gradleutils', 'net.minecraftforge.gradleutils' version '3.3.18'
24+
plugin 'gitversion', 'net.minecraftforge.gitversion' version '3.1.1'
25+
plugin 'changelog', 'net.minecraftforge.changelog' version '3.1.2'
26+
plugin 'shadow', 'com.gradleup.shadow' version '9.2.2'
2827

29-
// Static Analysis
30-
library 'nulls', 'org.jetbrains', 'annotations' version '26.0.2'
28+
// Static Analysis
29+
library 'nulls', 'org.jetbrains', 'annotations' version '26.0.2'
3130

32-
library 'gson', 'com.google.code.gson', 'gson' version '2.10.1'
33-
library 'jopt', 'net.sf.jopt-simple', 'jopt-simple' version '6.0-alpha-3'
34-
library 'jver', 'net.minecraftforge', 'java-provisioner' version '1.0.8'
35-
library 'srgutils', 'net.minecraftforge', 'srgutils' version '0.5.14'
36-
library 'diff', 'io.codechicken', 'DiffPatch' version '2.0.0.36' // Fuzzy patching
31+
library 'gson', 'com.google.code.gson', 'gson' version '2.10.1'
32+
library 'jopt', 'net.sf.jopt-simple', 'jopt-simple' version '6.0-alpha-3'
33+
library 'jver', 'net.minecraftforge', 'java-provisioner' version '1.0.8'
34+
library 'srgutils', 'net.minecraftforge', 'srgutils' version '0.5.14'
35+
library 'diff', 'io.codechicken', 'DiffPatch' version '2.0.0.36' // Fuzzy patching
3736

38-
library 'fastcsv', 'de.siegmar', 'fastcsv' version '3.4.0'
39-
library 'commonsio', 'commons-io', 'commons-io' version '2.18.0'
37+
library 'fastcsv', 'de.siegmar', 'fastcsv' version '3.4.0'
38+
library 'commonsio', 'commons-io', 'commons-io' version '2.18.0'
4039

41-
// Utilities
42-
library 'utils-download', 'net.minecraftforge', 'download-utils' version '0.3.1'
43-
library 'utils-files', 'net.minecraftforge', 'file-utils' version '0.3.1'
44-
library 'utils-hash', 'net.minecraftforge', 'hash-utils' version '0.1.9'
45-
library 'utils-data', 'net.minecraftforge', 'json-data-utils' version '0.2.3'
46-
library 'utils-logging', 'net.minecraftforge', 'log-utils' version '0.3.1'
47-
bundle 'utils', ['utils-download', 'utils-files', 'utils-hash', 'utils-data', 'utils-logging']
48-
}
49-
//@formatter:on
40+
// Utilities
41+
library 'utils-download', 'net.minecraftforge', 'download-utils' version '0.3.1'
42+
library 'utils-files', 'net.minecraftforge', 'file-utils' version '0.3.1'
43+
library 'utils-hash', 'net.minecraftforge', 'hash-utils' version '0.1.9'
44+
library 'utils-data', 'net.minecraftforge', 'json-data-utils' version '0.2.3'
45+
library 'utils-logging', 'net.minecraftforge', 'log-utils' version '0.3.1'
46+
bundle 'utils', ['utils-download', 'utils-files', 'utils-hash', 'utils-data', 'utils-logging']
5047
}
48+
//formatter:on

src/main/java/net/minecraftforge/mcmaven/cli/MavenTask.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ static void run(String[] args) throws Exception {
7575
.availableUnless(mappingsO)
7676
.withRequiredArg();
7777

78+
var foreignRepositoryO = parser.accepts("repository",
79+
"EXPERIMENTAL: URL of a foreign maven repository to use for dependencies. The format is \"name,url\". The name must not include any commas.")
80+
.withRequiredArg().ofType(String.class);
81+
7882
var globalAuxiliaryVariantsO = parser.accepts("global-auxiliary-variants",
7983
"Declares sources and javadoc jars as global variants, no matter the mapping version. This is used to work around gradle/gradle#35065");
8084

@@ -142,7 +146,13 @@ static void run(String[] args) throws Exception {
142146
? new ParchmentMappings(options.valueOf(parchmentO))
143147
: Mappings.of(options.valueOf(mappingsO));
144148

145-
var mcmaven = new MinecraftMaven(output, cache, jdkCache, mappings, options.has(globalAuxiliaryVariantsO));
149+
var foreignRepositories = new HashMap<String, String>();
150+
for (var s : options.valuesOf(foreignRepositoryO)) {
151+
var split = s.split(",", 2);
152+
foreignRepositories.put(split[0], split[1]);
153+
}
154+
155+
var mcmaven = new MinecraftMaven(output, cache, jdkCache, mappings, foreignRepositories, options.has(globalAuxiliaryVariantsO));
146156
mcmaven.run(artifact);
147157
}
148158
}

src/main/java/net/minecraftforge/mcmaven/impl/MinecraftMaven.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
import java.util.HashMap;
2525
import java.util.HashSet;
2626
import java.util.List;
27+
import java.util.Map;
2728

2829
// TODO [MCMavenizer][Deobf] ADD DEOBF
2930
// use single detached configuration to resolve individual configurations
3031
// pass in downloaded files to mcmaven (absolute path)
31-
public record MinecraftMaven(File output, Cache cache, Mappings mappings, boolean globalAuxiliaryVariants) {
32+
public record MinecraftMaven(File output, Cache cache, Mappings mappings, Map<String, String> foreignRepositories, boolean globalAuxiliaryVariants) {
3233
private static final ComparableVersion MIN_SUPPORTED_FORGE = new ComparableVersion("1.14.4"); // Only 1.14.4+ has official mappings, we can support more when we add more mappings
3334

34-
public MinecraftMaven(File output, File cacheRoot, File jdkCacheRoot, Mappings mappings, boolean globalAuxiliaryVariants) {
35-
this(output, new Cache(cacheRoot, jdkCacheRoot), mappings, globalAuxiliaryVariants);
35+
public MinecraftMaven(File output, File cacheRoot, File jdkCacheRoot, Mappings mappings, Map<String, String> foreignRepositories, boolean globalAuxiliaryVariants) {
36+
this(output, new Cache(cacheRoot, jdkCacheRoot, foreignRepositories), mappings, foreignRepositories, globalAuxiliaryVariants);
3637
}
3738

3839
public MinecraftMaven {
@@ -42,6 +43,8 @@ public MinecraftMaven(File output, File cacheRoot, File jdkCacheRoot, Mappings m
4243
Log.info(" Offline: " + GlobalOptions.isOffline());
4344
Log.info(" Cache Only: " + GlobalOptions.isCacheOnly());
4445
Log.info(" Mappings: " + mappings);
46+
if (!foreignRepositories.isEmpty())
47+
Log.info(" Foreign Repos: [" + String.join(", ", foreignRepositories.values()) + ']');
4548
Log.info(" GradleVariantHack: " + globalAuxiliaryVariants);
4649
Log.info();
4750
}

src/main/java/net/minecraftforge/mcmaven/impl/cache/Cache.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraftforge.mcmaven.impl.util.Constants;
88

99
import java.io.File;
10+
import java.util.Map;
1011

1112
/** Represents the cache for this tool. */
1213
public record Cache(File root, JDKCache jdks, MavenCache maven, MinecraftMavenCache minecraft) {
@@ -17,6 +18,10 @@ public record Cache(File root, JDKCache jdks, MavenCache maven, MinecraftMavenCa
1718
* @param jdkCache The JDK cache directory.
1819
*/
1920
public Cache(File root, File jdkCache) {
20-
this(root, new JDKCache(jdkCache), new MavenCache(Constants.FORGE_NAME, Constants.FORGE_MAVEN, root), new MinecraftMavenCache(root));
21+
this(root, jdkCache, Map.of());
22+
}
23+
24+
public Cache(File root, File jdkCache, Map<String, String> foreignRepositories) {
25+
this(root, new JDKCache(jdkCache), new MavenCache(Constants.FORGE_NAME, Constants.FORGE_MAVEN, root, foreignRepositories), new MinecraftMavenCache(root));
2126
}
2227
}

src/main/java/net/minecraftforge/mcmaven/impl/cache/MavenCache.java

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.minecraftforge.mcmaven.impl.util.Util;
1212
import net.minecraftforge.util.hash.HashUtils;
1313
import net.minecraftforge.util.logging.Log;
14+
import org.jetbrains.annotations.ApiStatus;
1415
import org.w3c.dom.Document;
1516
import org.w3c.dom.NodeList;
1617
import org.xml.sax.SAXException;
@@ -22,7 +23,9 @@
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.util.ArrayList;
26+
import java.util.LinkedList;
2527
import java.util.List;
28+
import java.util.Map;
2629

2730
// TODO: [MCMavenizer][MavenCache] Handle download failures properly
2831
/** Represents the maven cache for this tool. */
@@ -37,6 +40,7 @@ public sealed class MavenCache permits MinecraftMavenCache {
3740
private final HashFunction[] knownHashes;
3841
private final File cache;
3942
private final String repo;
43+
private final List<MavenCache> foreignRepositories;
4044

4145
/**
4246
* Initializes a new maven cache with the given name, repository, and cache directory.
@@ -49,9 +53,24 @@ public MavenCache(String name, String repo, File root) {
4953
this(name, repo, root, DEFAULT_HASHES);
5054
}
5155

56+
@ApiStatus.Experimental
57+
public MavenCache(String name, String repo, File root, Map<String, String> foreignRepositories) {
58+
this(name, repo, root, foreignRepositories, DEFAULT_HASHES);
59+
}
60+
5261
public MavenCache(String name, String repo, File root, HashFunction... knownHashes) {
62+
this(name, repo, root, Map.of(), knownHashes);
63+
}
64+
65+
public MavenCache(String name, String repo, File root, Map<String, String> foreignRepositories, HashFunction... knownHashes) {
5366
this.cache = new File(root, "maven/" + name);
5467
this.repo = repo;
68+
this.foreignRepositories = new ArrayList<>(foreignRepositories.size());
69+
for (var entry : foreignRepositories.entrySet()) {
70+
var n = entry.getKey();
71+
var r = entry.getValue();
72+
this.foreignRepositories.add(new MavenCache(n, r, root));
73+
}
5574
this.knownHashes = knownHashes;
5675
}
5776

@@ -68,8 +87,22 @@ public final File getFolder() {
6887
* @throws IOException If an error occurs while downloading the file
6988
*/
7089
@SuppressWarnings("JavadocDeclaration") // IOException thrown by Util.sneak
71-
public File download(Artifact artifact) {
72-
return download(false, artifact.getPath());
90+
public final File download(Artifact artifact) {
91+
try {
92+
return download(false, artifact.getPath());
93+
} catch (Exception e) {
94+
if (!this.foreignRepositories.isEmpty()) {
95+
for (var repo : this.foreignRepositories) {
96+
try {
97+
return repo.download(artifact);
98+
} catch (Exception s) {
99+
e.addSuppressed(s);
100+
}
101+
}
102+
}
103+
104+
return Util.sneak(e);
105+
}
73106
}
74107

75108
/**
@@ -83,7 +116,21 @@ public File download(Artifact artifact) {
83116
*/
84117
@SuppressWarnings("JavadocDeclaration") // IOException thrown by Util.sneak
85118
public final File downloadMeta(Artifact artifact) {
86-
return download(true, artifact.getGroup().replace('.', '/') + '/' + artifact.getName() + "/maven-metadata.xml");
119+
try {
120+
return download(true, artifact.getGroup().replace('.', '/') + '/' + artifact.getName() + "/maven-metadata.xml");
121+
} catch (Exception e) {
122+
if (!this.foreignRepositories.isEmpty()) {
123+
for (var repo : this.foreignRepositories) {
124+
try {
125+
return repo.downloadMeta(artifact);
126+
} catch (Exception s) {
127+
e.addSuppressed(s);
128+
}
129+
}
130+
}
131+
132+
return Util.sneak(e);
133+
}
87134
}
88135

89136
/**
@@ -96,7 +143,21 @@ public final File downloadMeta(Artifact artifact) {
96143
*/
97144
@SuppressWarnings("JavadocDeclaration") // IOException thrown by Util.sneak
98145
public final File downloadVersionMeta(Artifact artifact) {
99-
return download(true, artifact.getFolder() + "/maven-metadata.xml");
146+
try {
147+
return download(true, artifact.getFolder() + "/maven-metadata.xml");
148+
} catch (Exception e) {
149+
if (!this.foreignRepositories.isEmpty()) {
150+
for (var repo : this.foreignRepositories) {
151+
try {
152+
return repo.downloadVersionMeta(artifact);
153+
} catch (Exception s) {
154+
e.addSuppressed(s);
155+
}
156+
}
157+
}
158+
159+
return Util.sneak(e);
160+
}
100161
}
101162

102163
/**
@@ -108,8 +169,7 @@ public final File downloadVersionMeta(Artifact artifact) {
108169
*
109170
* @throws IOException If an error occurs while downloading the file
110171
*/
111-
@SuppressWarnings("JavadocDeclaration") // IOException thrown by Util.sneak
112-
protected File download(boolean changing, String path) {
172+
protected File download(boolean changing, String path) throws IOException {
113173
var target = new File(cache, path);
114174

115175
if (target.exists()) {
@@ -183,15 +243,11 @@ protected File download(boolean changing, String path) {
183243
target.delete();
184244
}
185245

186-
try {
187-
GlobalOptions.assertNotCacheOnly();
188-
GlobalOptions.assertOnline();
189-
downloadFile(target, path);
190-
HashUtils.updateHash(target, knownHashes);
191-
return target;
192-
} catch (IOException e) {
193-
return Util.sneak(e);
194-
}
246+
GlobalOptions.assertNotCacheOnly();
247+
GlobalOptions.assertOnline();
248+
downloadFile(target, path);
249+
HashUtils.updateHash(target, knownHashes);
250+
return target;
195251
}
196252

197253
/**

src/main/java/net/minecraftforge/mcmaven/impl/cache/MinecraftMavenCache.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ public MinecraftMavenCache(File root) {
4343
*/
4444
@SuppressWarnings("JavadocDeclaration") // IOException thrown by Util.sneak
4545
public File download(MinecraftVersion.LibraryDownload lib) {
46-
return this.download(false, lib.path);
46+
try {
47+
return this.download(false, lib.path);
48+
} catch (Exception e) {
49+
return Util.sneak(e);
50+
}
4751
}
4852

4953
@Override

0 commit comments

Comments
 (0)