Home / Class/ BuildInfoMojo Class — spring-boot Architecture

BuildInfoMojo Class — spring-boot Architecture

Architecture documentation for the BuildInfoMojo class in BuildInfoMojo.java from the spring-boot codebase.

Entity Profile

Source Code

build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildInfoMojo.java lines 51–169

@Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class BuildInfoMojo extends AbstractMojo {

	private final BuildContext buildContext;

	/**
	 * The Maven session.
	 */
	@Parameter(defaultValue = "${session}", readonly = true, required = true)
	@SuppressWarnings("NullAway.Init")
	private MavenSession session;

	/**
	 * The Maven project.
	 */
	@Parameter(defaultValue = "${project}", readonly = true, required = true)
	@SuppressWarnings("NullAway.Init")
	private MavenProject project;

	/**
	 * The location of the generated {@code build-info.properties} file.
	 */
	@Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
	@SuppressWarnings("NullAway.Init")
	private File outputFile;

	/**
	 * The value used for the {@code build.time} property in a form suitable for
	 * {@link Instant#parse(CharSequence)}. Defaults to
	 * {@code project.build.outputTimestamp} or {@code session.request.startTime} if the
	 * former is not set. To disable the {@code build.time} property entirely, use
	 * {@code 'off'} or add it to {@code excludeInfoProperties}.
	 * @since 2.2.0
	 */
	@Parameter(defaultValue = "${project.build.outputTimestamp}")
	private @Nullable String time;

	/**
	 * Additional properties to store in the {@code build-info.properties} file. Each
	 * entry is prefixed by {@code build.} in the generated {@code build-info.properties}.
	 */
	@Parameter
	private @Nullable Map<String, String> additionalProperties;

	/**
	 * Properties that should be excluded {@code build-info.properties} file. Can be used
	 * to exclude the standard {@code group}, {@code artifact}, {@code name},
	 * {@code version} or {@code time} properties as well as items from
	 * {@code additionalProperties}.
	 */
	@Parameter
	private @Nullable List<String> excludeInfoProperties;

	/**
	 * Skip the execution.
	 * @since 3.1.0
	 */
	@Parameter(property = "spring-boot.build-info.skip", defaultValue = "false")
	private boolean skip;

	@Inject
	public BuildInfoMojo(BuildContext buildContext) {
		this.buildContext = buildContext;
	}

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		if (this.skip) {
			getLog().debug("skipping build-info as per configuration.");
			return;
		}
		try {
			ProjectDetails details = getProjectDetails();
			new BuildPropertiesWriter(this.outputFile).writeBuildProperties(details);
			this.buildContext.refresh(this.outputFile);
		}
		catch (NullAdditionalPropertyValueException ex) {
			throw new MojoFailureException("Failed to generate build-info.properties. " + ex.getMessage(), ex);
		}
		catch (Exception ex) {
			throw new MojoExecutionException(ex.getMessage(), ex);
		}
	}

	private ProjectDetails getProjectDetails() {
		String group = getIfNotExcluded("group", this.project.getGroupId());
		String artifact = getIfNotExcluded("artifact", this.project.getArtifactId());
		String version = getIfNotExcluded("version", this.project.getVersion());
		String name = getIfNotExcluded("name", this.project.getName());
		Instant time = getIfNotExcluded("time", getBuildTime());
		Map<String, String> additionalProperties = applyExclusions(this.additionalProperties);
		return new ProjectDetails(group, artifact, version, name, time, additionalProperties);
	}

	private <T> @Nullable T getIfNotExcluded(String name, @Nullable T value) {
		return (this.excludeInfoProperties == null || !this.excludeInfoProperties.contains(name)) ? value : null;
	}

	private @Nullable Map<String, String> applyExclusions(@Nullable Map<String, String> source) {
		if (source == null || this.excludeInfoProperties == null) {
			return source;
		}
		Map<String, String> result = new LinkedHashMap<>(source);
		this.excludeInfoProperties.forEach(result::remove);
		return result;
	}

	private @Nullable Instant getBuildTime() {
		if (this.time == null || this.time.isEmpty()) {
			Date startTime = this.session.getRequest().getStartTime();
			return (startTime != null) ? startTime.toInstant() : Instant.now();
		}
		if ("off".equalsIgnoreCase(this.time)) {
			return null;
		}
		return new MavenBuildOutputTimestamp(this.time).toInstant();
	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free