Skip to content

Commit

Permalink
The new javaAgent configuration is no longer non-transitive
Browse files Browse the repository at this point in the history
This works around a GCV bug which fails to apply constraints to
non-transitive configurations.
See palantir/gradle-consistent-versions#677
  • Loading branch information
carterkozak committed Feb 25, 2021
1 parent c2d577f commit e198eac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,7 @@ public void apply(Project project) {
});

Configuration runtimeClasspath = project.getConfigurations().getByName("runtimeClasspath");
Configuration javaAgentConfiguration = project.getConfigurations()
.create("javaAgent", new Action<Configuration>() {
@Override
public void execute(Configuration javaAgent) {
// Each javaAgent is applied via a '-javaagent:path/to/agent.jar' argument
// Agents should have no dependencies, but this allows us to avoid adding
// non-agent jars as agents if any are listed.
javaAgent.setTransitive(false);
}
});
Configuration javaAgentConfiguration = project.getConfigurations().create("javaAgent");

// Set default configuration to look for product dependencies to be runtimeClasspath
distributionExtension.setProductDependenciesConfig(runtimeClasspath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import org.gradle.api.DefaultTask;
import org.gradle.api.JavaVersion;
Expand Down Expand Up @@ -204,10 +206,25 @@ private static void writeConfig(LaunchConfig config, File scriptFile) throws IOE

private List<String> javaAgentArgs() {
return getJavaAgents().getFiles().stream()
.map(file -> "-javaagent:service/lib/agent/" + file.getName())
.map(file -> "-javaagent:service/lib/agent/"
+ validateJavaAgent(file).getName())
.collect(Collectors.toList());
}

/** Returns the input file. An exception is thrown if the {@code agentFile} is not a java agent. */
private static File validateJavaAgent(File agentFile) {
try {
JarFile agentJarFile = new JarFile(agentFile);
if (!agentJarFile.getManifest().getMainAttributes().containsKey(new Attributes.Name("Premain-Class"))) {
throw new IllegalArgumentException("Jar file " + agentFile.getName()
+ " is not a java agent and contains no Premain-Class manifest entry");
}
return agentFile;
} catch (IOException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}

private static List<String> relativizeToServiceLibDirectory(FileCollection files) {
return files.getFiles().stream()
.map(file -> "service/lib/" + file.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,26 @@ class JavaServiceDistributionPluginTests extends GradleIntegrationSpec {
fileExists('dist/service-name-0.0.1/service/lib/agent/byte-buddy-agent-1.10.21.jar')
}

def 'fails at build time when non-agent jars are provided as agents'() {
createUntarBuildFile(buildFile)
buildFile << """
dependencies {
compile files("${EXTERNAL_JAR}")
javaAgent files("${EXTERNAL_JAR}")
}
tasks.jar.archiveBaseName = "internal"
distribution {
javaVersion 11
}""".stripIndent()
file('src/main/java/test/Test.java') << "package test;\npublic class Test {}"

when:
def result = runTasksAndFail(':distTar')

then:
result.output.contains('is not a java agent and contains no Premain-Class manifest entry')
}

private static createUntarBuildFile(File buildFile) {
buildFile << '''
plugins {
Expand Down

0 comments on commit e198eac

Please sign in to comment.