Skip to content

Commit 399ab7f

Browse files
chore: migrate from agent args to system environment variables
1 parent 4cec607 commit 399ab7f

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@ This is a java agent for Gradle `distributionUrl` property global mirror setting
66

77
1. Download the agent jar [here](https://github.com/CarefulDeveloper/GradleAgent/releases/latest/download/GradleAgent.jar)
88

9-
2. edit the system environment variable like this:
10-
9+
2. make this agent jar work
10+
11+
if you want to it worked in command line at terminal, you need to add this system environment variable:
12+
1113
```
12-
GRADLE_OPTS = -javaagent:C:\path\to\GradleAgent.jar=https://www.mirrror.host.com/gradle/gradle-%1$s-%2$s.zip
14+
GRADLE_OPTS = -javaagent:C:\path\to\GradleAgent.jar
15+
```
16+
17+
if you want to it worked in JetBrains IDE, you need follow [the documentation](https://www.jetbrains.com/help/idea/tuning-the-ide.html#procedure-jvm-options) and append new line in `*.vmoptions` with:
18+
19+
```
20+
-javaagent:C:\path\to\GradleAgent.jar
1321
```
14-
`%1$s` will be fill with the gradle version info, like `8.8`; `%2$s` will be filled with distribution type, like `all` `bin`.
15-
16-
3. edit JetBrains IDE VM Options(optional, support for JetBrains IDE)
1722

18-
follow [the documentation](https://www.jetbrains.com/help/idea/tuning-the-ide.html#procedure-jvm-options), then append new line with:
23+
3. config gradle distribution mirror url template:
1924

25+
add system environment variable:
2026
```
21-
-javaagent:C:\path\to\GradleAgent.jar=https://www.mirrror.host.com/gradle/gradle-%1$s-%2$s.zip
27+
GRADLE_DISTRIBUTION_URL_TEMPLATE = https://www.mirrror.host.com/gradle/gradle-%1$s-%2$s.zip
2228
```
29+
`%1$s` will be fill with the gradle version info, like `8.8`; `%2$s` will be filled with distribution type, like `all` `bin`.
2330

2431
4. restart terminal or JetBrains IDE
2532

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = "org.tingy"
11-
version = "1.1"
11+
version = "1.2"
1212

1313
dependencies {
1414
implementation("net.bytebuddy:byte-buddy:1.15.1")

settings.gradle.kts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import org.gradle.api.internal.FeaturePreviews
2+
13
dependencyResolutionManagement {
24
repositories {
5+
gradlePluginPortal()
36
mavenCentral()
7+
// not used. gradle project maven repository
8+
maven("https://repo.gradle.org/artifactory/libs-releases/")
49
}
510
}
611

@@ -20,4 +25,4 @@ pluginManagement {
2025
}
2126

2227
rootProject.name = "GradleAgent"
23-
28+
enableFeaturePreview(FeaturePreviews.Feature.TYPESAFE_PROJECT_ACCESSORS.name)

src/main/kotlin/Main.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ fun main() {
1010
}
1111

1212
fun premain(args: String?, instrumentation: Instrumentation) {
13-
if (args == null) {
14-
println("> Gradle Agent:\nMirror url template is null\n")
13+
val mirrorUrlTemplate = System.getenv("GRADLE_DISTRIBUTION_URL_TEMPLATE")
14+
if (mirrorUrlTemplate == null) {
15+
println(
16+
"> Gradle Agent:\nThe environment variable GRADLE_DISTRIBUTION_URL_TEMPLATE is not specified. " +
17+
"Please set it to a valid URL template. " +
18+
"For example: https://mirror.host.com/gradle/gradle-%1\$s-%2\$s.zip. \n"
19+
)
1520
return
1621
}
1722

1823
AgentBuilder.Default().type(ElementMatchers.named(TARGET_CLASS_NAME)).transform { builder, _, _, _, _ ->
1924
builder.method(ElementMatchers.named("readDistroUrl"))
20-
.intercept(MethodDelegation.to(WrapperExecutorInterceptor(args)))
25+
.intercept(MethodDelegation.to(WrapperExecutorInterceptor(mirrorUrlTemplate)))
2126
}.installOn(instrumentation)
2227
}

0 commit comments

Comments
 (0)