mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Use new permissions API with Gradle 8.3+
Closes gh-37878
This commit is contained in:
parent
c0eb2cb4ae
commit
42ee6b9e74
@ -19,8 +19,10 @@ package org.springframework.boot.gradle.plugin;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
|
import org.gradle.api.Action;
|
||||||
import org.gradle.api.GradleException;
|
import org.gradle.api.GradleException;
|
||||||
import org.gradle.api.Plugin;
|
import org.gradle.api.Plugin;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
@ -34,6 +36,7 @@ import org.gradle.api.plugins.JavaApplication;
|
|||||||
import org.gradle.api.tasks.TaskProvider;
|
import org.gradle.api.tasks.TaskProvider;
|
||||||
import org.gradle.jvm.application.scripts.TemplateBasedScriptGenerator;
|
import org.gradle.jvm.application.scripts.TemplateBasedScriptGenerator;
|
||||||
import org.gradle.jvm.application.tasks.CreateStartScripts;
|
import org.gradle.jvm.application.tasks.CreateStartScripts;
|
||||||
|
import org.gradle.util.GradleVersion;
|
||||||
|
|
||||||
import org.springframework.boot.gradle.tasks.run.BootRun;
|
import org.springframework.boot.gradle.tasks.run.BootRun;
|
||||||
|
|
||||||
@ -55,7 +58,7 @@ final class ApplicationPluginAction implements PluginApplicationAction {
|
|||||||
.register("bootStartScripts", CreateStartScripts.class,
|
.register("bootStartScripts", CreateStartScripts.class,
|
||||||
(task) -> configureCreateStartScripts(project, javaApplication, distribution, task));
|
(task) -> configureCreateStartScripts(project, javaApplication, distribution, task));
|
||||||
CopySpec binCopySpec = project.copySpec().into("bin").from(bootStartScripts);
|
CopySpec binCopySpec = project.copySpec().into("bin").from(bootStartScripts);
|
||||||
binCopySpec.setFileMode(0755);
|
configureFilePermissions(binCopySpec, 0755);
|
||||||
distribution.getContents().with(binCopySpec);
|
distribution.getContents().with(binCopySpec);
|
||||||
project.getTasks()
|
project.getTasks()
|
||||||
.named(SpringBootPlugin.BOOT_RUN_TASK_NAME, BootRun.class)
|
.named(SpringBootPlugin.BOOT_RUN_TASK_NAME, BootRun.class)
|
||||||
@ -85,7 +88,7 @@ final class ApplicationPluginAction implements PluginApplicationAction {
|
|||||||
|
|
||||||
private CopySpec artifactFilesToLibCopySpec(Project project, Configuration configuration) {
|
private CopySpec artifactFilesToLibCopySpec(Project project, Configuration configuration) {
|
||||||
CopySpec copySpec = project.copySpec().into("lib").from(artifactFiles(configuration));
|
CopySpec copySpec = project.copySpec().into("lib").from(artifactFiles(configuration));
|
||||||
copySpec.setFileMode(0644);
|
configureFilePermissions(copySpec, 0644);
|
||||||
return copySpec;
|
return copySpec;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,4 +116,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.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.function.Function;
|
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.CopySpec;
|
||||||
import org.gradle.api.file.FileCopyDetails;
|
import org.gradle.api.file.FileCopyDetails;
|
||||||
import org.gradle.api.file.FileTreeElement;
|
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.internal.file.copy.FileCopyDetailsInternal;
|
||||||
import org.gradle.api.java.archives.Attributes;
|
import org.gradle.api.java.archives.Attributes;
|
||||||
import org.gradle.api.java.archives.Manifest;
|
import org.gradle.api.java.archives.Manifest;
|
||||||
|
import org.gradle.api.provider.Property;
|
||||||
import org.gradle.api.specs.Spec;
|
import org.gradle.api.specs.Spec;
|
||||||
import org.gradle.api.specs.Specs;
|
import org.gradle.api.specs.Specs;
|
||||||
import org.gradle.api.tasks.WorkResult;
|
import org.gradle.api.tasks.WorkResult;
|
||||||
import org.gradle.api.tasks.bundling.Jar;
|
import org.gradle.api.tasks.bundling.Jar;
|
||||||
import org.gradle.api.tasks.util.PatternSet;
|
import org.gradle.api.tasks.util.PatternSet;
|
||||||
|
import org.gradle.util.GradleVersion;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Support class for implementations of {@link BootArchive}.
|
* Support class for implementations of {@link BootArchive}.
|
||||||
@ -114,8 +118,8 @@ class BootArchiveSupport {
|
|||||||
File output = jar.getArchiveFile().get().getAsFile();
|
File output = jar.getArchiveFile().get().getAsFile();
|
||||||
Manifest manifest = jar.getManifest();
|
Manifest manifest = jar.getManifest();
|
||||||
boolean preserveFileTimestamps = jar.isPreserveFileTimestamps();
|
boolean preserveFileTimestamps = jar.isPreserveFileTimestamps();
|
||||||
Integer dirMode = jar.getDirMode();
|
Integer dirMode = getDirMode(jar);
|
||||||
Integer fileMode = jar.getFileMode();
|
Integer fileMode = getFileMode(jar);
|
||||||
boolean includeDefaultLoader = isUsingDefaultLoader(jar);
|
boolean includeDefaultLoader = isUsingDefaultLoader(jar);
|
||||||
Spec<FileTreeElement> requiresUnpack = this.requiresUnpack.getAsSpec();
|
Spec<FileTreeElement> requiresUnpack = this.requiresUnpack.getAsSpec();
|
||||||
Spec<FileTreeElement> exclusions = this.exclusions.getAsExcludeSpec();
|
Spec<FileTreeElement> exclusions = this.exclusions.getAsExcludeSpec();
|
||||||
@ -129,6 +133,46 @@ class BootArchiveSupport {
|
|||||||
return jar.isReproducibleFileOrder() ? new ReproducibleOrderingCopyAction(action) : action;
|
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) {
|
private boolean isUsingDefaultLoader(Jar jar) {
|
||||||
return DEFAULT_LAUNCHER_CLASSES.contains(jar.getManifest().getAttributes().get("Main-Class"));
|
return DEFAULT_LAUNCHER_CLASSES.contains(jar.getManifest().getAttributes().get("Main-Class"));
|
||||||
}
|
}
|
||||||
|
@ -44,9 +44,11 @@ import org.gradle.api.internal.file.copy.CopyAction;
|
|||||||
import org.gradle.api.internal.file.copy.CopyActionProcessingStream;
|
import org.gradle.api.internal.file.copy.CopyActionProcessingStream;
|
||||||
import org.gradle.api.java.archives.Attributes;
|
import org.gradle.api.java.archives.Attributes;
|
||||||
import org.gradle.api.java.archives.Manifest;
|
import org.gradle.api.java.archives.Manifest;
|
||||||
|
import org.gradle.api.provider.Provider;
|
||||||
import org.gradle.api.specs.Spec;
|
import org.gradle.api.specs.Spec;
|
||||||
import org.gradle.api.tasks.WorkResult;
|
import org.gradle.api.tasks.WorkResult;
|
||||||
import org.gradle.api.tasks.WorkResults;
|
import org.gradle.api.tasks.WorkResults;
|
||||||
|
import org.gradle.util.GradleVersion;
|
||||||
|
|
||||||
import org.springframework.boot.loader.tools.DefaultLaunchScript;
|
import org.springframework.boot.loader.tools.DefaultLaunchScript;
|
||||||
import org.springframework.boot.loader.tools.FileUtils;
|
import org.springframework.boot.loader.tools.FileUtils;
|
||||||
@ -412,7 +414,24 @@ class BootZipCopyAction implements CopyAction {
|
|||||||
|
|
||||||
private int getFileMode(FileCopyDetails details) {
|
private int getFileMode(FileCopyDetails details) {
|
||||||
return (BootZipCopyAction.this.fileMode != null) ? BootZipCopyAction.this.fileMode
|
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