Skip to content

Commit e5483b8

Browse files
Add support for tycho-surefire-plugin instrumentation in Maven builds (#5573)
1 parent 2fb3b6f commit e5483b8

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

dd-java-agent/instrumentation/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenExecutionListener.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.maven.execution.ExecutionEvent;
1212
import org.apache.maven.execution.MavenExecutionResult;
1313
import org.apache.maven.execution.MavenSession;
14+
import org.apache.maven.model.Plugin;
1415
import org.apache.maven.plugin.MojoExecution;
1516
import org.apache.maven.project.MavenProject;
1617
import org.codehaus.plexus.util.xml.Xpp3Dom;
@@ -23,6 +24,7 @@ public class MavenExecutionListener extends AbstractExecutionListener {
2324

2425
private static final String FORK_COUNT_CONFIG = "forkCount";
2526
private static final String SYSTEM_PROPERTY_VARIABLES_CONFIG = "systemPropertyVariables";
27+
private static final String SYSTEM_PROPERTIES_CONFIG = "systemProperties";
2628

2729
private final BuildEventsHandler<MavenSession> buildEventsHandler;
2830

@@ -46,8 +48,7 @@ public void sessionEnded(ExecutionEvent event) {
4648
@Override
4749
public void mojoSkipped(ExecutionEvent event) {
4850
MojoExecution mojoExecution = event.getMojoExecution();
49-
if (MavenUtils.isMavenSurefireTest(mojoExecution)
50-
|| MavenUtils.isMavenFailsafeTest(mojoExecution)) {
51+
if (MavenUtils.isTestExecution(mojoExecution)) {
5152
MavenSession session = event.getSession();
5253
MavenProject project = event.getProject();
5354
String projectName = project.getName();
@@ -63,9 +64,7 @@ public void mojoSkipped(ExecutionEvent event) {
6364
@Override
6465
public void mojoStarted(ExecutionEvent event) {
6566
MojoExecution mojoExecution = event.getMojoExecution();
66-
if (MavenUtils.isMavenSurefireTest(mojoExecution)
67-
|| MavenUtils.isMavenFailsafeTest(mojoExecution)) {
68-
67+
if (MavenUtils.isTestExecution(mojoExecution)) {
6968
MavenSession session = event.getSession();
7069
MavenProject project = event.getProject();
7170
String projectName = project.getName();
@@ -104,23 +103,27 @@ public void mojoStarted(ExecutionEvent event) {
104103
if (forkTestVm) {
105104
configuration =
106105
setForkedVmSystemProperty(
106+
mojoExecution.getPlugin(),
107107
configuration,
108108
Strings.propertyNameToSystemPropertyName(
109109
CiVisibilityConfig.CIVISIBILITY_SESSION_ID),
110110
moduleInfo.sessionId);
111111
configuration =
112112
setForkedVmSystemProperty(
113+
mojoExecution.getPlugin(),
113114
configuration,
114115
Strings.propertyNameToSystemPropertyName(CiVisibilityConfig.CIVISIBILITY_MODULE_ID),
115116
moduleInfo.moduleId);
116117
configuration =
117118
setForkedVmSystemProperty(
119+
mojoExecution.getPlugin(),
118120
configuration,
119121
Strings.propertyNameToSystemPropertyName(
120122
CiVisibilityConfig.CIVISIBILITY_SIGNAL_SERVER_HOST),
121123
moduleInfo.signalServerHost);
122124
configuration =
123125
setForkedVmSystemProperty(
126+
mojoExecution.getPlugin(),
124127
configuration,
125128
Strings.propertyNameToSystemPropertyName(
126129
CiVisibilityConfig.CIVISIBILITY_SIGNAL_SERVER_PORT),
@@ -141,19 +144,19 @@ public void mojoStarted(ExecutionEvent event) {
141144
}
142145

143146
private static Xpp3Dom setForkedVmSystemProperty(
144-
Xpp3Dom configuration, String propertyName, Object propertyValue) {
147+
Plugin plugin, Xpp3Dom configuration, String propertyName, Object propertyValue) {
148+
String configTag =
149+
!"tycho-surefire-plugin".equals(plugin.getArtifactId())
150+
? SYSTEM_PROPERTY_VARIABLES_CONFIG
151+
: SYSTEM_PROPERTIES_CONFIG;
145152
return MavenUtils.setConfigurationValue(
146-
String.valueOf(propertyValue),
147-
configuration,
148-
SYSTEM_PROPERTY_VARIABLES_CONFIG,
149-
propertyName);
153+
String.valueOf(propertyValue), configuration, configTag, propertyName);
150154
}
151155

152156
@Override
153157
public void mojoSucceeded(ExecutionEvent event) {
154158
MojoExecution mojoExecution = event.getMojoExecution();
155-
if (MavenUtils.isMavenSurefireTest(mojoExecution)
156-
|| MavenUtils.isMavenFailsafeTest(mojoExecution)) {
159+
if (MavenUtils.isTestExecution(mojoExecution)) {
157160
MavenSession session = event.getSession();
158161
MavenProject project = event.getProject();
159162

@@ -172,8 +175,7 @@ public void mojoSucceeded(ExecutionEvent event) {
172175
@Override
173176
public void mojoFailed(ExecutionEvent event) {
174177
MojoExecution mojoExecution = event.getMojoExecution();
175-
if (MavenUtils.isMavenSurefireTest(mojoExecution)
176-
|| MavenUtils.isMavenFailsafeTest(mojoExecution)) {
178+
if (MavenUtils.isTestExecution(mojoExecution)) {
177179
MavenSession session = event.getSession();
178180
MavenProject project = event.getProject();
179181

dd-java-agent/instrumentation/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenLifecycleParticipant.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ public class MavenLifecycleParticipant extends AbstractMavenLifecycleParticipant
4747

4848
private static final Logger LOGGER = LoggerFactory.getLogger(MavenLifecycleParticipant.class);
4949

50-
private static final String MAVEN_SUREFIRE_PLUGIN_KEY =
51-
"org.apache.maven.plugins:maven-surefire-plugin";
52-
private static final String MAVEN_FAILSAFE_PLUGIN_KEY =
53-
"org.apache.maven.plugins:maven-failsafe-plugin";
54-
5550
private final BuildEventsHandler<MavenSession> buildEventsHandler =
5651
InstrumentationBridge.createBuildEventsHandler();
5752

@@ -200,9 +195,7 @@ private Map<Path, Collection<TestsExecution>> getTestExecutionsByJvmPath(
200195
Plugin plugin = mojoExecution.getPlugin();
201196
String pluginKey = plugin.getKey();
202197

203-
if (MAVEN_SUREFIRE_PLUGIN_KEY.equals(pluginKey)
204-
|| MAVEN_FAILSAFE_PLUGIN_KEY.equals(pluginKey)) {
205-
198+
if (MavenUtils.isTestExecution(mojoExecution)) {
206199
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
207200
PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
208201
// ensure plugin realm is loaded in container

dd-java-agent/instrumentation/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenUtils.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,20 @@ public static Throwable getException(MavenExecutionResult result) {
214214
}
215215
}
216216

217-
public static boolean isMavenSurefireTest(MojoExecution mojoExecution) {
217+
public static boolean isTestExecution(MojoExecution mojoExecution) {
218218
Plugin plugin = mojoExecution.getPlugin();
219-
return "maven-surefire-plugin".equals(plugin.getArtifactId())
220-
&& "org.apache.maven.plugins".equals(plugin.getGroupId())
221-
&& "test".equals(mojoExecution.getGoal());
222-
}
223-
224-
public static boolean isMavenFailsafeTest(MojoExecution mojoExecution) {
225-
Plugin plugin = mojoExecution.getPlugin();
226-
return "maven-failsafe-plugin".equals(plugin.getArtifactId())
227-
&& "org.apache.maven.plugins".equals(plugin.getGroupId())
228-
&& "integration-test".equals(mojoExecution.getGoal());
219+
String artifactId = plugin.getArtifactId();
220+
String groupId = plugin.getGroupId();
221+
String goal = mojoExecution.getGoal();
222+
return "maven-surefire-plugin".equals(artifactId)
223+
&& "org.apache.maven.plugins".equals(groupId)
224+
&& "test".equals(goal)
225+
|| "maven-failsafe-plugin".equals(artifactId)
226+
&& "org.apache.maven.plugins".equals(groupId)
227+
&& "integration-test".equals(goal)
228+
|| "tycho-surefire-plugin".equals(artifactId)
229+
&& "org.eclipse.tycho".equals(groupId)
230+
&& ("test".equals(goal) || "plugin-test".equals(goal) || "bnd-test".equals(goal));
229231
}
230232

231233
public static String getConfigurationValue(Xpp3Dom configuration, String... path) {

0 commit comments

Comments
 (0)