Skip to content

Commit

Permalink
Set implementation name and version in Gradle-built archives
Browse files Browse the repository at this point in the history
Closes gh-34059
  • Loading branch information
wilkinsona committed Feb 8, 2023
1 parent d65f1cd commit 9378fc4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,6 +54,8 @@ class BootArchiveSupport {

private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 };

private static final String UNSPECIFIED_VERSION = "unspecified";

private static final Set<String> DEFAULT_LAUNCHER_CLASSES;

static {
Expand Down Expand Up @@ -85,7 +87,7 @@ class BootArchiveSupport {
}

void configureManifest(Manifest manifest, String mainClass, String classes, String lib, String classPathIndex,
String layersIndex, String jdkVersion) {
String layersIndex, String jdkVersion, String implementationName, Object implementationVersion) {
Attributes attributes = manifest.getAttributes();
attributes.putIfAbsent("Main-Class", this.loaderMainClass);
attributes.putIfAbsent("Start-Class", mainClass);
Expand All @@ -99,6 +101,13 @@ void configureManifest(Manifest manifest, String mainClass, String classes, Stri
attributes.putIfAbsent("Spring-Boot-Layers-Index", layersIndex);
}
attributes.putIfAbsent("Build-Jdk-Spec", jdkVersion);
attributes.putIfAbsent("Implementation-Name", implementationName);
if (implementationVersion != null) {
String versionString = implementationVersion.toString();
if (!UNSPECIFIED_VERSION.equals(versionString)) {
attributes.putIfAbsent("Implementation-Version", versionString);
}
}
}

private String determineSpringBootVersion() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.gradle.api.file.FileCopyDetails;
import org.gradle.api.file.FileTreeElement;
import org.gradle.api.internal.file.copy.CopyAction;
import org.gradle.api.provider.Provider;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
Expand Down Expand Up @@ -65,6 +66,10 @@ public abstract class BootJar extends Jar implements BootArchive {

private final LayeredSpec layered;

private final Provider<String> projectName;

private final Provider<Object> projectVersion;

private FileCollection classpath;

/**
Expand All @@ -85,6 +90,8 @@ public BootJar() {
}
});
});
this.projectName = project.provider(project::getName);
this.projectVersion = project.provider(project::getVersion);
}

private void configureBootInfSpec(CopySpec bootInfSpec) {
Expand Down Expand Up @@ -120,7 +127,7 @@ private void moveMetaInfToRoot(CopySpec spec) {
public void copy() {
this.support.configureManifest(getManifest(), getMainClass().get(), CLASSES_DIRECTORY, LIB_DIRECTORY,
CLASSPATH_INDEX, (isLayeredDisabled()) ? null : LAYERS_INDEX,
this.getTargetJavaVersion().get().getMajorVersion());
this.getTargetJavaVersion().get().getMajorVersion(), this.projectName.get(), this.projectVersion.get());
super.copy();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
import org.gradle.api.file.FileCopyDetails;
import org.gradle.api.file.FileTreeElement;
import org.gradle.api.internal.file.copy.CopyAction;
import org.gradle.api.provider.Provider;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Internal;
Expand Down Expand Up @@ -65,6 +66,10 @@ public abstract class BootWar extends War implements BootArchive {

private final LayeredSpec layered;

private final Provider<String> projectName;

private final Provider<Object> projectVersion;

private FileCollection providedClasspath;

/**
Expand All @@ -85,6 +90,8 @@ public BootWar() {
}
});
});
this.projectName = project.provider(project::getName);
this.projectVersion = project.provider(project::getVersion);
}

private Object getProvidedLibFiles() {
Expand All @@ -95,7 +102,7 @@ private Object getProvidedLibFiles() {
public void copy() {
this.support.configureManifest(getManifest(), getMainClass().get(), CLASSES_DIRECTORY, LIB_DIRECTORY,
CLASSPATH_INDEX, (isLayeredDisabled()) ? null : LAYERS_INDEX,
this.getTargetJavaVersion().get().getMajorVersion());
this.getTargetJavaVersion().get().getMajorVersion(), this.projectName.get(), this.projectVersion.get());
super.copy();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -132,6 +132,42 @@ void basicArchiveCreation() throws IOException {
.isEqualTo(this.classesPath);
assertThat(jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Lib")).isEqualTo(this.libPath);
assertThat(jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Version")).isNotNull();
assertThat(jarFile.getManifest().getMainAttributes().getValue("Implementation-Name"))
.isEqualTo(this.project.getName());
assertThat(jarFile.getManifest().getMainAttributes().getValue("Implementation-Version")).isNull();
}
}

@Test
void whenImplementationNameIsCustomizedItShouldAppearInArchiveManifest() throws IOException {
this.task.getMainClass().set("com.example.Main");
this.task.getManifest().getAttributes().put("Implementation-Name", "Customized");
executeTask();
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
assertThat(jarFile.getManifest().getMainAttributes().getValue("Implementation-Name"))
.isEqualTo("Customized");
}
}

@Test
void whenProjectVersionIsSetThenImplementationVersionShouldAppearInArchiveManifest() throws IOException {
this.project.setVersion("1.0.0");
this.task.getMainClass().set("com.example.Main");
executeTask();
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
assertThat(jarFile.getManifest().getMainAttributes().getValue("Implementation-Version")).isEqualTo("1.0.0");
}
}

@Test
void whenImplementationVersionIsCustomizedItShouldAppearInArchiveManifest() throws IOException {
this.project.setVersion("1.0.0");
this.task.getMainClass().set("com.example.Main");
this.task.getManifest().getAttributes().put("Implementation-Version", "Customized");
executeTask();
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
assertThat(jarFile.getManifest().getMainAttributes().getValue("Implementation-Version"))
.isEqualTo("Customized");
}
}

Expand Down

0 comments on commit 9378fc4

Please sign in to comment.