|
| 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 | +} |
0 commit comments