Skip to content

Commit 80681b5

Browse files
authored
Log executed Allure CLI commands in Maven debug output (fixes #222, via #424)
1 parent 25f5d05 commit 80681b5

7 files changed

Lines changed: 372 additions & 19 deletions

File tree

src/main/java/io/qameta/allure/maven/Allure3Commandline.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.commons.exec.ExecuteWatchdog;
2929
import org.apache.commons.io.FileUtils;
3030
import org.apache.commons.lang3.StringUtils;
31+
import org.apache.maven.plugin.logging.Log;
3132
import org.apache.maven.settings.Proxy;
3233

3334
import java.io.BufferedReader;
@@ -57,7 +58,8 @@
5758
*/
5859
@SuppressWarnings({"PMD.GodClass", "ClassDataAbstractionCoupling", "ClassFanOutComplexity",
5960
"MultipleStringLiterals", "ParameterNumber", "PMD.CouplingBetweenObjects",
60-
"PMD.TooManyMethods", "PMD.CyclomaticComplexity", "PMD.ExcessiveParameterList"})
61+
"PMD.TooManyMethods", "PMD.CyclomaticComplexity", "PMD.ExcessiveParameterList",
62+
"PMD.NcssCount"})
6163
public class Allure3Commandline {
6264

6365
public static final String NODE_DEFAULT_VERSION = "24.14.1";
@@ -93,10 +95,20 @@ public class Allure3Commandline {
9395

9496
private final Allure3Platform platform;
9597

98+
private final Log log;
99+
96100
public Allure3Commandline(final Path installationDirectory, final String allureVersion,
97101
final String nodeVersion, final String nodeDownloadUrl, final String npmRegistry,
98102
final Path allurePackagePath, final Proxy proxy, final Properties downloadProperties,
99103
final boolean offline, final int timeout) {
104+
this(installationDirectory, allureVersion, nodeVersion, nodeDownloadUrl, npmRegistry,
105+
allurePackagePath, proxy, downloadProperties, offline, timeout, null);
106+
}
107+
108+
public Allure3Commandline(final Path installationDirectory, final String allureVersion,
109+
final String nodeVersion, final String nodeDownloadUrl, final String npmRegistry,
110+
final Path allurePackagePath, final Proxy proxy, final Properties downloadProperties,
111+
final boolean offline, final int timeout, final Log log) {
100112
this.installationDirectory = installationDirectory;
101113
this.allureVersion = allureVersion;
102114
this.nodeVersion = StringUtils.defaultIfBlank(nodeVersion, NODE_DEFAULT_VERSION);
@@ -109,6 +121,7 @@ public Allure3Commandline(final Path installationDirectory, final String allureV
109121
this.offline = offline;
110122
this.timeout = timeout;
111123
this.platform = Allure3Platform.detect();
124+
this.log = log;
112125
}
113126

114127
public void install() throws IOException {
@@ -584,13 +597,20 @@ private int execute(final CommandLine commandLine, final int timeout) throws IOE
584597
.setTimeout(Duration.ofMillis(TimeUnit.SECONDS.toMillis(timeout))).get();
585598
executor.setWatchdog(watchdog);
586599
executor.setExitValue(0);
600+
logCommandLine(commandLine);
587601
return executor.execute(commandLine);
588602
}
589603

590604
private void addPathArgument(final CommandLine commandLine, final Path path) {
591605
commandLine.addArgument(path.toAbsolutePath().toString(), platform.isWindows());
592606
}
593607

608+
private void logCommandLine(final CommandLine commandLine) {
609+
if (log != null && log.isDebugEnabled()) {
610+
log.debug("Executing Allure command: " + Arrays.toString(commandLine.toStrings()));
611+
}
612+
}
613+
594614
private static final class MapTypeReference extends TypeReference<Map<String, Object>> {
595615
}
596616
}

src/main/java/io/qameta/allure/maven/AllureCommandline.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.commons.lang3.StringUtils;
2525
import org.apache.maven.execution.MavenSession;
2626
import org.apache.maven.model.Dependency;
27+
import org.apache.maven.plugin.logging.Log;
2728
import org.apache.maven.project.DefaultProjectBuildingRequest;
2829
import org.apache.maven.project.ProjectBuildingRequest;
2930
import org.apache.maven.settings.Proxy;
@@ -38,6 +39,7 @@
3839
import java.nio.file.Files;
3940
import java.nio.file.Path;
4041
import java.time.Duration;
42+
import java.util.Arrays;
4143
import java.util.Collections;
4244
import java.util.Iterator;
4345
import java.util.List;
@@ -47,7 +49,7 @@
4749

4850
import static io.qameta.allure.maven.VersionUtils.versionCompare;
4951

50-
@SuppressWarnings({"ClassDataAbstractionCoupling", "ClassFanOutComplexity",
52+
@SuppressWarnings({"PMD.GodClass", "ClassDataAbstractionCoupling", "ClassFanOutComplexity",
5153
"MultipleStringLiterals"})
5254
public class AllureCommandline {
5355

@@ -61,17 +63,30 @@ public class AllureCommandline {
6163

6264
private final Path installationDirectory;
6365

66+
private final Log log;
67+
6468
public AllureCommandline(final Path installationDirectory, final String version) {
65-
this(installationDirectory, version, DEFAULT_TIMEOUT);
69+
this(installationDirectory, version, DEFAULT_TIMEOUT, null);
70+
}
71+
72+
public AllureCommandline(final Path installationDirectory, final String version,
73+
final Log log) {
74+
this(installationDirectory, version, DEFAULT_TIMEOUT, log);
6675
}
6776

6877
public AllureCommandline(final Path installationDirectory, final String version,
6978
final int timeout) {
79+
this(installationDirectory, version, timeout, null);
80+
}
81+
82+
public AllureCommandline(final Path installationDirectory, final String version,
83+
final int timeout, final Log log) {
7084
this.installationDirectory = installationDirectory;
7185
this.version = StringUtils.isBlank(version) || versionCompare(version, "2.8.0") < 0
7286
? ALLURE_DEFAULT_VERSION
7387
: version;
7488
this.timeout = timeout;
89+
this.log = log;
7590
}
7691

7792
public int generateReport(final List<Path> resultsPaths, final Path reportPath,
@@ -81,9 +96,7 @@ public int generateReport(final List<Path> resultsPaths, final Path reportPath,
8196

8297
FileUtils.deleteQuietly(reportPath.toFile());
8398

84-
final CommandLine commandLine =
85-
new CommandLine(getAllureExecutablePath().toAbsolutePath().toFile());
86-
commandLine.addArgument("generate");
99+
final CommandLine commandLine = createCommandLine("generate");
87100
commandLine.addArgument("--clean");
88101
if (singleFile) {
89102
commandLine.addArgument("--single-file");
@@ -103,9 +116,7 @@ public int serve(final List<Path> resultsPaths, final String serveHost, final In
103116

104117
this.checkAllureExists();
105118

106-
final CommandLine commandLine =
107-
new CommandLine(getAllureExecutablePath().toAbsolutePath().toFile());
108-
commandLine.addArgument("serve");
119+
final CommandLine commandLine = createCommandLine("serve");
109120
if (serveHost != null && serveHost.matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
110121
commandLine.addArgument("--host");
111122
commandLine.addArgument(serveHost);
@@ -133,9 +144,26 @@ private int execute(final CommandLine commandLine, final int timeout) throws IOE
133144
.setTimeout(Duration.ofMillis(TimeUnit.SECONDS.toMillis(timeout))).get();
134145
executor.setWatchdog(watchdog);
135146
executor.setExitValue(0);
147+
logCommandLine(commandLine);
136148
return executor.execute(commandLine);
137149
}
138150

151+
private CommandLine createCommandLine(final String command) {
152+
final CommandLine commandLine =
153+
new CommandLine(getAllureExecutablePath().toAbsolutePath().toFile());
154+
if (log != null && log.isDebugEnabled()) {
155+
commandLine.addArgument("--verbose");
156+
}
157+
commandLine.addArgument(command);
158+
return commandLine;
159+
}
160+
161+
private void logCommandLine(final CommandLine commandLine) {
162+
if (log != null && log.isDebugEnabled()) {
163+
log.debug("Executing Allure command: " + Arrays.toString(commandLine.toStrings()));
164+
}
165+
}
166+
139167
private void addPathArgument(final CommandLine commandLine, final Path path) {
140168
commandLine.addArgument(path.toAbsolutePath().toString(), isWindows());
141169
}

src/main/java/io/qameta/allure/maven/AllureGenerateMojo.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ private void installAllure(final AllureVersion allureVersion) throws MavenReport
294294

295295
private void installAllure2(final AllureVersion allureVersion) throws MavenReportException {
296296
try {
297-
final AllureCommandline commandline =
298-
new AllureCommandline(Paths.get(installDirectory), allureVersion.getVersion());
297+
final AllureCommandline commandline = new AllureCommandline(Paths.get(installDirectory),
298+
allureVersion.getVersion(), getLog());
299299
getLog().info(String.format("Allure installation directory %s", installDirectory));
300300
getLog().info(String.format("Try to finding out allure %s", commandline.getVersion()));
301301

@@ -344,8 +344,9 @@ private void generateAllure2Report(final List<Path> resultsPaths,
344344
try {
345345
final Path reportPath = Paths.get(getReportDirectory());
346346

347-
final AllureCommandline commandline = new AllureCommandline(
348-
Paths.get(getInstallDirectory()), allureVersion.getVersion(), reportTimeout);
347+
final AllureCommandline commandline =
348+
new AllureCommandline(Paths.get(getInstallDirectory()),
349+
allureVersion.getVersion(), reportTimeout, getLog());
349350

350351
getLog().info("Generate report to " + reportPath);
351352
commandline.generateReport(resultsPaths, reportPath, Boolean.TRUE.equals(singleFile));
@@ -382,7 +383,7 @@ protected Allure3Commandline createAllure3Commandline(final AllureVersion allure
382383
nodeVersion, nodeDownloadUrl, npmRegistry, resolveAllurePackagePathOrNull(),
383384
ProxyUtils.getProxy(session, decrypter),
384385
AllureCommandline.getDownloadProperties(session),
385-
session != null && session.isOffline(), timeout);
386+
session != null && session.isOffline(), timeout, getLog());
386387
}
387388

388389
protected void validateAllure3Configuration() throws IOException {

src/main/java/io/qameta/allure/maven/AllureInstallMojo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public void execute() throws MojoExecutionException {
9393
}
9494

9595
private void installAllure2(final AllureVersion allureVersion) throws IOException {
96-
final AllureCommandline commandline =
97-
new AllureCommandline(Paths.get(installDirectory), allureVersion.getVersion());
96+
final AllureCommandline commandline = new AllureCommandline(Paths.get(installDirectory),
97+
allureVersion.getVersion(), getLog());
9898
getLog().info(String.format("Allure installation directory %s", installDirectory));
9999
getLog().info(String.format("Try to finding out allure %s", commandline.getVersion()));
100100

@@ -121,7 +121,7 @@ private void installAllure3(final AllureVersion allureVersion) throws IOExceptio
121121
allureVersion.getVersion(), nodeVersion, nodeDownloadUrl, npmRegistry,
122122
resolveAllurePackagePathOrNull(), ProxyUtils.getProxy(session, decrypter),
123123
AllureCommandline.getDownloadProperties(session),
124-
session != null && session.isOffline(), 3600);
124+
session != null && session.isOffline(), 3600, getLog());
125125
getLog().info(String.format("Allure installation directory %s", installDirectory));
126126
getLog().info(String.format("Try to finding out allure %s using Node.js %s",
127127
commandline.getVersion(), commandline.getNodeVersion()));

src/main/java/io/qameta/allure/maven/AllureServeMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void serveAllure2(final List<Path> resultsPaths, final AllureVersion all
8686
try {
8787
final AllureCommandline commandline =
8888
new AllureCommandline(Paths.get(getInstallDirectory()),
89-
allureVersion.getVersion(), this.serveTimeout);
89+
allureVersion.getVersion(), this.serveTimeout, getLog());
9090

9191
getLog().info("Serve Allure report using a temporary directory managed by the CLI.");
9292
commandline.serve(resultsPaths, this.serveHost, this.servePort);

0 commit comments

Comments
 (0)