Skip to content

Commit 61cdf0a

Browse files
committed
Create JavaConventions for better options
- Looks like with gradle we don't get automatic utf-8 encoding with compiler so add it and same time use separate JavaConventions as we want to apply it to modules and samples. - Relates #470
1 parent bb7afdf commit 61cdf0a

File tree

3 files changed

+84
-17
lines changed

3 files changed

+84
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.gradle;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import org.gradle.api.JavaVersion;
22+
import org.gradle.api.Project;
23+
import org.gradle.api.plugins.JavaBasePlugin;
24+
import org.gradle.api.tasks.compile.JavaCompile;
25+
import org.gradle.api.tasks.javadoc.Javadoc;
26+
import org.gradle.api.tasks.testing.Test;
27+
import org.gradle.external.javadoc.CoreJavadocOptions;
28+
29+
/**
30+
* @author Janne Valkealahti
31+
*/
32+
class JavaConventions {
33+
34+
private static final String SOURCE_AND_TARGET_COMPATIBILITY = "17";
35+
36+
void apply(Project project) {
37+
project.getPlugins().withType(JavaBasePlugin.class, java -> {
38+
configureJavaConventions(project);
39+
configureJavadocConventions(project);
40+
configureTestConventions(project);
41+
});
42+
}
43+
44+
private void configureJavadocConventions(Project project) {
45+
project.getTasks().withType(Javadoc.class, (javadoc) -> {
46+
CoreJavadocOptions options = (CoreJavadocOptions) javadoc.getOptions();
47+
options.source("17");
48+
options.encoding("UTF-8");
49+
options.addStringOption("Xdoclint:none", "-quiet");
50+
});
51+
}
52+
53+
private void configureJavaConventions(Project project) {
54+
project.getTasks().withType(JavaCompile.class, (compile) -> {
55+
compile.getOptions().setEncoding("UTF-8");
56+
List<String> args = compile.getOptions().getCompilerArgs();
57+
if (!args.contains("-parameters")) {
58+
args.add("-parameters");
59+
}
60+
if (project.hasProperty("toolchainVersion")) {
61+
compile.setSourceCompatibility(SOURCE_AND_TARGET_COMPATIBILITY);
62+
compile.setTargetCompatibility(SOURCE_AND_TARGET_COMPATIBILITY);
63+
}
64+
else if (buildingWithJava17(project)) {
65+
args.addAll(Arrays.asList("-Xdoclint:none"));
66+
// TODO: When we're without javadoc errors
67+
// args.addAll(Arrays.asList("-Werror", "-Xlint:unchecked", "-Xlint:deprecation", "-Xlint:rawtypes",
68+
// "-Xlint:varargs"));
69+
}
70+
});
71+
}
72+
73+
private boolean buildingWithJava17(Project project) {
74+
return JavaVersion.current() == JavaVersion.VERSION_17;
75+
}
76+
77+
private void configureTestConventions(Project project) {
78+
project.getTasks().withType(Test.class, test -> {
79+
test.useJUnitPlatform();
80+
});
81+
}
82+
}

buildSrc/src/main/java/org/springframework/shell/gradle/ModulePlugin.java

+1-17
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717

1818
import org.gradle.api.Plugin;
1919
import org.gradle.api.Project;
20-
import org.gradle.api.plugins.JavaBasePlugin;
2120
import org.gradle.api.plugins.JavaLibraryPlugin;
2221
import org.gradle.api.plugins.JavaPlugin;
2322
import org.gradle.api.plugins.PluginManager;
24-
import org.gradle.api.tasks.javadoc.Javadoc;
25-
import org.gradle.api.tasks.testing.Test;
26-
import org.gradle.external.javadoc.CoreJavadocOptions;
2723

2824
/**
2925
* @author Janne Valkealahti
@@ -38,18 +34,6 @@ public final void apply(Project project) {
3834
pluginManager.apply(JavaLibraryPlugin.class);
3935
pluginManager.apply(SpringMavenPlugin.class);
4036
new ArtifactoryConventions().apply(project);
41-
42-
project.getPlugins().withType(JavaBasePlugin.class, java -> {
43-
project.getTasks().withType(Javadoc.class, (javadoc) -> {
44-
CoreJavadocOptions options = (CoreJavadocOptions) javadoc.getOptions();
45-
options.source("17");
46-
options.encoding("UTF-8");
47-
options.addStringOption("Xdoclint:none", "-quiet");
48-
});
49-
50-
project.getTasks().withType(Test.class, test -> {
51-
test.useJUnitPlatform();
52-
});
53-
});
37+
new JavaConventions().apply(project);
5438
}
5539
}

buildSrc/src/main/java/org/springframework/shell/gradle/SamplePlugin.java

+1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ public void apply(Project project) {
3030
PluginManager pluginManager = project.getPluginManager();
3131
pluginManager.apply(JavaPlugin.class);
3232
pluginManager.apply(ManagementConfigurationPlugin.class);
33+
new JavaConventions().apply(project);
3334
}
3435
}

0 commit comments

Comments
 (0)