Skip to content

Commit

Permalink
SONAR-14951 Scanners require Java 11
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb authored and sonartech committed Jun 9, 2021
1 parent ef7557b commit 41d7390
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 135 deletions.
74 changes: 66 additions & 8 deletions build.gradle
Expand Up @@ -164,9 +164,6 @@ subprojects {
apply plugin: 'idea'
apply plugin: 'signing'

sourceCompatibility = 1.8
targetCompatibility = 1.8

// do not deploy to Artifactory by default
artifactoryPublish.skip = true

Expand All @@ -177,8 +174,74 @@ subprojects {

ext {
protobufVersion = '3.11.4'

// define a method which can be called by project to change Java version to compile to
configureCompileJavaToVersion = { javaVersion ->
if ( javaVersion != 11 & javaVersion != 8) {
throw new InvalidUserDataException("Only Java 8 and 11 are supported")
}
if ( javaVersion == 8 ) {
println "Configuring project ${project.name} to compile to Java ${javaVersion}"

if (!project.hasProperty('skipJava8Checks')) {
task ensureDependenciesRunOnJava8(dependsOn: [configurations.compileClasspath]) {
ext.readJavaMajorVersion = { classFile ->
classFile.withDataInputStream({ d ->
if (d.skip(7) == 7) {
// read the version of the class file
d.read()
} else {
throw new GradleException("Could not read major version from $classFile")
}
})
}

doLast {
[configurations.compileClasspath].each { config ->
config.resolvedConfiguration.files
.findAll({ f -> f.name.endsWith("jar") })
.each { jarFile ->
def onlyJava8 = true
zipTree(jarFile)
.matching({
include "**/*.class"
// multi-release jar files were introduced with Java 9 => contains only classes targeting Java 9+
exclude "META-INF/versions/**"
// ignore module-info existing in some archives for Java 9 forward compatibility
exclude "module-info.class"
})
.files
.each { classFile ->
int javaMajorVersion = readJavaMajorVersion(classFile)
if (javaMajorVersion > 52) {
println "$classFile has been compiled to a more recent version of Java than 8 (java8=52, java11=55, actual=$javaMajorVersion)"
onlyJava8 = false
}
}
if (!onlyJava8) {
throw new GradleException("Dependency ${jarFile} in configuration ${config.name} contains class file(s) compiled to a Java version > Java 8 (see logs for details)")
}
}
}
}
}

compileJava.dependsOn ensureDependenciesRunOnJava8
}
}

sourceCompatibility = javaVersion
targetCompatibility = javaVersion

tasks.withType(JavaCompile) {
options.compilerArgs.addAll(['--release', javaVersion + ''])
options.encoding = 'UTF-8'
}
}
}

configureCompileJavaToVersion 11

sonarqube {
properties {
property 'sonar.moduleKey', project.group + ':' + project.name
Expand Down Expand Up @@ -367,11 +430,6 @@ subprojects {
exclude group: 'javax.mail', module: 'mail'
}

tasks.withType(JavaCompile) {
options.compilerArgs.addAll(['--release', '8'])
options.encoding = 'UTF-8'
}

tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.encoding = 'UTF-8'
Expand Down
2 changes: 2 additions & 0 deletions plugins/sonar-xoo-plugin/build.gradle
Expand Up @@ -2,6 +2,8 @@ configurations {
testCompile.extendsFrom(compileOnly)
}

configureCompileJavaToVersion 8

dependencies {
compile 'com.google.guava:guava'
compile 'commons-io:commons-io'
Expand Down
Expand Up @@ -19,12 +19,12 @@
*/
package org.sonar.ce.container;

import com.google.common.collect.ImmutableList;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -100,7 +100,7 @@ public void stop() {
@Override
public Collection<PluginInfo> getPluginInfos() {
checkState(started.get(), NOT_STARTED_YET);
return ImmutableList.copyOf(pluginInfosByKeys.values());
return Set.copyOf(pluginInfosByKeys.values());
}

@Override
Expand Down
Expand Up @@ -25,21 +25,18 @@

public class CeJvmOptions extends JvmOptions<CeJvmOptions> {

public CeJvmOptions(File tmpDir, JavaVersion javaVersion) {
super(mandatoryOptions(tmpDir, javaVersion));
public CeJvmOptions(File tmpDir) {
super(mandatoryOptions(tmpDir));
}

private static Map<String, String> mandatoryOptions(File tmpDir, JavaVersion javaVersion) {
private static Map<String, String> mandatoryOptions(File tmpDir) {
Map<String, String> res = new LinkedHashMap<>(3);
res.put("-Djava.awt.headless=", "true");
res.put("-Dfile.encoding=", "UTF-8");
res.put("-Djava.io.tmpdir=", tmpDir.getAbsolutePath());
res.put("-XX:-OmitStackTraceInFastThrow", "");

if (javaVersion.isAtLeastJava11()) {
// avoid illegal reflective access operations done by MyBatis
res.put("--add-opens=java.base/java.util=ALL-UNNAMED", "");
}
// avoid illegal reflective access operations done by MyBatis
res.put("--add-opens=java.base/java.util=ALL-UNNAMED", "");
return res;
}
}
Expand Up @@ -75,13 +75,11 @@ public class CommandFactoryImpl implements CommandFactory {
private final Props props;
private final File tempDir;
private final System2 system2;
private final JavaVersion javaVersion;

public CommandFactoryImpl(Props props, File tempDir, System2 system2, JavaVersion javaVersion) {
public CommandFactoryImpl(Props props, File tempDir, System2 system2) {
this.props = props;
this.tempDir = tempDir;
this.system2 = system2;
this.javaVersion = javaVersion;
String javaToolOptions = system2.getenv(ENV_VAR_JAVA_TOOL_OPTIONS);
if (javaToolOptions != null && !javaToolOptions.trim().isEmpty()) {
LoggerFactory.getLogger(CommandFactoryImpl.class)
Expand Down Expand Up @@ -155,7 +153,7 @@ private EsInstallation createEsInstallation() {
public JavaCommand createWebCommand(boolean leader) {
File homeDir = props.nonNullValueAsFile(PATH_HOME.getKey());

WebJvmOptions jvmOptions = new WebJvmOptions(tempDir, javaVersion)
WebJvmOptions jvmOptions = new WebJvmOptions(tempDir)
.addFromMandatoryProperty(props, WEB_JAVA_OPTS.getKey())
.addFromMandatoryProperty(props, WEB_JAVA_ADDITIONAL_OPTS.getKey());
addProxyJvmOptions(jvmOptions);
Expand All @@ -182,7 +180,7 @@ public JavaCommand createWebCommand(boolean leader) {
public JavaCommand createCeCommand() {
File homeDir = props.nonNullValueAsFile(PATH_HOME.getKey());

CeJvmOptions jvmOptions = new CeJvmOptions(tempDir, javaVersion)
CeJvmOptions jvmOptions = new CeJvmOptions(tempDir)
.addFromMandatoryProperty(props, CE_JAVA_OPTS.getKey())
.addFromMandatoryProperty(props, CE_JAVA_ADDITIONAL_OPTS.getKey());
addProxyJvmOptions(jvmOptions);
Expand Down

This file was deleted.

Expand Up @@ -24,26 +24,24 @@
import java.util.Map;

public class WebJvmOptions extends JvmOptions<WebJvmOptions> {
public WebJvmOptions(File tmpDir, JavaVersion javaVersion) {
super(mandatoryOptions(tmpDir, javaVersion));
public WebJvmOptions(File tmpDir) {
super(mandatoryOptions(tmpDir));
}

private static Map<String, String> mandatoryOptions(File tmpDir, JavaVersion javaVersion) {
private static Map<String, String> mandatoryOptions(File tmpDir) {
Map<String, String> res = new LinkedHashMap<>(3);
res.put("-Djava.awt.headless=", "true");
res.put("-Dfile.encoding=", "UTF-8");
res.put("-Djava.io.tmpdir=", tmpDir.getAbsolutePath());
res.put("-XX:-OmitStackTraceInFastThrow", "");

if (javaVersion.isAtLeastJava11()) {
// avoid illegal reflective access operations done by MyBatis
res.put("--add-opens=java.base/java.util=ALL-UNNAMED", "");
// avoid illegal reflective access operations done by MyBatis
res.put("--add-opens=java.base/java.util=ALL-UNNAMED", "");

// avoid illegal reflective access operations done by Tomcat
res.put("--add-opens=java.base/java.lang=ALL-UNNAMED", "");
res.put("--add-opens=java.base/java.io=ALL-UNNAMED", "");
res.put("--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED", "");
}
// avoid illegal reflective access operations done by Tomcat
res.put("--add-opens=java.base/java.lang=ALL-UNNAMED", "");
res.put("--add-opens=java.base/java.io=ALL-UNNAMED", "");
res.put("--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED", "");
return res;
}
}
Expand Up @@ -27,15 +27,12 @@
import org.junit.rules.TemporaryFolder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class CeJvmOptionsTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File tmpDir;
private JavaVersion javaVersion = mock(JavaVersion.class);
private CeJvmOptions underTest;

@Before
Expand All @@ -44,17 +41,8 @@ public void setUp() throws IOException {
}

@Test
public void constructor_sets_mandatory_JVM_options_before_java11() {
when(javaVersion.isAtLeastJava11()).thenReturn(false);
underTest = new CeJvmOptions(tmpDir, javaVersion);
assertThat(underTest.getAll()).containsExactly(
"-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(), "-XX:-OmitStackTraceInFastThrow");
}

@Test
public void constructor_sets_mandatory_JVM_options_for_java11() {
when(javaVersion.isAtLeastJava11()).thenReturn(true);
underTest = new CeJvmOptions(tmpDir, javaVersion);
public void constructor_sets_mandatory_JVM_options() {
underTest = new CeJvmOptions(tmpDir);
assertThat(underTest.getAll()).containsExactly(
"-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(), "-XX:-OmitStackTraceInFastThrow",
"--add-opens=java.base/java.util=ALL-UNNAMED");
Expand Down
Expand Up @@ -53,7 +53,6 @@ public class CommandFactoryImplTest {
public TemporaryFolder temp = new TemporaryFolder();

private System2 system2 = Mockito.mock(System2.class);
private JavaVersion javaVersion = Mockito.mock(JavaVersion.class);
private File homeDir;
private File tempDir;
private File logsDir;
Expand All @@ -77,7 +76,7 @@ public void tearDown() {
public void constructor_logs_no_warning_if_env_variable_JAVA_TOOL_OPTIONS_is_not_set() {
attachMemoryAppenderToLoggerOf(CommandFactoryImpl.class);

new CommandFactoryImpl(new Props(new Properties()), tempDir, system2, javaVersion);
new CommandFactoryImpl(new Props(new Properties()), tempDir, system2);

assertThat(listAppender.getLogs()).isEmpty();
}
Expand All @@ -87,7 +86,7 @@ public void constructor_logs_warning_if_env_variable_JAVA_TOOL_OPTIONS_is_set()
when(system2.getenv("JAVA_TOOL_OPTIONS")).thenReturn("sds");
attachMemoryAppenderToLoggerOf(CommandFactoryImpl.class);

new CommandFactoryImpl(new Props(new Properties()), tempDir, system2, javaVersion);
new CommandFactoryImpl(new Props(new Properties()), tempDir, system2);

assertThat(listAppender.getLogs())
.extracting(ILoggingEvent::getMessage)
Expand All @@ -101,7 +100,7 @@ public void constructor_logs_warning_if_env_variable_ES_JAVA_OPTS_is_set() {
when(system2.getenv("ES_JAVA_OPTS")).thenReturn("xyz");
attachMemoryAppenderToLoggerOf(CommandFactoryImpl.class);

new CommandFactoryImpl(new Props(new Properties()), tempDir, system2, javaVersion);
new CommandFactoryImpl(new Props(new Properties()), tempDir, system2);

assertThat(listAppender.getLogs())
.extracting(ILoggingEvent::getMessage)
Expand Down Expand Up @@ -321,7 +320,7 @@ private CommandFactoryImpl newFactory(Properties userProps, System2 system2) {
ServiceLoaderWrapper serviceLoaderWrapper = mock(ServiceLoaderWrapper.class);
when(serviceLoaderWrapper.load()).thenReturn(ImmutableSet.of());
new ProcessProperties(serviceLoaderWrapper).completeDefaults(props);
return new CommandFactoryImpl(props, tempDir, system2, javaVersion);
return new CommandFactoryImpl(props, tempDir, system2);
}

private <T> void attachMemoryAppenderToLoggerOf(Class<T> loggerClass) {
Expand Down
Expand Up @@ -27,15 +27,12 @@
import org.junit.rules.TemporaryFolder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class WebJvmOptionsTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File tmpDir;
private JavaVersion javaVersion = mock(JavaVersion.class);
private WebJvmOptions underTest;

@Before
Expand All @@ -44,17 +41,8 @@ public void setUp() throws IOException {
}

@Test
public void constructor_sets_mandatory_JVM_options_before_java11() {
when(javaVersion.isAtLeastJava11()).thenReturn(false);
underTest = new WebJvmOptions(tmpDir, javaVersion);
assertThat(underTest.getAll()).containsExactly(
"-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(), "-XX:-OmitStackTraceInFastThrow");
}

@Test
public void constructor_sets_mandatory_JVM_options_for_java11() {
when(javaVersion.isAtLeastJava11()).thenReturn(true);
underTest = new WebJvmOptions(tmpDir, javaVersion);
public void constructor_sets_mandatory_JVM_options() {
underTest = new WebJvmOptions(tmpDir);

assertThat(underTest.getAll()).containsExactly(
"-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(), "-XX:-OmitStackTraceInFastThrow",
Expand Down
Expand Up @@ -176,7 +176,7 @@ public int deliverAll(Set<EmailDeliveryRequest> deliveries) {
}

return (int) deliveries.stream()
.filter(t -> !t.getRecipientEmail().trim().isEmpty())
.filter(t -> !t.getRecipientEmail().isBlank())
.map(t -> {
EmailMessage emailMessage = format(t.getNotification());
if (emailMessage != null) {
Expand Down
Expand Up @@ -225,7 +225,7 @@ private Optional<RuleDefinitionDto> getDbRuleFor(RulesDefinition.Rule ruleDef) {
.filter(Objects::nonNull)
.findFirst();
// may occur in case of plugin downgrade
if (!res.isPresent()) {
if (res.isEmpty()) {
return Optional.ofNullable(dbRulesByDbDeprecatedKey.get(ruleKey));
}
return res;
Expand Down

0 comments on commit 41d7390

Please sign in to comment.