Skip to content

Commit 6dc1380

Browse files
authored
Disable download logs in Maven by default (#159)
Disable download logs in Maven by default This commit adds a flag to Maven by default, which disables the messages like "Downloading from central". The logger is set to level "warn", so errors will still be visible, but successful messages won't clutter logs anymore. This option is also set by default in the GitLab CI template file for maven. See [1] for reference on the option. 1: https://stackoverflow.com/a/35653426/8843830
1 parent a4935a1 commit 6dc1380

File tree

6 files changed

+48
-27
lines changed

6 files changed

+48
-27
lines changed

documentation/docs/steps/mavenExecute.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ Executes a maven command inside a Docker container.
66

77
## Parameters
88

9-
| parameter | mandatory | default | example values |
10-
| ---------------------|-----------|-------------------|----------------------------|
11-
| `dockerImage` | no | 'maven:3.5-jdk-7' | |
12-
| `globalSettingsFile` | no | | 'local_folder/settings.xml'|
13-
| `projectSettingsFile`| no | | |
14-
| `pomPath` | no | | 'local_folder/m2' |
15-
| `flags` | no | | '-o' |
16-
| `goals` | no | | 'clean install' |
17-
| `m2Path` | no | | 'local_folder/m2' |
18-
| `defines` | no | | '-Dmaven.tests.skip=true' |
9+
| parameter | mandatory | default | example values |
10+
| -------------------------------|-----------|-------------------|----------------------------|
11+
| `dockerImage` | no | 'maven:3.5-jdk-7' | |
12+
| `globalSettingsFile` | no | | 'local_folder/settings.xml'|
13+
| `projectSettingsFile` | no | | |
14+
| `pomPath` | no | | 'local_folder/m2' |
15+
| `flags` | no | | '-o' |
16+
| `goals` | no | | 'clean install' |
17+
| `m2Path` | no | | 'local_folder/m2' |
18+
| `defines` | no | | '-Dmaven.tests.skip=true' |
19+
| `logSuccessfulMavenTransfers` | no | `false` | 'true' |
1920

2021
* `dockerImage` Name of the docker image that should be used.
2122
* `globalSettingsFile` Path or url to the mvn settings file that should be used as global settings file.
@@ -25,6 +26,7 @@ Executes a maven command inside a Docker container.
2526
* `goals` Maven goals that should be executed.
2627
* `m2Path` Path to the location of the local repository that should be used.
2728
* `defines` Additional properties.
29+
* `logSuccessfulMavenTransfers` configures maven to log successful downloads. This is set to `false` by default to reduce the noise in build logs.
2830

2931
## Step configuration
3032
The following parameters can also be specified as step parameters using the global configuration file:

resources/default_pipeline_environment.yml

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ steps:
7777
influxServer: 'jenkins'
7878
mavenExecute:
7979
dockerImage: 'maven:3.5-jdk-7'
80+
logSuccessfulMavenTransfers: false
8081
mtaBuild:
8182
buildTarget: 'NEO'
8283
mtaJarLocation: 'mta.jar'

test/groovy/ArtifactSetVersionTest.groovy

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ArtifactSetVersionTest extends BasePiperTest {
6969
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
7070
assertEquals('testCommitId', jer.env.getGitCommitId())
7171

72-
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' versions:set -DnewVersion=1.2.3-20180101010203_testCommitId"))
72+
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId"))
7373
assertThat(jscr.shell, hasItem('git add .'))
7474
assertThat(jscr.shell, hasItem("git commit -m 'update version 1.2.3-20180101010203_testCommitId'"))
7575
assertThat(jscr.shell, hasItem("git remote set-url origin myGitSshUrl"))
@@ -82,15 +82,15 @@ class ArtifactSetVersionTest extends BasePiperTest {
8282
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
8383

8484
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
85-
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' versions:set -DnewVersion=1.2.3-20180101010203_testCommitId"))
85+
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId"))
8686
}
8787

8888
@Test
8989
void testVersioningWithoutScript() {
9090
jsr.step.call(juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
9191

9292
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
93-
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' versions:set -DnewVersion=1.2.3-20180101010203_testCommitId"))
93+
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId"))
9494
}
9595

9696
@Test

test/groovy/MavenExecuteTest.groovy

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
2-
import org.junit.Before
31
import org.junit.Rule
42
import org.junit.Test
53
import org.junit.rules.RuleChain
6-
4+
import util.BasePiperTest
75
import util.JenkinsDockerExecuteRule
6+
import util.JenkinsShellCallRule
87
import util.JenkinsStepRule
8+
import util.Rules
99

1010
import static org.junit.Assert.assertEquals
1111
import static org.junit.Assert.assertTrue
1212

13-
import util.BasePiperTest
14-
import util.JenkinsShellCallRule
15-
import util.Rules
16-
1713
class MavenExecuteTest extends BasePiperTest {
1814

1915
Map dockerParameters
@@ -35,7 +31,16 @@ class MavenExecuteTest extends BasePiperTest {
3531
jsr.step.mavenExecute(script: nullScript, goals: 'clean install')
3632
assertEquals('maven:3.5-jdk-7', jder.dockerParams.dockerImage)
3733

38-
assert jscr.shell[0] == 'mvn clean install'
34+
assert jscr.shell[0] == 'mvn --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean install'
35+
}
36+
37+
@Test
38+
void testExecuteBasicMavenCommandWithDownloadLogsEnabled() throws Exception {
39+
40+
jsr.step.mavenExecute(script: nullScript, goals: 'clean install', logSuccessfulMavenTransfers: true)
41+
assertEquals('maven:3.5-jdk-7', jder.dockerParams.dockerImage)
42+
43+
assert jscr.shell[0] == 'mvn --batch-mode clean install'
3944
}
4045

4146
@Test
@@ -52,7 +57,7 @@ class MavenExecuteTest extends BasePiperTest {
5257
m2Path: 'm2Path',
5358
defines: '-Dmaven.tests.skip=true')
5459
assertEquals('maven:3.5-jdk-8-alpine', jder.dockerParams.dockerImage)
55-
String mvnCommand = "mvn --global-settings 'globalSettingsFile.xml' -Dmaven.repo.local='m2Path' --settings 'projectSettingsFile.xml' --file 'pom.xml' -o clean install -Dmaven.tests.skip=true"
60+
String mvnCommand = "mvn --global-settings 'globalSettingsFile.xml' -Dmaven.repo.local='m2Path' --settings 'projectSettingsFile.xml' --file 'pom.xml' -o --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean install -Dmaven.tests.skip=true"
5661
assertTrue(jscr.shell.contains(mvnCommand))
5762
}
5863

@@ -62,6 +67,6 @@ class MavenExecuteTest extends BasePiperTest {
6267
jsr.step.mavenExecute(script: nullScript, goals: 'clean install')
6368
assertEquals('maven:3.5-jdk-7', jder.dockerParams.dockerImage)
6469

65-
assert jscr.shell[0] == 'mvn clean install'
70+
assert jscr.shell[0] == 'mvn --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean install'
6671
}
6772
}

test/groovy/com/sap/piper/versioning/MavenArtifactVersioningTest.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class MavenArtifactVersioningTest extends BasePiperTest{
4242
av = new MavenArtifactVersioning(nullScript, [filePath: 'pom.xml'])
4343
assertEquals('1.2.3', av.getVersion())
4444
av.setVersion('1.2.3-20180101')
45-
assertEquals('mvn --file \'pom.xml\' versions:set -DnewVersion=1.2.3-20180101', jscr.shell[0])
45+
assertEquals('mvn --file \'pom.xml\' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101', jscr.shell[0])
4646
}
4747

4848
@Test
4949
void testVersioningCustomFilePathSnapshot() {
5050
av = new MavenArtifactVersioning(nullScript, [filePath: 'snapshot/pom.xml'])
5151
assertEquals('1.2.3', av.getVersion())
5252
av.setVersion('1.2.3-20180101')
53-
assertEquals('mvn --file \'snapshot/pom.xml\' versions:set -DnewVersion=1.2.3-20180101', jscr.shell[0])
53+
assertEquals('mvn --file \'snapshot/pom.xml\' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101', jscr.shell[0])
5454
}
5555
}

vars/mavenExecute.groovy

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import com.sap.piper.ConfigurationLoader
21
import com.sap.piper.ConfigurationMerger
32

43
def call(Map parameters = [:]) {
@@ -17,7 +16,8 @@ def call(Map parameters = [:]) {
1716
'flags',
1817
'goals',
1918
'm2Path',
20-
'defines'
19+
'defines',
20+
'logSuccessfulMavenTransfers'
2121
]
2222
Set stepConfigurationKeys = [
2323
'dockerImage',
@@ -66,6 +66,19 @@ def call(Map parameters = [:]) {
6666
command += " ${mavenFlags}"
6767
}
6868

69+
// Always use Maven's batch mode
70+
if (!(command.contains('-B') || command.contains('--batch-mode'))){
71+
command += ' --batch-mode'
72+
}
73+
74+
// Disable log for successful transfers by default. Note this requires the batch-mode flag.
75+
final String disableSuccessfulMavenTransfersLogFlag = ' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'
76+
if (!configuration.logSuccessfulMavenTransfers) {
77+
if (!command.contains(disableSuccessfulMavenTransfersLogFlag)) {
78+
command += disableSuccessfulMavenTransfersLogFlag
79+
}
80+
}
81+
6982
def mavenGoals = configuration.goals
7083
if (mavenGoals?.trim()) {
7184
command += " ${mavenGoals}"

0 commit comments

Comments
 (0)