20
20
package org .sonarsource .scanner .maven .bootstrap ;
21
21
22
22
import com .google .common .annotations .VisibleForTesting ;
23
+
23
24
import java .io .IOException ;
24
25
import java .nio .file .Files ;
25
26
import java .nio .file .Path ;
26
27
import java .nio .file .Paths ;
27
28
import java .util .ArrayList ;
28
29
import java .util .List ;
29
30
import java .util .Map ;
31
+ import java .util .Objects ;
30
32
import java .util .Properties ;
31
33
import java .util .Set ;
32
34
import java .util .stream .Collectors ;
35
+
33
36
import org .apache .maven .artifact .versioning .ComparableVersion ;
34
37
import org .apache .maven .execution .MavenSession ;
35
38
import org .apache .maven .plugin .MojoExecutionException ;
36
39
import org .apache .maven .plugin .logging .Log ;
37
40
import org .apache .maven .project .MavenProject ;
38
41
import org .sonarsource .scanner .api .EmbeddedScanner ;
39
42
import org .sonarsource .scanner .api .ScanProperties ;
43
+ import org .sonarsource .scanner .api .ScannerProperties ;
44
+
45
+ import static org .sonarsource .scanner .maven .bootstrap .MavenProjectConverter .getPropertyByKey ;
40
46
41
47
/**
42
48
* Configure properties and bootstrap using SonarQube scanner API
43
49
*/
44
50
public class ScannerBootstrapper {
45
51
46
52
static final String UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE = "With SonarQube server prior to 5.6, use sonar-maven-plugin <= 3.3" ;
53
+ private static final String SONARCLOUD_HOST_URL = "https://sonarcloud.io" ;
47
54
48
55
private final Log log ;
49
56
private final MavenSession session ;
@@ -65,7 +72,15 @@ public void execute() throws MojoExecutionException {
65
72
scanner .start ();
66
73
serverVersion = scanner .serverVersion ();
67
74
68
- checkSQVersion ();
75
+ if (isSonarCloudUsed ()) {
76
+ log .info ("Communicating with SonarCloud" );
77
+ } else {
78
+ if (serverVersion != null ) {
79
+ log .info ("Communicating with SonarQube Server " + serverVersion );
80
+ }
81
+ checkSQVersion ();
82
+ }
83
+
69
84
70
85
if (log .isDebugEnabled ()) {
71
86
scanner .setGlobalProperty ("sonar.verbose" , "true" );
@@ -77,6 +92,20 @@ public void execute() throws MojoExecutionException {
77
92
}
78
93
}
79
94
95
+
96
+ // TODO remove this workaround when discovering if the sevrer is SC or SQ is available through the API
97
+ private boolean isSonarCloudUsed () {
98
+ return session .getProjects ().stream ()
99
+ // We can use EnvProperties from MavenProjectConverter as they are initialized at construction time,
100
+ // but we can't use UserProperties from the MavenProjectConverter as they are only initialized
101
+ // in the "collectProperties" method.
102
+ .map (project ->
103
+ getPropertyByKey (ScannerProperties .HOST_URL , project , session .getUserProperties (), mavenProjectConverter .getEnvProperties ())
104
+ )
105
+ .filter (Objects ::nonNull )
106
+ .anyMatch (hostUrl -> hostUrl .startsWith (SONARCLOUD_HOST_URL ));
107
+ }
108
+
80
109
@ VisibleForTesting
81
110
Map <String , String > collectProperties ()
82
111
throws MojoExecutionException {
@@ -143,10 +172,6 @@ private void collectAllSources(Map<String, String> props) {
143
172
}
144
173
145
174
private void checkSQVersion () {
146
- if (serverVersion != null ) {
147
- log .info ("SonarQube version: " + serverVersion );
148
- }
149
-
150
175
if (isVersionPriorTo ("5.6" )) {
151
176
throw new UnsupportedOperationException (UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE );
152
177
}
0 commit comments