/
SpringBootExtension.java
153 lines (135 loc) · 4.95 KB
/
SpringBootExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.gradle.dsl;
import java.io.File;
import java.lang.reflect.Method;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.jvm.tasks.Jar;
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo;
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfoProperties;
/**
* Entry point to Spring Boot's Gradle DSL.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
public class SpringBootExtension {
private final Project project;
private String mainClassName;
/**
* Creates a new {@code SpringBootPluginExtension} that is associated with the given
* {@code project}.
* @param project the project
*/
public SpringBootExtension(Project project) {
this.project = project;
}
/**
* Returns the main class name of the application.
* @return the name of the application's main class
*/
public String getMainClassName() {
return this.mainClassName;
}
/**
* Sets the main class name of the application.
* @param mainClassName the name of the application's main class
*/
public void setMainClassName(String mainClassName) {
this.mainClassName = mainClassName;
}
/**
* Creates a new {@link BuildInfo} task named {@code bootBuildInfo} and configures the
* Java plugin's {@code classes} task to depend upon it.
* <p>
* By default, the task's destination dir will be a directory named {@code META-INF}
* beneath the main source set's resources output directory, and the task's project
* artifact will be the base name of the {@code bootWar} or {@code bootJar} task.
*/
public void buildInfo() {
this.buildInfo(null);
}
/**
* Creates a new {@link BuildInfo} task named {@code bootBuildInfo} and configures the
* Java plugin's {@code classes} task to depend upon it. The task is passed to the
* given {@code configurer} for further configuration.
* <p>
* By default, the task's destination dir will be a directory named {@code META-INF}
* beneath the main source set's resources output directory, and the task's project
* artifact will be the base name of the {@code bootWar} or {@code bootJar} task.
* @param configurer the task configurer
*/
public void buildInfo(Action<BuildInfo> configurer) {
BuildInfo bootBuildInfo = this.project.getTasks().create("bootBuildInfo", BuildInfo.class);
bootBuildInfo.setGroup(BasePlugin.BUILD_GROUP);
bootBuildInfo.setDescription("Generates a META-INF/build-info.properties file.");
this.project.getPlugins().withType(JavaPlugin.class, (plugin) -> {
this.project.getTasks().getByName(JavaPlugin.CLASSES_TASK_NAME).dependsOn(bootBuildInfo);
this.project.afterEvaluate((evaluated) -> {
BuildInfoProperties properties = bootBuildInfo.getProperties();
if (properties.getArtifact() == null) {
properties.setArtifact(determineArtifactBaseName());
}
});
bootBuildInfo.getConventionMapping().map("destinationDir",
() -> new File(determineMainSourceSetResourcesOutputDir(), "META-INF"));
});
if (configurer != null) {
configurer.execute(bootBuildInfo);
}
}
private File determineMainSourceSetResourcesOutputDir() {
return this.project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets()
.getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput().getResourcesDir();
}
private String determineArtifactBaseName() {
Jar artifactTask = findArtifactTask();
return (artifactTask != null) ? getArchiveBaseName(artifactTask) : null;
}
private Jar findArtifactTask() {
Jar artifactTask = (Jar) this.project.getTasks().findByName("bootWar");
if (artifactTask != null) {
return artifactTask;
}
return (Jar) this.project.getTasks().findByName("bootJar");
}
private static String getArchiveBaseName(AbstractArchiveTask task) {
try {
Method method = findMethod(task.getClass(), "getArchiveBaseName");
if (method != null) {
return (String) method.invoke(task);
}
}
catch (Exception ex) {
// Continue
}
return task.getBaseName();
}
private static Method findMethod(Class<?> type, String name) {
for (Method candidate : type.getMethods()) {
if (candidate.getName().equals(name)) {
return candidate;
}
}
return null;
}
}