Home / Class/ ArtifactsLibraries Class — spring-boot Architecture

ArtifactsLibraries Class — spring-boot Architecture

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

Entity Profile

Source Code

build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ArtifactsLibraries.java lines 49–210

public class ArtifactsLibraries implements Libraries {

	private static final Map<String, LibraryScope> SCOPES;

	static {
		Map<String, LibraryScope> libraryScopes = new HashMap<>();
		libraryScopes.put(Artifact.SCOPE_COMPILE, LibraryScope.COMPILE);
		libraryScopes.put(Artifact.SCOPE_RUNTIME, LibraryScope.RUNTIME);
		libraryScopes.put(Artifact.SCOPE_PROVIDED, LibraryScope.PROVIDED);
		libraryScopes.put(Artifact.SCOPE_SYSTEM, LibraryScope.PROVIDED);
		SCOPES = Collections.unmodifiableMap(libraryScopes);
	}

	private final Set<Artifact> artifacts;

	private final Set<Artifact> includedArtifacts;

	private final Collection<MavenProject> localProjects;

	private final @Nullable Collection<Dependency> unpacks;

	private final Log log;

	/**
	 * Creates a new {@code ArtifactsLibraries} from the given {@code artifacts}.
	 * @param artifacts the artifacts to represent as libraries
	 * @param localProjects projects for which {@link Library#isLocal() local} libraries
	 * should be created
	 * @param unpacks artifacts that should be unpacked on launch
	 * @param log the log
	 * @since 2.4.0
	 */
	public ArtifactsLibraries(Set<Artifact> artifacts, Collection<MavenProject> localProjects,
			@Nullable Collection<Dependency> unpacks, Log log) {
		this(artifacts, artifacts, localProjects, unpacks, log);
	}

	/**
	 * Creates a new {@code ArtifactsLibraries} from the given {@code artifacts}.
	 * @param artifacts all artifacts that can be represented as libraries
	 * @param includedArtifacts the actual artifacts to include in the uber jar
	 * @param localProjects projects for which {@link Library#isLocal() local} libraries
	 * should be created
	 * @param unpacks artifacts that should be unpacked on launch
	 * @param log the log
	 * @since 2.4.8
	 */
	public ArtifactsLibraries(Set<Artifact> artifacts, Set<Artifact> includedArtifacts,
			Collection<MavenProject> localProjects, @Nullable Collection<Dependency> unpacks, Log log) {
		this.artifacts = artifacts;
		this.includedArtifacts = includedArtifacts;
		this.localProjects = localProjects;
		this.unpacks = unpacks;
		this.log = log;
	}

	@Override
	public void doWithLibraries(LibraryCallback callback) throws IOException {
		Set<String> duplicates = getDuplicates(this.artifacts);
		for (Artifact artifact : this.artifacts) {
			String name = getFileName(artifact);
			File file = artifact.getFile();
			LibraryScope scope = SCOPES.get(artifact.getScope());
			if (scope == null || file == null) {
				continue;
			}
			if (duplicates.contains(name)) {
				this.log.debug("Duplicate found: " + name);
				name = artifact.getGroupId() + "-" + name;
				this.log.debug("Renamed to: " + name);
			}
			LibraryCoordinates coordinates = new ArtifactLibraryCoordinates(artifact);
			boolean unpackRequired = isUnpackRequired(artifact);
			boolean local = isLocal(artifact);
			boolean included = this.includedArtifacts.contains(artifact);
			callback.library(new Library(name, file, scope, coordinates, unpackRequired, local, included));
		}
	}

	private Set<String> getDuplicates(Set<Artifact> artifacts) {
		Set<String> duplicates = new HashSet<>();
		Set<String> seen = new HashSet<>();
		for (Artifact artifact : artifacts) {
			String fileName = getFileName(artifact);
			if (artifact.getFile() != null && !seen.add(fileName)) {
				duplicates.add(fileName);
			}
		}
		return duplicates;
	}

	private boolean isUnpackRequired(Artifact artifact) {
		if (this.unpacks != null) {
			for (Dependency unpack : this.unpacks) {
				if (artifact.getGroupId().equals(unpack.getGroupId())
						&& artifact.getArtifactId().equals(unpack.getArtifactId())) {
					return true;
				}
			}
		}
		return false;
	}

	private boolean isLocal(Artifact artifact) {
		for (MavenProject localProject : this.localProjects) {
			if (localProject.getArtifact().equals(artifact)) {
				return true;
			}
			for (Artifact attachedArtifact : localProject.getAttachedArtifacts()) {
				if (attachedArtifact.equals(artifact)) {
					return true;
				}
			}
		}
		return false;
	}

	private String getFileName(Artifact artifact) {
		StringBuilder sb = new StringBuilder();
		sb.append(artifact.getArtifactId()).append("-").append(artifact.getBaseVersion());
		String classifier = artifact.getClassifier();
		if (classifier != null) {
			sb.append("-").append(classifier);
		}
		sb.append(".").append(artifact.getArtifactHandler().getExtension());
		return sb.toString();
	}

	/**
	 * {@link LibraryCoordinates} backed by a Maven {@link Artifact}.
	 */
	private static class ArtifactLibraryCoordinates implements LibraryCoordinates {

		private final Artifact artifact;

		ArtifactLibraryCoordinates(Artifact artifact) {
			this.artifact = artifact;
		}

		@Override
		public String getGroupId() {
			return this.artifact.getGroupId();
		}

		@Override
		public String getArtifactId() {
			return this.artifact.getArtifactId();
		}

		@Override
		public String getVersion() {
			return this.artifact.getBaseVersion();
		}

		@Override
		public String toString() {
			return this.artifact.toString();
		}

	}

}

Analyze Your Own Codebase

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

Try Supermodel Free