Skip to content

Commit ae2693b

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-37880
2 parents 6a7fc6b + 42ee6b9 commit ae2693b

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ApplicationPluginAction.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.io.IOException;
2020
import java.io.InputStreamReader;
2121
import java.io.StringWriter;
22+
import java.lang.reflect.Method;
2223
import java.util.concurrent.Callable;
2324

25+
import org.gradle.api.Action;
2426
import org.gradle.api.GradleException;
2527
import org.gradle.api.Plugin;
2628
import org.gradle.api.Project;
@@ -34,6 +36,7 @@
3436
import org.gradle.api.tasks.TaskProvider;
3537
import org.gradle.jvm.application.scripts.TemplateBasedScriptGenerator;
3638
import org.gradle.jvm.application.tasks.CreateStartScripts;
39+
import org.gradle.util.GradleVersion;
3740

3841
import org.springframework.boot.gradle.tasks.run.BootRun;
3942

@@ -55,7 +58,7 @@ public void execute(Project project) {
5558
.register("bootStartScripts", CreateStartScripts.class,
5659
(task) -> configureCreateStartScripts(project, javaApplication, distribution, task));
5760
CopySpec binCopySpec = project.copySpec().into("bin").from(bootStartScripts);
58-
binCopySpec.setFileMode(0755);
61+
configureFilePermissions(binCopySpec, 0755);
5962
distribution.getContents().with(binCopySpec);
6063
project.getTasks()
6164
.named(SpringBootPlugin.BOOT_RUN_TASK_NAME, BootRun.class)
@@ -85,7 +88,7 @@ private void configureCreateStartScripts(Project project, JavaApplication javaAp
8588

8689
private CopySpec artifactFilesToLibCopySpec(Project project, Configuration configuration) {
8790
CopySpec copySpec = project.copySpec().into("lib").from(artifactFiles(configuration));
88-
copySpec.setFileMode(0644);
91+
configureFilePermissions(copySpec, 0644);
8992
return copySpec;
9093
}
9194

@@ -113,4 +116,34 @@ private String loadResource(String name) {
113116
}
114117
}
115118

119+
private void configureFilePermissions(CopySpec copySpec, int mode) {
120+
if (GradleVersion.current().compareTo(GradleVersion.version("8.3")) >= 0) {
121+
try {
122+
Method filePermissions = copySpec.getClass().getMethod("filePermissions", Action.class);
123+
filePermissions.invoke(copySpec, new Action<Object>() {
124+
125+
@Override
126+
public void execute(Object filePermissions) {
127+
String unixPermissions = Integer.toString(mode, 8);
128+
try {
129+
Method unix = filePermissions.getClass().getMethod("unix", String.class);
130+
unix.invoke(filePermissions, unixPermissions);
131+
}
132+
catch (Exception ex) {
133+
throw new GradleException("Failed to set file permissions to '" + unixPermissions + "'",
134+
ex);
135+
}
136+
}
137+
138+
});
139+
}
140+
catch (Exception ex) {
141+
throw new GradleException("Failed to set file permissions", ex);
142+
}
143+
}
144+
else {
145+
copySpec.setFileMode(mode);
146+
}
147+
}
148+
116149
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import java.util.Set;
2727
import java.util.TreeMap;
2828
import java.util.function.Function;
29+
import java.util.function.Supplier;
2930

31+
import org.gradle.api.GradleException;
3032
import org.gradle.api.file.CopySpec;
3133
import org.gradle.api.file.FileCopyDetails;
3234
import org.gradle.api.file.FileTreeElement;
@@ -36,11 +38,13 @@
3638
import org.gradle.api.internal.file.copy.FileCopyDetailsInternal;
3739
import org.gradle.api.java.archives.Attributes;
3840
import org.gradle.api.java.archives.Manifest;
41+
import org.gradle.api.provider.Property;
3942
import org.gradle.api.specs.Spec;
4043
import org.gradle.api.specs.Specs;
4144
import org.gradle.api.tasks.WorkResult;
4245
import org.gradle.api.tasks.bundling.Jar;
4346
import org.gradle.api.tasks.util.PatternSet;
47+
import org.gradle.util.GradleVersion;
4448

4549
/**
4650
* Support class for implementations of {@link BootArchive}.
@@ -125,8 +129,8 @@ CopyAction createCopyAction(Jar jar, ResolvedDependencies resolvedDependencies,
125129
File output = jar.getArchiveFile().get().getAsFile();
126130
Manifest manifest = jar.getManifest();
127131
boolean preserveFileTimestamps = jar.isPreserveFileTimestamps();
128-
Integer dirMode = jar.getDirMode();
129-
Integer fileMode = jar.getFileMode();
132+
Integer dirMode = getDirMode(jar);
133+
Integer fileMode = getFileMode(jar);
130134
boolean includeDefaultLoader = isUsingDefaultLoader(jar);
131135
Spec<FileTreeElement> requiresUnpack = this.requiresUnpack.getAsSpec();
132136
Spec<FileTreeElement> exclusions = this.exclusions.getAsExcludeSpec();
@@ -140,6 +144,46 @@ CopyAction createCopyAction(Jar jar, ResolvedDependencies resolvedDependencies,
140144
return jar.isReproducibleFileOrder() ? new ReproducibleOrderingCopyAction(action) : action;
141145
}
142146

147+
private Integer getDirMode(CopySpec copySpec) {
148+
return getMode(copySpec, "getDirPermissions", copySpec::getDirMode);
149+
}
150+
151+
private Integer getFileMode(CopySpec copySpec) {
152+
return getMode(copySpec, "getFilePermissions", copySpec::getFileMode);
153+
}
154+
155+
@SuppressWarnings("unchecked")
156+
private Integer getMode(CopySpec copySpec, String methodName, Supplier<Integer> fallback) {
157+
if (GradleVersion.current().compareTo(GradleVersion.version("8.3")) >= 0) {
158+
try {
159+
Object filePermissions = ((Property<Object>) copySpec.getClass().getMethod(methodName).invoke(copySpec))
160+
.getOrNull();
161+
return getMode(filePermissions);
162+
}
163+
catch (Exception ex) {
164+
throw new GradleException("Failed to get permissions", ex);
165+
}
166+
}
167+
return fallback.get();
168+
}
169+
170+
private Integer getMode(Object permissions) throws Exception {
171+
if (permissions == null) {
172+
return null;
173+
}
174+
String user = asIntegerString(permissions.getClass().getMethod("getUser").invoke(permissions));
175+
String group = asIntegerString(permissions.getClass().getMethod("getGroup").invoke(permissions));
176+
String other = asIntegerString(permissions.getClass().getMethod("getOther").invoke(permissions));
177+
return Integer.parseInt("0" + user + group + other, 8);
178+
}
179+
180+
private String asIntegerString(Object permissions) throws Exception {
181+
boolean read = (boolean) permissions.getClass().getMethod("getRead").invoke(permissions);
182+
boolean write = (boolean) permissions.getClass().getMethod("getWrite").invoke(permissions);
183+
boolean execute = (boolean) permissions.getClass().getMethod("getExecute").invoke(permissions);
184+
return Integer.toString(((read) ? 4 : 0) + ((write) ? 2 : 0) + ((execute) ? 1 : 0));
185+
}
186+
143187
private boolean isUsingDefaultLoader(Jar jar) {
144188
return DEFAULT_LAUNCHER_CLASSES.contains(jar.getManifest().getAttributes().get("Main-Class"));
145189
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@
4646
import org.gradle.api.internal.file.copy.CopyActionProcessingStream;
4747
import org.gradle.api.java.archives.Attributes;
4848
import org.gradle.api.java.archives.Manifest;
49+
import org.gradle.api.provider.Provider;
4950
import org.gradle.api.specs.Spec;
5051
import org.gradle.api.tasks.WorkResult;
5152
import org.gradle.api.tasks.WorkResults;
53+
import org.gradle.util.GradleVersion;
5254

5355
import org.springframework.boot.gradle.tasks.bundling.ResolvedDependencies.DependencyDescriptor;
5456
import org.springframework.boot.loader.tools.DefaultLaunchScript;
@@ -455,7 +457,24 @@ private int getFileMode() {
455457

456458
private int getFileMode(FileCopyDetails details) {
457459
return (BootZipCopyAction.this.fileMode != null) ? BootZipCopyAction.this.fileMode
458-
: UnixStat.FILE_FLAG | details.getMode();
460+
: UnixStat.FILE_FLAG | getPermissions(details);
461+
}
462+
463+
@SuppressWarnings("unchecked")
464+
private int getPermissions(FileCopyDetails details) {
465+
if (GradleVersion.current().compareTo(GradleVersion.version("8.3")) >= 0) {
466+
try {
467+
Object permissions = ((Provider<Object>) details.getClass()
468+
.getMethod("getImmutablePermissions")
469+
.invoke(details)).get();
470+
return ((Provider<Integer>) permissions.getClass().getMethod("toUnixNumeric").invoke(permissions))
471+
.get();
472+
}
473+
catch (Exception ex) {
474+
throw new GradleException("Failed to get permissions", ex);
475+
}
476+
}
477+
return details.getMode();
459478
}
460479

461480
}

0 commit comments

Comments
 (0)