Skip to content

Commit

Permalink
Merge pull request quarkusio#44870 from aloubyansky/record-main-compo…
Browse files Browse the repository at this point in the history
…nent-deps

Make sure the main component has its direct dependencies recorded
  • Loading branch information
aloubyansky authored Dec 2, 2024
2 parents 85201cd + 065d8cf commit e1f2a57
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,14 @@ private JarBuildItem buildUberJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
.build();
}
final ApplicationManifestConfig manifestConfig = ApplicationManifestConfig.builder()
.setApplicationModel(curateOutcomeBuildItem.getApplicationModel())
.setMainComponent(ApplicationComponent.builder()
.setPath(runnerJar)
.setResolvedDependency(appArtifact)
.build())
.setRunnerPath(runnerJar)
.addComponents(curateOutcomeBuildItem.getApplicationModel().getDependencies())
.build();

return new JarBuildItem(runnerJar, originalJar, null, UBER_JAR,
suffixToClassifier(packageConfig.computedRunnerSuffix()), manifestConfig);
}
Expand Down Expand Up @@ -696,6 +697,9 @@ private JarBuildItem buildThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
fastJarJarsBuilder.setRunner(runnerJar);

if (!rebuild) {
manifestConfig.addComponent(ApplicationComponent.builder()
.setResolvedDependency(applicationArchivesBuildItem.getRootArchive().getResolvedDependency())
.setPath(runnerJar));
Predicate<String> ignoredEntriesPredicate = getThinJarIgnoredEntriesPredicate(packageConfig);
try (FileSystem runnerZipFs = createNewZip(runnerJar, packageConfig)) {
copyFiles(applicationArchivesBuildItem.getRootArchive(), runnerZipFs, null, ignoredEntriesPredicate);
Expand Down Expand Up @@ -766,7 +770,9 @@ private JarBuildItem buildThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,

runnerJar.toFile().setReadable(true, false);
Path initJar = buildDir.resolve(QUARKUS_RUN_JAR);
manifestConfig.setMainComponent(ApplicationComponent.builder().setPath(initJar))
manifestConfig.setMainComponent(ApplicationComponent.builder()
.setPath(initJar)
.setDependencies(List.of(curateOutcomeBuildItem.getApplicationModel().getAppArtifact())))
.setRunnerPath(initJar);
boolean mutableJar = packageConfig.jar().type() == MUTABLE_JAR;
if (mutableJar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ ArtifactResultBuildItem result(NativeImageBuildItem image,
graalVMVersion.toMap(),
ApplicationManifestConfig.builder()
.setApplicationModel(curateOutcomeBuildItem.getApplicationModel())
.setMainComponent(ApplicationComponent.builder().setPath(image.getPath()))
.setMainComponent(ApplicationComponent.builder()
.setPath(image.getPath())
.setDependencies(List.of(curateOutcomeBuildItem.getApplicationModel().getAppArtifact())))
.setRunnerPath(image.getPath())
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import io.quarkus.cyclonedx.generator.*;
import io.quarkus.cyclonedx.generator.CycloneDxSbomGenerator;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.AppModelProviderBuildItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,16 @@ public List<SbomResult> generate() {
private void addComponent(Bom bom, ApplicationComponent component) {
final org.cyclonedx.model.Component c = getComponent(component);
bom.addComponent(c);
if (component.getResolvedDependency() != null) {
var deps = component.getResolvedDependency().getDependencies();
if (!deps.isEmpty()) {
final Dependency d = new Dependency(c.getBomRef());
for (var depCoords : sortAlphabetically(deps)) {
d.addDependency(new Dependency(getPackageURL(depCoords).toString()));
}
bom.addDependency(d);
recordDependencies(bom, component, c);
}

private static void recordDependencies(Bom bom, ApplicationComponent component, Component c) {
if (!component.getDependencies().isEmpty()) {
final Dependency d = new Dependency(c.getBomRef());
for (var depCoords : sortAlphabetically(component.getDependencies())) {
d.addDependency(new Dependency(getPackageURL(depCoords).toString()));
}
bom.addDependency(d);
}
}

Expand All @@ -191,6 +192,7 @@ private void addApplicationComponent(Bom bom, ApplicationComponent component) {
c.setType(org.cyclonedx.model.Component.Type.APPLICATION);
bom.getMetadata().setComponent(c);
bom.addComponent(c);
recordDependencies(bom, component, c);
}

private org.cyclonedx.model.Component getComponent(ApplicationComponent component) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.quarkus.sbom;

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;

import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ResolvedDependency;

public class ApplicationComponent {
Expand Down Expand Up @@ -32,6 +35,9 @@ public Builder setDistributionPath(String distributionPath) {

public Builder setResolvedDependency(ResolvedDependency dep) {
this.dep = dep;
if (dependencies.isEmpty()) {
dependencies = dep.getDependencies();
}
return this;
}

Expand All @@ -49,6 +55,11 @@ public Builder setScope(String scope) {
return this;
}

public Builder setDependencies(Collection<ArtifactCoords> dependencies) {
this.dependencies = dependencies;
return this;
}

public ApplicationComponent build() {
return ensureImmutable();
}
Expand All @@ -64,6 +75,7 @@ protected ApplicationComponent ensureImmutable() {
protected ResolvedDependency dep;
protected String pedigree;
protected String scope;
protected Collection<ArtifactCoords> dependencies = List.of();

private ApplicationComponent() {
}
Expand All @@ -74,6 +86,7 @@ private ApplicationComponent(ApplicationComponent builder) {
this.dep = builder.dep;
this.pedigree = builder.pedigree;
this.scope = builder.scope;
this.dependencies = List.copyOf(builder.dependencies);
}

public Path getPath() {
Expand All @@ -96,6 +109,10 @@ public String getScope() {
return scope;
}

public Collection<ArtifactCoords> getDependencies() {
return dependencies;
}

protected ApplicationComponent ensureImmutable() {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.ResolvedDependency;

public class ApplicationManifestConfig {

Expand Down Expand Up @@ -39,20 +40,28 @@ private Builder() {
}

public Builder setApplicationModel(ApplicationModel model) {
setMainComponent(ApplicationComponent.builder().setResolvedDependency(model.getAppArtifact()).build());
for (var d : model.getDependencies()) {
final ApplicationComponent.Builder comp = ApplicationComponent.builder().setResolvedDependency(d);
if (!d.getResolvedPaths().isEmpty()) {
comp.setPath(d.getResolvedPaths().iterator().next());
}
addComponent(comp.build());
setMainComponent(toAppComponent(model.getAppArtifact()));
return addComponents(model.getDependencies());
}

public Builder addComponents(Collection<ResolvedDependency> dependencies) {
for (var d : dependencies) {
addComponent(toAppComponent(d));
}
return this;
}

private static ApplicationComponent toAppComponent(ResolvedDependency d) {
final ApplicationComponent.Builder comp = ApplicationComponent.builder().setResolvedDependency(d);
if (!d.getResolvedPaths().isEmpty()) {
comp.setPath(d.getResolvedPaths().iterator().next());
}
return comp.build();
}

public Builder setMainComponent(ApplicationComponent applicationRunner) {
ensureNotBuilt();
main = applicationRunner == null ? null : new ComponentHolder(applicationRunner);
main = applicationRunner == null ? null : addComponentHolder(applicationRunner);
return this;
}

Expand All @@ -69,6 +78,11 @@ public Builder setRunnerPath(Path runnerPath) {
}

public Builder addComponent(ApplicationComponent component) {
addComponentHolder(component);
return this;
}

private ComponentHolder addComponentHolder(ApplicationComponent component) {
ComponentHolder holder = null;
if (component.getPath() != null) {
holder = compPaths.get(component.getPath());
Expand Down Expand Up @@ -108,7 +122,7 @@ public Builder addComponent(ApplicationComponent component) {
holder.component.scope = component.scope;
}
}
return this;
return holder;
}

public ApplicationManifestConfig build() {
Expand All @@ -118,10 +132,14 @@ public ApplicationManifestConfig build() {
if (!compList.isEmpty()) {
ApplicationManifestConfig.this.components = new ArrayList<>(compList.size());
for (var holder : compList) {
if (holder == main) {
continue;
}
if (holder.path != null && !holder.path.equals(holder.component.getPath())) {
holder.component = ApplicationComponent.builder()
.setPath(holder.path)
.setResolvedDependency(holder.component.getResolvedDependency())
.setDependencies(holder.component.getDependencies())
.setPedigree(holder.component.getPedigree());
}
if (distDir != null) {
Expand Down

0 comments on commit e1f2a57

Please sign in to comment.