forked from ehcache/ehcache3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaBaseConvention.java
More file actions
160 lines (141 loc) · 5.88 KB
/
JavaBaseConvention.java
File metadata and controls
160 lines (141 loc) · 5.88 KB
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
154
155
156
157
158
159
160
package org.ehcache.build.conventions;
import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.java.archives.Attributes;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.api.tasks.testing.Test;
import org.gradle.external.javadoc.CoreJavadocOptions;
import org.gradle.internal.jvm.JavaInfo;
import org.gradle.internal.jvm.Jvm;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.process.internal.ExecException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static java.util.regex.Pattern.quote;
public class JavaBaseConvention implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getPlugins().apply(JavaBasePlugin.class);
project.getPlugins().apply(BaseConvention.class);
JavaInfo testJava = fetchTestJava(project);
project.getExtensions().getExtraProperties().set("testJava", testJava);
project.getExtensions().configure(JavaPluginExtension.class, java -> {
java.toolchain(spec -> spec.getLanguageVersion().convention(JavaLanguageVersion.of(8)));
});
project.getTasks().withType(Jar.class).configureEach(jar -> {
jar.manifest(manifest -> {
Attributes attributes = manifest.getAttributes();
attributes.put("Implementation-Title", project.getName());
attributes.put("Implementation-Vendor-Id", project.getGroup());
attributes.put("Implementation-Version", project.getVersion());
attributes.put("Implementation-Revision", getRevision(project));
attributes.put("Built-By", System.getProperty("user.name"));
attributes.put("Built-JDK", System.getProperty("java.version"));
});
jar.from(project.getRootProject().file("LICENSE"));
});
project.getTasks().withType(Test.class).configureEach(test -> {
test.setExecutable(testJava.getJavaExecutable());
test.setMaxHeapSize("256m");
test.setMaxParallelForks(16);
test.systemProperty("java.awt.headless", "true");
});
project.getTasks().withType(JavaCompile.class).configureEach(compile -> {
compile.getOptions().setEncoding("UTF-8");
compile.getOptions().setCompilerArgs(asList("-Werror", "-Xlint:all"));
});
project.getTasks().withType(Javadoc.class).configureEach(javadoc -> {
javadoc.setTitle(project.getName() + " " + project.getVersion() + " API");
javadoc.exclude(fte -> !isPublicApi(fte.getFile().toPath()));
javadoc.getOptions().setEncoding("UTF-8");
((CoreJavadocOptions) javadoc.getOptions()).addStringOption("Xdoclint:none", "-quiet");
});
}
private static boolean isPublicApi(Path source) {
if (Files.isDirectory(source)) {
return true;
} else {
return (isTypeAnnotated(source.getParent(), "PublicApi") && !isTypeAnnotated(source, "PrivateApi")) || isTypeAnnotated(source, "PublicApi");
}
}
private static boolean isTypeAnnotated(Path source, String annotation) {
if (Files.isDirectory(source)) {
return isTypeAnnotated(source.resolve("package-info.java"), annotation);
} else if (Files.isRegularFile(source) && source.getFileName().toString().endsWith(".java")) {
try (Stream<String> lines = Files.lines(source, StandardCharsets.UTF_8)) {
return lines.anyMatch(line -> line.matches("(?:^|^.*\\s+)@" + quote(annotation) + "(?:$|\\s+.*$)"));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
return false;
}
}
private static JavaInfo fetchTestJava(Project project) {
Object testVM = project.findProperty("testVM");
if (testVM == null) {
return Jvm.current();
} else {
File jvmHome = project.file(testVM);
if (!jvmHome.exists() && project.hasProperty(testVM.toString())) {
testVM = project.property(testVM.toString());
jvmHome = project.file(testVM);
}
return jvmForHome(project, jvmHome);
}
}
private static final Pattern VERSION_OUTPUT = Pattern.compile("\\w+ version \"(?<version>.+)\"");
private static Jvm jvmForHome(Project project, File home) {
File java = Jvm.forHome(home).getJavaExecutable();
OutputStream stdout = new ByteArrayOutputStream();
OutputStream stderr = new ByteArrayOutputStream();
project.exec(spec -> {
spec.executable(java);
spec.args("-version");
spec.setStandardOutput(stdout);
spec.setErrorOutput(stderr);
});
String versionOutput = stderr.toString();
Matcher matcher = VERSION_OUTPUT.matcher(versionOutput);
if (matcher.find()) {
return Jvm.discovered(home, null, JavaVersion.toVersion(matcher.group("version")));
} else {
throw new IllegalArgumentException("Could not parse version of " + java + " from output:\n" + versionOutput);
}
}
private static Object getRevision(Project project) {
String envCommit = System.getenv("GIT_COMMIT");
if(envCommit != null) {
return envCommit;
} else {
try {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
project.exec(spec -> {
spec.executable("git");
spec.args("rev-parse", "HEAD");
spec.setStandardOutput(stdout);
spec.setErrorOutput(stderr);
}).assertNormalExitValue();
return stdout.toString().trim();
} catch (ExecException e) {
return "Unknown";
}
}
}
}