-
Notifications
You must be signed in to change notification settings - Fork 118
/
SonarQubeMojo.java
164 lines (138 loc) · 5.94 KB
/
SonarQubeMojo.java
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
/*
* SonarQube Scanner for Maven
* Copyright (C) 2009-2019 SonarSource SA
* 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 org.sonarsource.scanner.maven;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.LifecycleExecutor;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.sonarsource.scanner.api.EmbeddedScanner;
import org.sonarsource.scanner.api.ScanProperties;
import org.sonarsource.scanner.api.Utils;
import org.sonarsource.scanner.maven.bootstrap.JavaVersionResolver;
import org.sonarsource.scanner.maven.bootstrap.LogHandler;
import org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter;
import org.sonarsource.scanner.maven.bootstrap.PropertyDecryptor;
import org.sonarsource.scanner.maven.bootstrap.ScannerBootstrapper;
import org.sonarsource.scanner.maven.bootstrap.ScannerFactory;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
/**
* Analyze project. SonarQube server must be started.
*/
@Mojo(name = "sonar", requiresDependencyResolution = ResolutionScope.TEST, aggregator = true)
public class SonarQubeMojo extends AbstractMojo {
@Parameter(defaultValue = "${session}", required = true, readonly = true)
private MavenSession session;
/**
* Set this to 'true' to skip analysis.
*
* @since 2.3
*/
@Parameter(alias = "sonar.skip", property = "sonar.skip", defaultValue = "false")
private boolean skip;
@Component
private LifecycleExecutor lifecycleExecutor;
@Component(hint = "mng-4384")
private SecDispatcher securityDispatcher;
@Component
private RuntimeInformation runtimeInformation;
@Parameter(defaultValue = "${mojoExecution}", required = true, readonly = true)
private MojoExecution mojoExecution;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (getLog().isDebugEnabled()) {
setLog(new TimestampLogger(getLog()));
}
if (shouldDelayExecution()) {
getLog().info("Delaying SonarQube Scanner to the end of multi-module project");
return;
}
Properties envProps = Utils.loadEnvironmentProperties(System.getenv());
JavaVersionResolver pluginParameterResolver = new JavaVersionResolver(session, lifecycleExecutor, getLog());
MavenProjectConverter mavenProjectConverter = new MavenProjectConverter(getLog(), pluginParameterResolver, envProps);
LogHandler logHandler = new LogHandler(getLog());
PropertyDecryptor propertyDecryptor = new PropertyDecryptor(getLog(), securityDispatcher);
ScannerFactory runnerFactory = new ScannerFactory(logHandler, getLog(), runtimeInformation, mojoExecution, session, envProps, propertyDecryptor);
if (isSkip(runnerFactory.createGlobalProperties())) {
return;
}
EmbeddedScanner runner = runnerFactory.create();
new ScannerBootstrapper(getLog(), session, runner, mavenProjectConverter, propertyDecryptor).execute();
}
/**
* Should scanner be delayed?
* @return true if goal is attached to phase and not last in a multi-module project
*/
private boolean shouldDelayExecution() {
return !isDetachedGoal() && !isLastProjectInReactor();
}
/**
* Is this execution a 'detached' goal run from the cli. e.g. mvn sonar:sonar
*
* See <a href="https://maven.apache.org/guides/mini/guide-default-execution-ids.html#Default_executionIds_for_Implied_Executions">
Default executionIds for Implied Executions</a>
* for explanation of command line execution id.
*
* @return true if this execution is from the command line
*/
private boolean isDetachedGoal() {
return "default-cli".equals(mojoExecution.getExecutionId());
}
/**
* Is this project the last project in the reactor?
*
* @return true if last project (including only project)
*/
private boolean isLastProjectInReactor() {
List<MavenProject> sortedProjects = session.getProjectDependencyGraph().getSortedProjects();
MavenProject lastProject = sortedProjects.isEmpty()
? session.getCurrentProject()
: sortedProjects.get( sortedProjects.size() - 1 );
if ( getLog().isDebugEnabled() ) {
getLog().debug( "Current project: '" + session.getCurrentProject().getName() +
"', Last project to execute based on dependency graph: '" + lastProject.getName() + "'" );
}
return session.getCurrentProject().equals( lastProject );
}
private boolean isSkip(Map<String, String> properties) {
if (skip) {
getLog().info("sonar.skip = true: Skipping analysis");
return true;
}
if ("true".equalsIgnoreCase(properties.get(ScanProperties.SKIP))) {
getLog().info("SonarQube Scanner analysis skipped");
return true;
}
return false;
}
MavenSession getSession() {
return session;
}
}