Skip to content

Commit

Permalink
Allow better specification of outputFile
Browse files Browse the repository at this point in the history
This should allow directly using from(tasks.named("mySbomTask"))
in copy tasks or maven publications

Also allow users to specify the output file directly instead
of redirecting with a copy task or something

Signed-off-by: Appu Goundan <[email protected]>
  • Loading branch information
loosebazooka committed Feb 28, 2024
1 parent aa8f980 commit 06875c3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/spdx/sbom/gradle/SpdxSbomExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Nested;
Expand All @@ -30,6 +31,8 @@ abstract class Target {

public abstract ListProperty<String> getConfigurations();

public abstract RegularFileProperty getOutputFile();

@Nested
public abstract Scm getScm();

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/spdx/sbom/gradle/SpdxSbomPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,17 @@ private void createTaskForTarget(
SpdxSbomTask.class,
t -> {
t.setGroup("Spdx sbom tasks");
t.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir("spdx"));
if (target.getOutputFile().isPresent()) {
t.getOutputFile().set(target.getOutputFile());
} else {
t.getOutputFile()
.set(
project
.getLayout()
.getBuildDirectory()
.file("spdx/" + target.getName() + ".spdx.json"));
}
t.getProjectPath().set(project.getPath());
t.getFilename().set(target.getName() + ".spdx.json");
t.getDocumentInfo().set(DocumentInfo.from(target));
t.getScmInfo().set(ScmInfo.from(target));
t.getProjectInfoService().set(projectInfoService);
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/org/spdx/sbom/gradle/SpdxSbomTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier;
import org.gradle.api.artifacts.result.ResolvedComponentResult;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.spdx.jacksonstore.MultiFormatStore;
Expand All @@ -55,13 +54,8 @@ public abstract class SpdxSbomTask extends DefaultTask {
@Input
abstract MapProperty<ComponentArtifactIdentifier, File> getResolvedArtifacts();

@OutputDirectory
public abstract DirectoryProperty getOutputDirectory();

@OutputFile
public File getOutputFile() {
return getOutputDirectory().file(getFilename()).get().getAsFile();
}
public abstract RegularFileProperty getOutputFile();

@Internal
abstract Property<ProjectInfoService> getProjectInfoService();
Expand All @@ -72,9 +66,6 @@ public File getOutputFile() {
@Input
abstract MapProperty<String, PomInfo> getPoms();

@Input
abstract Property<String> getFilename();

@Input
abstract Property<DocumentInfo> getDocumentInfo();

Expand Down Expand Up @@ -115,7 +106,7 @@ public void generateSbom() throws Exception {
List<String> verificationErrors = doc.verify();
verificationErrors.forEach(errors -> getLogger().warn(errors));

FileOutputStream out = new FileOutputStream(getOutputFile());
FileOutputStream out = new FileOutputStream(getOutputFile().get().getAsFile());
modelStore.serialize(doc.getDocumentUri(), out);
}
}

0 comments on commit 06875c3

Please sign in to comment.