Fixe Log-Ausgabe und Laden von Resource-Dateien

main
hummel 2025-01-08 00:30:39 +01:00
parent 3ee0bc95bd
commit 4263c8c85c
6 changed files with 44 additions and 19 deletions

View File

@ -66,8 +66,7 @@
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>
de.hs_mannheim.informatik.mvn.Main</mainClass>
<mainClass>de.hs_mannheim.informatik.mvn.Main</mainClass>
</transformer>
</transformers>
</configuration>
@ -120,11 +119,8 @@
</build>
<reporting>
<plugins>
<!-- generate Javadocs via "mvn site" and find them in the site
folder-->
<!-- generate Javadocs via "mvn site" and find them in the sie folder-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>

View File

@ -1,20 +1,41 @@
package de.hs_mannheim.informatik.mvn;
import org.apache.logging.log4j.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Main {
private static final Logger LOG = LogManager.getLogger();
// Aufruf mit externer Log-Config im gleichen Pfad wie das JAR:
// java -Dlog4j.configurationFile=log4j2.xml -jar MvnDemo-0.0.1-SNAPSHOT.jar
private static final Logger LOG = LogManager.getLogger(Main.class);
public static void main(String[] args) {
LOG.info("Hallo Maven, Gitea & Jenkins!?");
LOG.info("Hallo Log4J");
System.out.println("Hello!");
System.out.println(loadText("someText.txt"));
System.out.println(new Main().add(3, 4));
}
public int add(int a, int b) {
LOG.info("a = {}, b = {}", a, b);
return a + b;
}
public static String loadText(String filename) {
try (InputStream in = Main.class.getClassLoader().getResourceAsStream(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -1,17 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Configuration xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-config-2.xsd">
<Appenders>
<!-- Konsole Appender -->
<Console name="Console" target="SYSTEM_OUT">
<!-- Console appender configuration -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %C{1} - %msg%n" />
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</Console>
</Appenders>
<Loggers>
<!-- Root Logger -->
<Root level="info">
<AppenderRef ref="Console" />
<!-- Root logger referring to console appender -->
<Root level="INFO" additivity="false">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1 @@
Hallo Maven!

View File

@ -6,8 +6,13 @@ import org.junit.jupiter.api.Test;
class AddTest {
@Test
void test() {
void testAdd() {
assertEquals(7, new Main().add(3, 4));
}
@Test
void testLoader() {
assertEquals("Test Text", Main.loadText("test.txt"));
}
}

View File

@ -0,0 +1 @@
Test Text