1
1
/*
2
2
* This file is part of fabric-loom, licensed under the MIT License (MIT).
3
3
*
4
- * Copyright (c) 2021-2023 FabricMC
4
+ * Copyright (c) 2021-2024 FabricMC
5
5
*
6
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
* of this software and associated documentation files (the "Software"), to deal
35
35
import org .gradle .api .Project ;
36
36
import org .gradle .api .artifacts .Configuration ;
37
37
import org .gradle .api .artifacts .Dependency ;
38
+ import org .gradle .api .artifacts .DependencyResolveDetails ;
38
39
import org .gradle .api .artifacts .ModuleDependency ;
40
+ import org .gradle .api .artifacts .ModuleVersionSelector ;
39
41
import org .gradle .api .artifacts .dsl .DependencyHandler ;
40
42
import org .gradle .api .attributes .Attribute ;
41
43
import org .gradle .api .file .FileCollection ;
44
+ import org .jetbrains .annotations .VisibleForTesting ;
42
45
43
46
/**
44
47
* Simplified but powerful dependency downloading.
45
48
*
46
49
* @author Juuz
47
50
*/
48
51
public final class DependencyDownloader {
52
+ private static final String LOG4J_GROUP = "org.apache.logging.log4j" ;
53
+ private static final String LOG4J_NAME = "log4j-core" ;
54
+ private static final String LOG4J_MINIMUM_VERSION = "2.17.1" ;
55
+ private static final int [] LOG4J_MINIMUM_VERSION_COMPONENTS = {2 , 17 , 1 };
56
+
49
57
private final Project project ;
50
58
private final List <DependencyEntry > dependencies = new ArrayList <>();
51
59
private final Map <Attribute <?>, Object > attributes = new HashMap <>();
@@ -133,6 +141,7 @@ public FileCollection download(boolean transitive, boolean resolve) {
133
141
attributes .attribute ((Attribute <Object >) attribute , value );
134
142
});
135
143
});
144
+ config .getResolutionStrategy ().eachDependency (DependencyDownloader ::upgradeLog4j );
136
145
FileCollection files = config .fileCollection (dep -> true );
137
146
138
147
if (resolve ) {
@@ -142,6 +151,49 @@ public FileCollection download(boolean transitive, boolean resolve) {
142
151
return files ;
143
152
}
144
153
154
+ private static void upgradeLog4j (DependencyResolveDetails details ) {
155
+ ModuleVersionSelector requested = details .getRequested ();
156
+
157
+ if (LOG4J_GROUP .equals (requested .getGroup ()) && LOG4J_NAME .equals (requested .getName ())) {
158
+ final String requestedVersion = requested .getVersion ();
159
+
160
+ if (requestedVersion != null && shouldUpgradeLog4jVersion (requestedVersion )) {
161
+ details .useVersion (LOG4J_MINIMUM_VERSION );
162
+ }
163
+ }
164
+ }
165
+
166
+ @ VisibleForTesting
167
+ public static boolean shouldUpgradeLog4jVersion (String requestedVersion ) {
168
+ final String [] splitVersion = requestedVersion .split ("\\ ." );
169
+
170
+ for (int i = 0 ; i < LOG4J_MINIMUM_VERSION_COMPONENTS .length ; i ++) {
171
+ if (i >= splitVersion .length ) {
172
+ // Not enough version components in the requested version, upgrade just to be sure.
173
+ return true ;
174
+ }
175
+
176
+ final int minimumComponent = LOG4J_MINIMUM_VERSION_COMPONENTS [i ];
177
+ final String givenComponentStr = splitVersion [i ];
178
+ final int givenComponent ;
179
+
180
+ try {
181
+ givenComponent = Integer .parseInt (givenComponentStr );
182
+ } catch (NumberFormatException e ) {
183
+ // We can't read the version component for comparing, upgrade just to be sure.
184
+ return true ;
185
+ }
186
+
187
+ if (givenComponent < minimumComponent ) {
188
+ // Too old, upgrade.
189
+ return true ;
190
+ }
191
+ }
192
+
193
+ // Seems to be new enough, let's not upgrade.
194
+ return false ;
195
+ }
196
+
145
197
/**
146
198
* Resolves a dependency as well as its transitive dependencies into a {@link FileCollection}.
147
199
*
0 commit comments