io.github.pmckeown.dependencytrack.upload.UploadBomMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dependency-track-maven-plugin Show documentation
Show all versions of dependency-track-maven-plugin Show documentation
Maven plugin to integrate with a Dependency Track server to submit dependency manifests and gather project metrics.
package io.github.pmckeown.dependencytrack.upload;
import io.github.pmckeown.dependencytrack.AbstractDependencyTrackMojo;
import io.github.pmckeown.dependencytrack.CommonConfig;
import io.github.pmckeown.dependencytrack.DependencyTrackException;
import io.github.pmckeown.dependencytrack.metrics.MetricsAction;
import io.github.pmckeown.dependencytrack.project.Project;
import io.github.pmckeown.dependencytrack.project.ProjectAction;
import io.github.pmckeown.util.Logger;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import javax.inject.Inject;
/**
* Provides the capability to upload a Bill of Material (BOM) to your Dependency Track server.
*
* The BOM may any format supported by your Dependency Track server, has only been tested with the output from the
* cyclonedx-maven-plugin in the
* CycloneDX format
*
* Specific configuration options are:
*
* - bomLocation
*
*
* @author Paul McKeown
*/
@Mojo(name = "upload-bom", defaultPhase = LifecyclePhase.VERIFY)
public class UploadBomMojo extends AbstractDependencyTrackMojo {
@Parameter(property = "dependency-track.bomLocation")
private String bomLocation;
@Parameter(property = "project", readonly = true, required = true)
private MavenProject mavenProject;
private UploadBomAction uploadBomAction;
private MetricsAction metricsAction;
private ProjectAction projectAction;
@Inject
public UploadBomMojo(UploadBomAction uploadBomAction, MetricsAction metricsAction, ProjectAction projectAction,
CommonConfig commonConfig, Logger logger) {
super(commonConfig, logger);
this.uploadBomAction = uploadBomAction;
this.metricsAction = metricsAction;
this.projectAction = projectAction;
}
@Override
public void performAction() throws MojoExecutionException, MojoFailureException {
try {
if (!uploadBomAction.upload(getBomLocation())) {
handleFailure("Bom upload failed");
}
Project project = projectAction.getProject(projectName, projectVersion);
metricsAction.refreshMetrics(project);
} catch (DependencyTrackException ex) {
handleFailure("Error occurred during upload", ex);
}
}
private String getBomLocation() {
if (StringUtils.isNotBlank(bomLocation)) {
return bomLocation;
} else {
String defaultLocation = mavenProject.getBasedir() + "/target/bom.xml";
logger.debug("bomLocation not supplied so using: %s", defaultLocation);
return defaultLocation;
}
}
/*
* Setters for dependency injection in tests
*/
void setBomLocation(String bomLocation) {
this.bomLocation = bomLocation;
}
void setMavenProject(MavenProject mp) {
this.mavenProject = mp;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy