Skip to content

Commit

Permalink
Add a task that reports the artifact identifiers of the project's run…
Browse files Browse the repository at this point in the history
…time classpath.
  • Loading branch information
adammurdoch committed Dec 19, 2021
1 parent b079ec3 commit b9aa310
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ repositories {
dependencies {
implementation(project(":util"))
implementation("test.libs:lib1:1.0")
implementation("org.apache.commons:commons-collections4:4.4")
}
3 changes: 3 additions & 0 deletions app/src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import org.apache.commons.collections4.map.LinkedMap;

public class App {
public static void main(String[] args) {
new Util();
new Lib1();
new LinkedMap<String, Long>();
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.gradle.unsafe.configuration-cache=true
org.gradle.unsafe.configuration-cache=false
31 changes: 31 additions & 0 deletions plugins/src/main/java/ReportArtifactMetadataTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public abstract class ReportArtifactMetadataTask extends DefaultTask {
@Input
public abstract ListProperty<ComponentArtifactIdentifier> getArtifacts();

@OutputFile
public abstract RegularFileProperty getOutputFile();

@TaskAction
public void report() throws IOException {
File outputFile = getOutputFile().getAsFile().get();
try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
for (ComponentArtifactIdentifier id : getArtifacts().get()) {
writer.println(id.getDisplayName());
}
}
System.out.println("Wrote report to " + outputFile);
}
}
12 changes: 12 additions & 0 deletions plugins/src/main/java/TestPlugin.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.result.ArtifactResult;
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
import org.gradle.api.provider.Provider;

import java.util.Set;
import java.util.stream.Collectors;

public class TestPlugin implements Plugin<Project> {
@Override
public void apply(Project target) {
target.getPlugins().apply("java-library");

target.getTasks().register("artifacts-report", ReportArtifactMetadataTask.class, t -> {
Provider<Set<ResolvedArtifactResult>> artifacts = target.getConfigurations().getByName("runtimeClasspath").getIncoming().getArtifacts().getResolvedArtifacts();
t.getArtifacts().set(artifacts.map(s -> s.stream().map(ArtifactResult::getId).collect(Collectors.toList())));
t.getOutputFile().set(target.getLayout().getBuildDirectory().file("artifacts.txt"));
});
}
}

0 comments on commit b9aa310

Please sign in to comment.