]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14676 Shade core jars into a single jar
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 6 Apr 2021 21:22:45 +0000 (16:22 -0500)
committersonartech <sonartech@sonarsource.com>
Wed, 14 Apr 2021 20:03:29 +0000 (20:03 +0000)
server/sonar-main/src/main/java/org/sonar/application/command/CommandFactoryImpl.java
server/sonar-main/src/test/java/org/sonar/application/command/CommandFactoryImplTest.java
sonar-application/build.gradle
sonar-application/src/main/assembly/conf/wrapper.conf

index 3475bb689fc99832266ea1dabc844f36965999b3..9bd07d512e6a350dccbc318879f5cbc4b7fc6189 100644 (file)
@@ -23,6 +23,8 @@ import java.io.File;
 import java.util.Map;
 import java.util.Optional;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.internal.MetadataLoader;
+import org.sonar.api.utils.Version;
 import org.sonar.application.es.EsInstallation;
 import org.sonar.application.es.EsLogging;
 import org.sonar.application.es.EsSettings;
@@ -32,8 +34,6 @@ import org.sonar.process.ProcessProperties;
 import org.sonar.process.Props;
 import org.sonar.process.System2;
 
-import static org.sonar.process.ProcessProperties.Property.WEB_GRACEFUL_STOP_TIMEOUT;
-import static org.sonar.process.ProcessProperties.parseTimeoutMs;
 import static org.sonar.process.ProcessProperties.Property.CE_GRACEFUL_STOP_TIMEOUT;
 import static org.sonar.process.ProcessProperties.Property.CE_JAVA_ADDITIONAL_OPTS;
 import static org.sonar.process.ProcessProperties.Property.CE_JAVA_OPTS;
@@ -50,8 +50,10 @@ import static org.sonar.process.ProcessProperties.Property.SEARCH_JAVA_ADDITIONA
 import static org.sonar.process.ProcessProperties.Property.SEARCH_JAVA_OPTS;
 import static org.sonar.process.ProcessProperties.Property.SOCKS_PROXY_HOST;
 import static org.sonar.process.ProcessProperties.Property.SOCKS_PROXY_PORT;
+import static org.sonar.process.ProcessProperties.Property.WEB_GRACEFUL_STOP_TIMEOUT;
 import static org.sonar.process.ProcessProperties.Property.WEB_JAVA_ADDITIONAL_OPTS;
 import static org.sonar.process.ProcessProperties.Property.WEB_JAVA_OPTS;
+import static org.sonar.process.ProcessProperties.parseTimeoutMs;
 
 public class CommandFactoryImpl implements CommandFactory {
   private static final String ENV_VAR_JAVA_TOOL_OPTIONS = "JAVA_TOOL_OPTIONS";
@@ -69,6 +71,7 @@ public class CommandFactoryImpl implements CommandFactory {
     SOCKS_PROXY_HOST.getKey(),
     SOCKS_PROXY_PORT.getKey()};
 
+  private static final Version SQ_VERSION = MetadataLoader.loadVersion(org.sonar.api.utils.System2.INSTANCE);
   private final Props props;
   private final File tempDir;
   private final System2 system2;
@@ -166,7 +169,7 @@ public class CommandFactoryImpl implements CommandFactory {
       .setEnvVariable(PATH_LOGS.getKey(), props.nonNullValue(PATH_LOGS.getKey()))
       .setArgument("sonar.cluster.web.startupLeader", Boolean.toString(leader))
       .setClassName("org.sonar.server.app.WebServer")
-      .addClasspath("./lib/common/*");
+      .addClasspath("./lib/sonar-application-" + SQ_VERSION + ".jar");
     String driverPath = props.value(JDBC_DRIVER_PATH.getKey());
     if (driverPath != null) {
       command.addClasspath(driverPath);
@@ -190,7 +193,7 @@ public class CommandFactoryImpl implements CommandFactory {
       .setJvmOptions(jvmOptions)
       .setGracefulStopTimeoutMs(getGracefulStopTimeoutMs(props, CE_GRACEFUL_STOP_TIMEOUT))
       .setClassName("org.sonar.ce.app.CeServer")
-      .addClasspath("./lib/common/*");
+      .addClasspath("./lib/sonar-application-" + SQ_VERSION + ".jar");
     String driverPath = props.value(JDBC_DRIVER_PATH.getKey());
     if (driverPath != null) {
       command.addClasspath(driverPath);
index 45cdcf1da8616faa8cd5ce4c60e072e7ac187410..f8d7029008a71e77f55accaa67eab0d000ca4713 100644 (file)
@@ -224,8 +224,7 @@ public class CommandFactoryImplTest {
 
     assertThat(command.getClassName()).isEqualTo("org.sonar.server.app.WebServer");
     assertThat(command.getWorkDir().getAbsolutePath()).isEqualTo(homeDir.getAbsolutePath());
-    assertThat(command.getClasspath())
-      .containsExactly("./lib/common/*");
+    assertThat(command.getClasspath()).hasSize(1).allMatch(p -> p.toString().startsWith("./lib/sonar-application-"));
     assertThat(command.getJvmOptions().getAll())
       // enforced values
       .contains("-Djava.awt.headless=true", "-Dfile.encoding=UTF-8")
@@ -249,8 +248,7 @@ public class CommandFactoryImplTest {
 
     assertThat(command.getClassName()).isEqualTo("org.sonar.ce.app.CeServer");
     assertThat(command.getWorkDir().getAbsolutePath()).isEqualTo(homeDir.getAbsolutePath());
-    assertThat(command.getClasspath())
-      .containsExactly("./lib/common/*");
+    assertThat(command.getClasspath()).hasSize(1).allMatch(p -> p.toString().startsWith("./lib/sonar-application-"));
     assertThat(command.getJvmOptions().getAll())
       // enforced values
       .contains("-Djava.awt.headless=true", "-Dfile.encoding=UTF-8")
@@ -298,9 +296,9 @@ public class CommandFactoryImplTest {
     props.setProperty("sonar.jdbc.driverPath", driverFile.getAbsolutePath());
 
     JavaCommand command = newFactory(props).createWebCommand(true);
-
-    assertThat(command.getClasspath())
-      .containsExactlyInAnyOrder("./lib/common/*", driverFile.getAbsolutePath());
+    assertThat(command.getClasspath()).hasSize(2);
+    assertThat(command.getClasspath().get(0).toString()).startsWith("./lib/sonar-application-");
+    assertThat(command.getClasspath().get(1)).isEqualTo(driverFile.getAbsolutePath());
   }
 
   private void prepareEsFileSystem() throws IOException {
index b1fe29e3c65d53bdf8920e3afc8c69700eadd36c..31eaad203ff47e039847bb8299312e3c928bcdad 100644 (file)
@@ -1,5 +1,7 @@
 import org.apache.tools.ant.filters.ReplaceTokens
 
+apply plugin: 'com.github.johnrengelman.shadow'
+
 sonarqube {
   properties {
     property 'sonar.projectName', "${projectTitle} :: Application"
@@ -26,6 +28,16 @@ configurations {
   }
 }
 
+jar.enabled = false
+shadowJar {
+  baseName = 'sonar-application'
+  classifier = null
+  mergeServiceFiles()
+  manifest {
+    attributes('Main-Class': 'org.sonar.application.App')
+  }
+}
+
 dependencies {
     // please keep list ordered
     compile 'org.slf4j:slf4j-api'
@@ -54,15 +66,6 @@ dependencies {
 // declare dependencies in configuration bundledPlugin to be packaged in lib/extensions
 apply from: 'bundled_plugins.gradle'
 
-jar {
-  manifest {
-    attributes(
-      'Class-Path': configurations.compile.resolvedConfiguration.files.collect { "common/${it.getName()}" }.join(' '),
-      'Main-Class': 'org.sonar.application.App'
-    )
-  }
-}
-
 task zip(type: Zip, dependsOn: [configurations.compile]) {
   duplicatesStrategy DuplicatesStrategy.EXCLUDE
   def archiveDir = "sonarqube-$version"
@@ -70,6 +73,7 @@ task zip(type: Zip, dependsOn: [configurations.compile]) {
   into("${archiveDir}/") {
     from file('src/main/assembly')
       exclude 'conf/sonar.properties'
+      exclude 'conf/wrapper.conf'
       exclude 'elasticsearch-patch'
       // elasticsearch script will be replaced by patched version below
       exclude 'elasticsearch/bin/elasticsearch'
@@ -134,16 +138,21 @@ task zip(type: Zip, dependsOn: [configurations.compile]) {
 
   into("${archiveDir}/conf/") {
     from file('src/main/assembly/conf/sonar.properties')
-      filter(ReplaceTokens, tokens: [
-              'searchDefaultHeapSize': '512MB',
-              'searchJavaOpts'       : '-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError',
-              'ceDefaultHeapSize'    : '512MB',
-              'ceJavaOpts'           : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError',
-              'webDefaultHeapSize'   : '512MB',
-              'webJavaOpts'          : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError'
-      ])
-  }
-    
+    filter(ReplaceTokens, tokens: [
+            'searchDefaultHeapSize': '512MB',
+            'searchJavaOpts'       : '-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError',
+            'ceDefaultHeapSize'    : '512MB',
+            'ceJavaOpts'           : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError',
+            'webDefaultHeapSize'   : '512MB',
+            'webJavaOpts'          : '-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError'
+    ])
+  }
+  into("${archiveDir}/conf/") {
+    from file('src/main/assembly/conf/wrapper.conf')
+    filter(ReplaceTokens, tokens: [
+            'sqversion': version
+    ])
+  }
   
   into("${archiveDir}/elasticsearch/") {
     from file('src/main/assembly/elasticsearch-patch')
@@ -153,9 +162,6 @@ task zip(type: Zip, dependsOn: [configurations.compile]) {
   into("${archiveDir}/elasticsearch/") {
     from "$buildDir/elasticsearch"
   }
-  into("${archiveDir}/lib/") {
-    from jar
-  }
   into("${archiveDir}/lib/extensions/") {
     from configurations.bundledPlugin
   }
@@ -165,8 +171,8 @@ task zip(type: Zip, dependsOn: [configurations.compile]) {
   into("${archiveDir}/lib/scanner/") {
     from configurations.scanner
   }
-  into("${archiveDir}/lib/common/") {
-    from configurations.compile
+  into("${archiveDir}/lib/") {
+    from shadowJar
   }
   into("${archiveDir}/web/") {
     duplicatesStrategy DuplicatesStrategy.FAIL
index d4cdf7e3776e36daa4a048aa077186f0a0386292..360026d398d589f73f3c09ec9384e33eb7cc6aa3 100644 (file)
@@ -15,9 +15,9 @@ wrapper.java.command=java
 wrapper.java.additional.1=-Dsonar.wrapped=true
 wrapper.java.additional.2=-Djava.awt.headless=true
 wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
-wrapper.java.classpath.1=../../lib/jsw/*.jar
-wrapper.java.classpath.2=../../lib/common/*.jar
-wrapper.java.classpath.3=../../lib/*.jar
+wrapper.java.classpath.1=../../lib/sonar-application-@sqversion@.jar
+wrapper.java.classpath.2=../../lib/jsw/wrapper-3.2.3.jar
+wrapper.java.classpath.3=../../lib/sonar-shutdowner-@sqversion@.jar
 wrapper.java.library.path.1=./lib
 wrapper.app.parameter.1=org.sonar.application.App
 wrapper.java.initmemory=8