Was tricky to find through google, but I found that replacing the log4j entries in the new POM (bottom of the original email) with the following will solve the issue:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

If I understood the explanation, it seems that the spring boot starter (parent) defaults to slf4j and that the code above removes that support while replacing it with log4j2.


On 1/25/2022 11:33 AM, Bruno Melloni wrote:

I wrote and used a custom library that includes some log4j aware classes.  Was working perfectly when built as a standard Java JAR.  I am converting the POM to rely on a few Spring Boot starters as a way to simplify it.

All went well until I run "Maven Install", when I got the following ClassCastException even though my code never uses slf4j:  "*class org.apache.logging.slf4j.SLF4JLogger cannot be cast to class org.apache.logging.log4j.core.Logger (org.apache.logging.slf4j.SLF4JLogger and org.apache.logging.log4j.core.Logger are in unnamed module of loader 'app')*"

The issue is probably trivial and I suspect that one or more of the starters (possibly the TEST portion) relies on slf4j and I somehow need to provide a library to accept slf4j and automatically convert to log4j logging.

Can anybody see where I messed up, and know how to fix it?

*This is the old (good) POM*:

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd";>
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.cndc</groupId>
  <artifactId>libCore</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <oshi.version>5.8.2</oshi.version>
  </properties>

  <dependencies>
    <!-- Apache commons -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-text</artifactId>
      <version>1.9</version>
    </dependency>
    <!-- Log4j -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.15.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.15.0</version>
    </dependency>

    <!-- JJWT, library to add JWT bearer token support -->
    <dependency>
      <groupId>io.jsonwebtoken</groupId>
      <artifactId>jjwt</artifactId>
      <version>0.9.1</version>
    </dependency>
    <dependency>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>2.3.1</version>
    </dependency>

    <!-- Jackson JSON -->
    <dependency>  <!-- Jackson -->
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.12.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.13</version>
    </dependency>

    <!-- Server compatible -->
    <dependency> <!-- Servlet API -->
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

    <!--  Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.9</version>
    </dependency>
    <!-- SpringSecurity -->
    <dependency>
<groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>5.5.2</version>
    </dependency>
    <dependency>
<groupId>org.springframework.security</groupId>
      <artifactId>spring-security-core</artifactId>
      <version>5.5.2</version>
    </dependency>
    <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
      <version>5.5.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.10</version>
    </dependency>

    <dependency>
      <groupId>com.github.oshi</groupId>
      <artifactId>oshi-core</artifactId>
      <version>${oshi.version}</version>
    </dependency>

    <!-- Misc -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>

    <!-- Other -->
    <dependency> <!-- JUnit -->
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
          <configuration>
            <release>15</release>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

*And this is the POM that causes the error*:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd";>
  <modelVersion>4.0.0</modelVersion>
  <parent>
     <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
     <version>2.6.3</version>
     <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.cndc</groupId>
    <artifactId>core-lib</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>core-lib</name>
    <description>Core library for all CNDC apps</description>

    <properties>
        <java.version>17</java.version>
    <oshi.version>5.8.2</oshi.version>
    </properties>

  <dependencies>
     <!-- starters -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
<groupId>org.springframework.security</groupId>
      <artifactId>spring-security-test</artifactId>
      <scope>test</scope>
    </dependency>

    <!-- Apache commons -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-text</artifactId>
      <version>1.9</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>
    <!-- Log4j -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
    </dependency>
    <!-- Hardware -->
    <dependency>
      <groupId>com.github.oshi</groupId>
      <artifactId>oshi-core</artifactId>
      <version>${oshi.version}</version>
    </dependency>
    <!-- Jackson JSON -->
    <dependency>  <!-- Jackson -->
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
    </dependency>
    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

Reply via email to