-
-
Notifications
You must be signed in to change notification settings - Fork 8
Add ability to pass options to javaagents (#50) #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package com.ryandens.javaagent; | ||
|
|
||
| import java.io.File; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.gradle.api.artifacts.Configuration; | ||
| import org.gradle.api.artifacts.component.ComponentIdentifier; | ||
| import org.gradle.api.artifacts.component.ModuleComponentIdentifier; | ||
| import org.gradle.api.artifacts.component.ProjectComponentIdentifier; | ||
| import org.gradle.api.artifacts.result.ResolvedArtifactResult; | ||
| import org.gradle.api.provider.Provider; | ||
|
|
||
| /** | ||
| * Resolves the {@link JavaagentExtension#getAgentOptions() agent options} declared by coordinate | ||
| * into a map keyed by the resolved artifact file name, which is the join key every {@code | ||
| * -javaagent:} argument site can compute (the run/test sites hold the {@link File}, the | ||
| * distribution and jib sites use {@code file.getName()}). | ||
| * | ||
| * <p>This class is written in Java for the same reason as {@link JavaForkOptionsConfigurer}: to | ||
| * avoid the <a | ||
| * href="https://docs.gradle.org/7.5.1/userguide/validation_problems.html#implementation_unknown">Gradle | ||
| * task input serialization limitation</a> with Kotlin lambdas. | ||
| */ | ||
| public final class AgentOptionsResolver { | ||
|
|
||
| /** Prevent instantiation for utility class. */ | ||
| private AgentOptionsResolver() {} | ||
|
|
||
| /** | ||
| * Builds a {@code fileName -> options} map from the resolved artifacts of the provided | ||
| * configuration and the coordinate-keyed options declared on the {@link JavaagentExtension}. Only | ||
| * agents that have options declared appear in the resulting map. | ||
| * | ||
| * <p>The returned {@link Provider} is lazy and configuration-cache safe: it reads the | ||
| * configuration's resolved artifacts and the options map without touching {@code Project} at | ||
| * execution time. | ||
| * | ||
| * @param configuration the resolvable javaagent configuration whose artifacts should be matched | ||
| * @param coordinateOptions options keyed by dependency coordinate (see {@link | ||
| * JavaagentExtension}) | ||
| * @return provider of a map from resolved artifact file name to its option string | ||
| */ | ||
| public static Provider<Map<String, String>> optionsByFileName( | ||
| final Configuration configuration, final Provider<Map<String, String>> coordinateOptions) { | ||
| return configuration | ||
| .getIncoming() | ||
| .getArtifacts() | ||
| .getResolvedArtifacts() | ||
| .map( | ||
| artifacts -> { | ||
| final Map<String, String> coordinates = coordinateOptions.getOrElse(new HashMap<>()); | ||
| final Map<String, String> byFileName = new HashMap<>(); | ||
| if (coordinates.isEmpty()) { | ||
| return byFileName; | ||
| } | ||
| for (final ResolvedArtifactResult artifact : artifacts) { | ||
| final String key = coordinateKey(artifact.getId().getComponentIdentifier()); | ||
| final String options = coordinates.get(key); | ||
| if (options != null) { | ||
| byFileName.put(artifact.getFile().getName(), options); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium
Also found in 1 other location(s)
🚀 Reply "fix it for me" or copy this AI Prompt for your agent: |
||
| } | ||
| } | ||
| return byFileName; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Derives the coordinate key used to look up options for a resolved artifact. Module dependencies | ||
| * are keyed by {@code group:name} (version intentionally excluded so options survive version | ||
| * bumps), project dependencies by their project path, and anything else by its display name as a | ||
| * best-effort fallback. | ||
| */ | ||
| private static String coordinateKey(final ComponentIdentifier identifier) { | ||
| if (identifier instanceof ModuleComponentIdentifier) { | ||
| final ModuleComponentIdentifier module = (ModuleComponentIdentifier) identifier; | ||
| return module.getGroup() + ":" + module.getModule(); | ||
| } | ||
| if (identifier instanceof ProjectComponentIdentifier) { | ||
| return ((ProjectComponentIdentifier) identifier).getProjectPath(); | ||
| } | ||
| return identifier.getDisplayName(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
jib/JavaagentJibExtension.kt:49extendContainerBuildPlan()throwsIllegalStateExceptionwheneveragentOptionsis unset. For Jib integrations liketel.schich.tinyjibwhereagentOptionsis never configured, this breaks previously working builds even when no agent options are needed. The call toextraConfig.get().agentOptions.get()on line 49 unconditionally finalizes theMapProperty, but no default is provided, so any consumer that doesn't explicitly setagentOptionsfails. Consider calling.getOrElse(emptyMap())instead of.get()so unset options default to an empty map.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: