Skip to content

Commit 6d4ab82

Browse files
authored
Merge pull request #702 from axonivy/slf4j.warnings
XIVY-16828 Propagate Process warnings to Maven CLI console
2 parents d274d54 + 43f39c8 commit 6d4ab82

File tree

5 files changed

+104
-6
lines changed

5 files changed

+104
-6
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<properties>
4242
<java.version>21</java.version>
4343
<maven.version>3.9.9</maven.version>
44-
<slf4j.version>2.0.13</slf4j.version>
44+
<slf4j.version>1.7.36</slf4j.version>
4545
<junit-jupiter.version>5.12.2</junit-jupiter.version>
4646
<site.path>snapshot</site.path>
4747
<other.site.path>release</other.site.path>

src/main/java/ch/ivyteam/ivy/maven/engine/Slf4jSimpleEngineProperties.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
package ch.ivyteam.ivy.maven.engine;
1818

19+
import java.lang.reflect.Method;
1920
import java.util.Arrays;
2021
import java.util.List;
2122

22-
import org.slf4j.simple.SimpleLogger;
23+
import org.slf4j.impl.SimpleLogger;
2324

2425
/**
2526
* Sets the logging properties for the ivy engine.
@@ -65,6 +66,19 @@ public static void install() {
6566
// only warnings from any logger used by ivy third parties (e.g.
6667
// org.apache.myfaces.xxx, org.apache.cxf, ...)
6768
System.setProperty(DEFAULT_LOG_LEVEL, Level.WARNING);
69+
70+
// remain in same stream as the Maven CLI; don't use the default 'System.err'
71+
System.setProperty(SimpleLogger.LOG_FILE_KEY, "System.out");
72+
}
73+
74+
public static void enforceSimpleConfigReload() {
75+
try {
76+
Method initMethod = SimpleLogger.class.getDeclaredMethod("init");
77+
initMethod.setAccessible(true);
78+
initMethod.invoke(null);
79+
} catch (Exception ex) {
80+
throw new RuntimeException(ex);
81+
}
6882
}
6983

7084
public static void reset() {
@@ -91,7 +105,7 @@ private static void setDefaultProperty(String property, String value) {
91105
}
92106

93107
/**
94-
* Valid levels as documented in {@link org.slf4j.simple.SimpleLogger}
108+
* Valid levels as documented in {@link SimpleLogger}
95109
*/
96110
interface Level {
97111
String TRACE = "trace";

src/test/java/ch/ivyteam/ivy/maven/TestSlf4jWarningConfiguration.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,54 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
import java.io.PrintStream;
8+
9+
import org.junit.jupiter.api.BeforeEach;
510
import org.junit.jupiter.api.Test;
6-
import org.slf4j.simple.SimpleLogger;
11+
import org.slf4j.LoggerFactory;
12+
import org.slf4j.impl.SimpleLogger;
713

814
import ch.ivyteam.ivy.maven.engine.Slf4jSimpleEngineProperties;
915

1016
class TestSlf4jWarningConfiguration {
17+
18+
@BeforeEach
19+
void setup() {
20+
Slf4jSimpleEngineProperties.install();
21+
}
22+
1123
/*
1224
* XIVY-3123 Streamline the log output to be maven-like, instead of logging
1325
* [WARN] we want [WARNING]. This allows us to use the maven log parser on our
1426
* jenkins pipelines to avoid introducing new warnings.
1527
*/
1628
@Test
1729
void mavenLikeWarning() {
18-
Slf4jSimpleEngineProperties.install();
1930
assertThat(System.getProperty(SimpleLogger.WARN_LEVEL_STRING_KEY))
2031
.as("SLF4J warning string is not maven-like [WARNING]")
2132
.isEqualTo("WARNING");
2233
}
34+
35+
@Test
36+
void mavenLoggerWarningOut() throws IOException {
37+
var original = System.out;
38+
try (var bos = new ByteArrayOutputStream();
39+
var memoryOut = new PrintStream(bos);) {
40+
System.setOut(memoryOut);
41+
42+
var logger = LoggerFactory.getLogger("maven.cli");
43+
logger.warn("hey");
44+
45+
String out = bos.toString();
46+
assertThat(out)
47+
.as("WARNING bracket matches Maven CLI")
48+
.startsWith("[WARNING] hey");
49+
50+
} finally {
51+
System.setOut(original);
52+
}
53+
}
54+
2355
}

src/test/java/ch/ivyteam/ivy/maven/compile/TestCompileProjectMojo.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,41 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21+
import java.io.ByteArrayOutputStream;
2122
import java.io.IOException;
23+
import java.io.PrintStream;
2224
import java.nio.file.Files;
2325
import java.nio.file.Path;
2426
import java.util.List;
2527

28+
import org.apache.commons.lang3.StringUtils;
29+
import org.junit.After;
30+
import org.junit.Before;
2631
import org.junit.Rule;
2732
import org.junit.Test;
2833

2934
import ch.ivyteam.ivy.maven.BaseEngineProjectMojoTest;
35+
import ch.ivyteam.ivy.maven.engine.Slf4jSimpleEngineProperties;
3036
import ch.ivyteam.ivy.maven.log.LogCollector;
3137
import ch.ivyteam.ivy.maven.util.PathUtils;
3238

3339
public class TestCompileProjectMojo extends BaseEngineProjectMojoTest {
3440
private CompileTestProjectMojo testMojo;
3541

42+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
43+
private final PrintStream originalOut = System.out;
44+
45+
@Before
46+
public void setup() {
47+
Slf4jSimpleEngineProperties.enforceSimpleConfigReload();
48+
System.setOut(new PrintStream(outContent));
49+
}
50+
51+
@After
52+
public void restoreStreams() {
53+
System.setOut(originalOut);
54+
}
55+
3656
@Rule
3757
public CompileMojoRule<CompileProjectMojo> compile = new CompileMojoRule<>(
3858
CompileProjectMojo.GOAL){
@@ -101,4 +121,36 @@ public void compilerSettingsFile_notFoundWarnings() throws Exception {
101121
mojo.execute();
102122
assertThat(log.getWarnings().toString()).contains("Could not locate compiler settings file");
103123
}
124+
125+
@Test
126+
public void validateProcess() throws Exception {
127+
CompileProjectMojo mojo = compile.getMojo();
128+
129+
Path project = mojo.project.getBasedir().toPath();
130+
var dataClassDir = project.resolve("src_dataClasses");
131+
var wsProcDir = project.resolve("src_wsproc");
132+
PathUtils.clean(wsProcDir);
133+
PathUtils.clean(dataClassDir);
134+
135+
var ws = project.resolve("processes").resolve("myWebService.p.json");
136+
String wsJson = Files.readString(ws);
137+
var patched = StringUtils.replace(wsJson, "//TEMPLATE!!", "ivy.session.assignRole(null);");
138+
Files.writeString(ws, patched);
139+
140+
mojo.buildApplicationDirectory = Files.createTempDirectory("MyBuildApplicationVald");
141+
mojo.execute();
142+
143+
assertThat(outContent.toString())
144+
.contains("processes/myWebService.p.json /element=148CA74B16C580BF-ws0 : "
145+
+ "Start code: Method assignRole of class ch.ivyteam.ivy.workflow.IWorkflowSession "
146+
+ "is deprecated");
147+
148+
var warning = outContent.toString().lines()
149+
.filter(l -> l.contains("/element=148CA74B16C580BF-ws0"))
150+
.findFirst().get();
151+
assertThat(warning)
152+
.as("WARNING prefix is streamlined with Maven CLI")
153+
.startsWith("[WARNING]");
154+
}
155+
104156
}

src/test/resources/base/processes/myWebService.p.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"params" : [
1717
{ "name" : "myText", "type" : "String", "desc" : "" }
1818
],
19-
"map" : { }
19+
"code" : "//TEMPLATE!!"
2020
}
2121
},
2222
"visual" : {

0 commit comments

Comments
 (0)