-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathSonarMaven.java
More file actions
185 lines (170 loc) · 7.29 KB
/
SonarMaven.java
File metadata and controls
185 lines (170 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* SonarQube Scanner for Jenkins
* Copyright (C) 2007-2025 SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package hudson.plugins.sonar.utils;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.maven.local_repo.DefaultLocalRepositoryLocator;
import hudson.maven.local_repo.LocalRepositoryLocator;
import hudson.maven.local_repo.PerJobLocalRepositoryLocator;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Computer;
import hudson.model.JDK;
import hudson.plugins.sonar.SonarInstallation;
import hudson.plugins.sonar.SonarPublisher;
import hudson.plugins.sonar.client.HttpClient;
import hudson.plugins.sonar.client.OkHttpClientSingleton;
import hudson.tasks.Maven;
import hudson.util.ArgumentListBuilder;
import java.io.IOException;
import jenkins.model.Jenkins;
import jenkins.mvn.GlobalSettingsProvider;
import jenkins.mvn.SettingsProvider;
import org.apache.commons.lang3.StringUtils;
/**
* @author Evgeny Mandrikov
* @since 1.3
*/
public final class SonarMaven extends Maven {
/**
* Produce execution error messages and run in non-interactive (batch) mode.
*/
private static final String TARGET = "-e -B";
private final SonarPublisher publisher;
private final String additionalProperties;
private JDK jdk;
private final BuildListener listener;
private final LocalRepositoryLocator locaRepository;
public SonarMaven(String additionalProperties, String name, String pom, String jvmOptions, LocalRepositoryLocator locaRepository,
SonarPublisher publisher, BuildListener listener, JDK jdk, SettingsProvider settings, GlobalSettingsProvider globalSettings) {
super(getTarget(publisher.getInstallation()), name, pom, "", jvmOptions, false,
settings, globalSettings);
this.additionalProperties = additionalProperties;
this.locaRepository = locaRepository;
this.publisher = publisher;
this.jdk = jdk;
this.listener = listener;
}
/**
* Visibility of a method has been relaxed for tests.
*/
static String getTarget(SonarInstallation installation) {
String mojoVersion = installation.getMojoVersion();
if (StringUtils.isBlank(mojoVersion)) {
return TARGET + " sonar:sonar";
} else {
return TARGET + " " + SonarUtils.getMavenGoal(mojoVersion);
}
}
private SonarInstallation getInstallation() {
return publisher.getInstallation();
}
@Override
protected void wrapUpArguments(ArgumentListBuilder args, String normalizedTarget, AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws IOException, InterruptedException {
args.addTokenized(additionalProperties);
ExtendedArgumentListBuilder argsBuilder = new ExtendedArgumentListBuilder(args, launcher.isUnix());
argsBuilder.append("sonar.host.url", getInstallation().getServerUrl());
argsBuilder.append("sonar.branch", publisher.getBranch());
String token = getInstallation().getServerAuthenticationToken(build);
if (StringUtils.isNotBlank(token)) {
argsBuilder.appendMasked(SonarUtils.getTokenProperty(getInstallation(), new HttpClient(OkHttpClientSingleton.getInstance())), token);
}
if (build instanceof MavenModuleSetBuild) {
FilePath localRepo = locaRepository.locate((MavenModuleSetBuild) build);
if (localRepo != null) {
args.add("-Dmaven.repo.local=" + localRepo.getRemote());
}
} else if (locaRepository instanceof PerJobLocalRepositoryLocator) {
FilePath workspace = build.getWorkspace();
if (workspace != null) {
args.add("-Dmaven.repo.local=" + workspace.child(".repository"));
}
}
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) Jenkins.get().getDescriptorOrDie(Maven.class);
}
public static boolean executeMaven(
AbstractBuild<?, ?> build,
Launcher launcher,
BuildListener listener,
String mavenName,
String pom,
SonarInstallation sonarInstallation,
SonarPublisher sonarPublisher,
JDK jdk,
SettingsProvider settings,
GlobalSettingsProvider globalSettings,
boolean usesLocalRepository) throws IOException, InterruptedException {
MavenModuleSet mavenModuleProject = sonarPublisher.getMavenProject(build);
EnvVars envVars = build.getEnvironment(listener);
/**
* MAVEN_OPTS
*/
String mvnOptions = sonarPublisher.getMavenOpts();
if (StringUtils.isEmpty(mvnOptions)
&& mavenModuleProject != null
&& StringUtils.isNotEmpty(mavenModuleProject.getMavenOpts())) {
mvnOptions = mavenModuleProject.getMavenOpts();
}
// Private Repository and Alternate Settings
LocalRepositoryLocator locaRepositoryToUse = usesLocalRepository ? new PerJobLocalRepositoryLocator() : new DefaultLocalRepositoryLocator();
SettingsProvider settingsToUse = settings;
GlobalSettingsProvider globalSettingsToUse = globalSettings;
if (mavenModuleProject != null) {
// If we are on a Maven job then take values from the job itself
locaRepositoryToUse = mavenModuleProject.getLocalRepository();
settingsToUse = mavenModuleProject.getSettings();
globalSettingsToUse = mavenModuleProject.getGlobalSettings();
}
// Other properties
String additionalArguments = sonarInstallation.getAdditionalProperties();
String analysisProperties = StringUtils.join(sonarInstallation.getAdditionalAnalysisPropertiesUnix(), ' ');
String jobProperties = envVars.expand(sonarPublisher.getJobAdditionalProperties());
String additionalProperties = ""
+ (StringUtils.isNotBlank(additionalArguments) ? additionalArguments : "") + " "
+ (StringUtils.isNotBlank(analysisProperties) ? analysisProperties : "") + " "
+ (StringUtils.isNotBlank(jobProperties) ? jobProperties : "");
// Execute Maven
// SONARPLUGINS-487
String pomPath = build.getModuleRoot().child(pom).getRemote();
return new SonarMaven(additionalProperties, mavenName, pomPath, mvnOptions, locaRepositoryToUse, sonarPublisher, listener, jdk,
settingsToUse, globalSettingsToUse)
.perform(build, launcher, listener);
}
@Override
protected void buildEnvVars(EnvVars env, MavenInstallation mi) throws IOException, InterruptedException {
super.buildEnvVars(env, mi);
// Override JDK in case it is set on Sonar publisher
if (jdk != null) {
Computer computer = Computer.currentComputer();
if (computer != null) {
// just in case were not in a build
jdk = jdk.forNode(computer.getNode(), listener);
}
jdk.buildEnvVars(env);
}
}
}