-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTeamscaleMojoBase.java
More file actions
151 lines (132 loc) · 5.06 KB
/
TeamscaleMojoBase.java
File metadata and controls
151 lines (132 loc) · 5.06 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
package com.teamscale.maven;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.io.IOException;
import java.nio.file.Path;
/**
* A base class for all Teamscale related maven Mojos. Offers basic attributes and functionality related to Teamscale
* and Maven.
*/
public abstract class TeamscaleMojoBase extends AbstractMojo {
/**
* The URL of the Teamscale instance to which the recorded coverage will be uploaded.
*/
@Parameter()
public String teamscaleUrl;
/**
* The Teamscale project to which the recorded coverage will be uploaded
*/
@Parameter()
public String projectId;
/**
* The username to use to perform the upload. Must have the "Upload external data" permission for the {@link
* #projectId}. Can also be specified via the Maven property {@code teamscale.username}.
*/
@Parameter(property = "teamscale.username")
public String username;
/**
* Teamscale access token of the {@link #username}. Can also be specified via the Maven property
* {@code teamscale.accessToken}.
*/
@Parameter(property = "teamscale.accessToken")
public String accessToken;
/**
* You can optionally use this property to override the code commit to which the coverage will be uploaded. Format:
* {@code BRANCH:UNIX_EPOCH_TIMESTAMP_IN_MILLISECONDS}
* <p>
* If no commit and revision is manually specified, the plugin will try to determine the currently checked-out Git
* commit. You should specify either commit or revision, not both. If both are specified, a warning is logged and
* the revision takes precedence.
*/
@Parameter(property = "teamscale.commit")
public String commit;
/**
* You can optionally use this property to override the revision to which the coverage will be uploaded. If no
* commit and revision is manually specified, the plugin will try to determine the current git revision. You should
* specify either commit or revision, not both. If both are specified, a warning is logged and the revision takes
* precedence.
*/
@Parameter(property = "teamscale.revision")
public String revision;
/**
* The repository id in your Teamscale project which Teamscale should use to look up the revision, if given. Null or
* empty will lead to a lookup in all repositories in the Teamscale project.
*/
@Parameter(property = "teamscale.repository")
public String repository;
/**
* Whether to skip the execution of this Mojo.
*/
@Parameter(defaultValue = "false")
public boolean skip;
/**
* The running Maven session. Provided automatically by Maven.
*/
@Parameter(defaultValue = "${session}")
public MavenSession session;
/**
* The resolved commit, either provided by the user or determined via the GitCommit class
*/
protected String resolvedCommit;
/**
* The resolved revision, either provided by the user or determined via the GitCommit class
*/
protected String resolvedRevision;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (StringUtils.isNotEmpty(revision) && StringUtils.isNotBlank(commit)) {
getLog().warn("Both revision and commit are set but only one of them is needed. " +
"Teamscale will prefer the revision. If that's not intended, please do not set the revision manually.");
}
}
/**
* Sets the <code>resolvedRevision</code> or <code>resolvedCommit</code>. If not provided, try to determine the
* revision via the GitCommit class.
*
* @see GitCommitUtils
*/
protected void resolveCommitOrRevision() throws MojoFailureException {
if (StringUtils.isNotBlank(revision)) {
resolvedRevision = revision;
return;
}
if (StringUtils.isNotBlank(commit)) {
resolvedCommit = commit;
return;
}
Path basedir = session.getCurrentProject().getBasedir().toPath();
try {
resolvedRevision = GitCommitUtils.getGitHeadRevision(basedir);
} catch (IOException e) {
throw new MojoFailureException("There is no <revision> or <commit> configured in the pom.xml" +
" and it was not possible to determine the current revision in " + basedir + " from Git", e);
}
}
/**
* Retrieves the configuration of a goal execution for the given plugin
*
* @param pluginArtifact The id of the plugin
* @param pluginGoal The name of the goal
* @return The configuration DOM if present, otherwise <code>null</code>
*/
protected Xpp3Dom getExecutionConfigurationDom(MavenProject project, String pluginArtifact, String pluginGoal) {
Plugin plugin = project.getPlugin(pluginArtifact);
if (plugin == null) {
return null;
}
for (PluginExecution pluginExecution : plugin.getExecutions()) {
if (pluginExecution.getGoals().contains(pluginGoal)) {
return (Xpp3Dom) pluginExecution.getConfiguration();
}
}
return null;
}
}