mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '3.1.x'
Closes gh-37882
This commit is contained in:
commit
60f05eaa94
@ -19,8 +19,10 @@ package org.springframework.boot.gradle.plugin;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.gradle.api.Action;
|
||||
import org.gradle.api.GradleException;
|
||||
import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
@ -35,6 +37,7 @@ import org.gradle.api.tasks.TaskContainer;
|
||||
import org.gradle.api.tasks.TaskProvider;
|
||||
import org.gradle.jvm.application.scripts.TemplateBasedScriptGenerator;
|
||||
import org.gradle.jvm.application.tasks.CreateStartScripts;
|
||||
import org.gradle.util.GradleVersion;
|
||||
|
||||
import org.springframework.boot.gradle.tasks.run.BootRun;
|
||||
|
||||
@ -56,7 +59,7 @@ final class ApplicationPluginAction implements PluginApplicationAction {
|
||||
.register("bootStartScripts", CreateStartScripts.class,
|
||||
(task) -> configureCreateStartScripts(project, javaApplication, distribution, task));
|
||||
CopySpec binCopySpec = project.copySpec().into("bin").from(bootStartScripts);
|
||||
binCopySpec.setFileMode(0755);
|
||||
configureFilePermissions(binCopySpec, 0755);
|
||||
distribution.getContents().with(binCopySpec);
|
||||
applyApplicationDefaultJvmArgsToRunTasks(project.getTasks(), javaApplication);
|
||||
}
|
||||
@ -95,7 +98,7 @@ final class ApplicationPluginAction implements PluginApplicationAction {
|
||||
|
||||
private CopySpec artifactFilesToLibCopySpec(Project project, Configuration configuration) {
|
||||
CopySpec copySpec = project.copySpec().into("lib").from(artifactFiles(configuration));
|
||||
copySpec.setFileMode(0644);
|
||||
configureFilePermissions(copySpec, 0644);
|
||||
return copySpec;
|
||||
}
|
||||
|
||||
@ -123,4 +126,34 @@ final class ApplicationPluginAction implements PluginApplicationAction {
|
||||
}
|
||||
}
|
||||
|
||||
private void configureFilePermissions(CopySpec copySpec, int mode) {
|
||||
if (GradleVersion.current().compareTo(GradleVersion.version("8.3")) >= 0) {
|
||||
try {
|
||||
Method filePermissions = copySpec.getClass().getMethod("filePermissions", Action.class);
|
||||
filePermissions.invoke(copySpec, new Action<Object>() {
|
||||
|
||||
@Override
|
||||
public void execute(Object filePermissions) {
|
||||
String unixPermissions = Integer.toString(mode, 8);
|
||||
try {
|
||||
Method unix = filePermissions.getClass().getMethod("unix", String.class);
|
||||
unix.invoke(filePermissions, unixPermissions);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new GradleException("Failed to set file permissions to '" + unixPermissions + "'",
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new GradleException("Failed to set file permissions", ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
copySpec.setFileMode(mode);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,7 +26,9 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.gradle.api.GradleException;
|
||||
import org.gradle.api.file.CopySpec;
|
||||
import org.gradle.api.file.FileCopyDetails;
|
||||
import org.gradle.api.file.FileTreeElement;
|
||||
@ -36,11 +38,13 @@ import org.gradle.api.internal.file.copy.CopyActionProcessingStream;
|
||||
import org.gradle.api.internal.file.copy.FileCopyDetailsInternal;
|
||||
import org.gradle.api.java.archives.Attributes;
|
||||
import org.gradle.api.java.archives.Manifest;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.specs.Spec;
|
||||
import org.gradle.api.specs.Specs;
|
||||
import org.gradle.api.tasks.WorkResult;
|
||||
import org.gradle.api.tasks.bundling.Jar;
|
||||
import org.gradle.api.tasks.util.PatternSet;
|
||||
import org.gradle.util.GradleVersion;
|
||||
|
||||
import org.springframework.boot.loader.tools.LoaderImplementation;
|
||||
|
||||
@ -128,8 +132,8 @@ class BootArchiveSupport {
|
||||
File output = jar.getArchiveFile().get().getAsFile();
|
||||
Manifest manifest = jar.getManifest();
|
||||
boolean preserveFileTimestamps = jar.isPreserveFileTimestamps();
|
||||
Integer dirMode = jar.getDirMode();
|
||||
Integer fileMode = jar.getFileMode();
|
||||
Integer dirMode = getDirMode(jar);
|
||||
Integer fileMode = getFileMode(jar);
|
||||
boolean includeDefaultLoader = isUsingDefaultLoader(jar);
|
||||
Spec<FileTreeElement> requiresUnpack = this.requiresUnpack.getAsSpec();
|
||||
Spec<FileTreeElement> exclusions = this.exclusions.getAsExcludeSpec();
|
||||
@ -143,6 +147,46 @@ class BootArchiveSupport {
|
||||
return jar.isReproducibleFileOrder() ? new ReproducibleOrderingCopyAction(action) : action;
|
||||
}
|
||||
|
||||
private Integer getDirMode(CopySpec copySpec) {
|
||||
return getMode(copySpec, "getDirPermissions", copySpec::getDirMode);
|
||||
}
|
||||
|
||||
private Integer getFileMode(CopySpec copySpec) {
|
||||
return getMode(copySpec, "getFilePermissions", copySpec::getFileMode);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Integer getMode(CopySpec copySpec, String methodName, Supplier<Integer> fallback) {
|
||||
if (GradleVersion.current().compareTo(GradleVersion.version("8.3")) >= 0) {
|
||||
try {
|
||||
Object filePermissions = ((Property<Object>) copySpec.getClass().getMethod(methodName).invoke(copySpec))
|
||||
.getOrNull();
|
||||
return getMode(filePermissions);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new GradleException("Failed to get permissions", ex);
|
||||
}
|
||||
}
|
||||
return fallback.get();
|
||||
}
|
||||
|
||||
private Integer getMode(Object permissions) throws Exception {
|
||||
if (permissions == null) {
|
||||
return null;
|
||||
}
|
||||
String user = asIntegerString(permissions.getClass().getMethod("getUser").invoke(permissions));
|
||||
String group = asIntegerString(permissions.getClass().getMethod("getGroup").invoke(permissions));
|
||||
String other = asIntegerString(permissions.getClass().getMethod("getOther").invoke(permissions));
|
||||
return Integer.parseInt("0" + user + group + other, 8);
|
||||
}
|
||||
|
||||
private String asIntegerString(Object permissions) throws Exception {
|
||||
boolean read = (boolean) permissions.getClass().getMethod("getRead").invoke(permissions);
|
||||
boolean write = (boolean) permissions.getClass().getMethod("getWrite").invoke(permissions);
|
||||
boolean execute = (boolean) permissions.getClass().getMethod("getExecute").invoke(permissions);
|
||||
return Integer.toString(((read) ? 4 : 0) + ((write) ? 2 : 0) + ((execute) ? 1 : 0));
|
||||
}
|
||||
|
||||
private boolean isUsingDefaultLoader(Jar jar) {
|
||||
return DEFAULT_LAUNCHER_CLASSES.contains(jar.getManifest().getAttributes().get("Main-Class"));
|
||||
}
|
||||
|
@ -46,9 +46,11 @@ import org.gradle.api.internal.file.copy.CopyAction;
|
||||
import org.gradle.api.internal.file.copy.CopyActionProcessingStream;
|
||||
import org.gradle.api.java.archives.Attributes;
|
||||
import org.gradle.api.java.archives.Manifest;
|
||||
import org.gradle.api.provider.Provider;
|
||||
import org.gradle.api.specs.Spec;
|
||||
import org.gradle.api.tasks.WorkResult;
|
||||
import org.gradle.api.tasks.WorkResults;
|
||||
import org.gradle.util.GradleVersion;
|
||||
|
||||
import org.springframework.boot.gradle.tasks.bundling.ResolvedDependencies.DependencyDescriptor;
|
||||
import org.springframework.boot.loader.tools.DefaultLaunchScript;
|
||||
@ -461,7 +463,24 @@ class BootZipCopyAction implements CopyAction {
|
||||
|
||||
private int getFileMode(FileCopyDetails details) {
|
||||
return (BootZipCopyAction.this.fileMode != null) ? BootZipCopyAction.this.fileMode
|
||||
: UnixStat.FILE_FLAG | details.getMode();
|
||||
: UnixStat.FILE_FLAG | getPermissions(details);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private int getPermissions(FileCopyDetails details) {
|
||||
if (GradleVersion.current().compareTo(GradleVersion.version("8.3")) >= 0) {
|
||||
try {
|
||||
Object permissions = ((Provider<Object>) details.getClass()
|
||||
.getMethod("getImmutablePermissions")
|
||||
.invoke(details)).get();
|
||||
return ((Provider<Integer>) permissions.getClass().getMethod("toUnixNumeric").invoke(permissions))
|
||||
.get();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new GradleException("Failed to get permissions", ex);
|
||||
}
|
||||
}
|
||||
return details.getMode();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user