Search code examples
javamavenlog4j2logbackopenrewrite

Migrate from Log4j2 to Logback not working using OpenRewrite recipe


Following the instructions on https://docs.openrewrite.org/recipes/java/logging/logback/log4jtologback I've added the plugin and dependencies to my pom.xml. I had to exclude many artifacts because of version conflicts but I can run mvn recipe:run but it fails with the following error:

[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.13.0:run (default-cli) on project serverlib: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.13.0:run failed: Recipes not found: org.openrewrite.java.logging.logback.Log4jToLogback

Note that this is a library project. Any ideas on how to fix this?

I'm using the following pom.xml file:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>XXX</groupId>
    <artifactId>XXX</artifactId>
    <version>x.yy-SNAPSHOT</version>

    <name>ServerLib</name>

    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <!-- Enable reproducible builds, see https://maven.apache.org/guides/mini/guide-reproducible-builds.html -->
        <project.build.outputTimestamp>2023-09-22T07:16:28Z</project.build.outputTimestamp>

        <server.project-dir>${basedir}</server.project-dir>
        <server.work-dir>${java.io.tmpdir}\XYZ</server.work-dir>

        <local.properties.path>${user.home}/User/${project.groupId}.${project.artifactId}/local.properties</local.properties.path>

        <!-- Some version info for related dependencies -->
        <log4j.version>2.17.1</log4j.version>
        <jersey.version>2.40</jersey.version>
        <aws.version>2.20.69</aws.version>
        <jsonwebtoken.version>0.12.3</jsonwebtoken.version>
    </properties>

    <build>
        <finalName>${project.artifactId}</finalName>

        <!-- Enable filtering -->
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>

        <!-- Default plugins, fixate versions -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.3.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.3.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.11.0</version>
                    <inherited>true</inherited>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.3.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.4.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>3.1.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>3.1.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>3.0.1</version>
                    <!-- <configuration><remoteTagging>false</remoteTagging></configuration> -->
                </plugin>
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>4.0.0-M11</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.4.5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <version>3.4.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <version>1.2.1</version>
                </plugin>
                <plugin>
                    <groupId>org.datanucleus</groupId>
                    <artifactId>datanucleus-maven-plugin</artifactId>
                    <version>5.2.1</version>
                </plugin>
                <plugin>
                    <groupId>org.openrewrite.maven</groupId>
                    <artifactId>rewrite-maven-plugin</artifactId>
                    <version>5.13.0</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>

            <!-- Migrate Log4j2 to Logback -->
            <plugin>
                <groupId>org.openrewrite.maven</groupId>
                <artifactId>rewrite-maven-plugin</artifactId>
                <configuration>
                    <activeRecipes>
                        <recipe>org.openrewrite.java.logging.logback.Log4jToLogback</recipe>
                    </activeRecipes>
                </configuration>
            </plugin>

            <!-- Read local properties before enforcer such that we can check it's 
                settings -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${local.properties.path}</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Verify project -->
            <plugin>
                <artifactId>maven-enforcer-plugin</artifactId>
                <executions>
                    <execution>
                        <id>sanity-check</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <phase>validate</phase>
                        <configuration>
                            <rules>
                                <banDuplicatePomDependencyVersions />
                                <!-- There are incompatible dependencies, so level is set to warn -->
                                <dependencyConvergence>
                                    <excludes>
                                        <exclude>org.apache.commons:commons-lang3:*</exclude>
                                        <exclude>com.fasterxml.jackson.core:*:jar:*</exclude>
                                        <exclude>org.slf4j:slf4j-api:jar:*</exclude>
                                        <exclude>commons-codec:commons-codec:jar:*</exclude>
                                    </excludes>
                                </dependencyConvergence>
                                <requireUpperBoundDeps>
                                    <excludes>
                                        <exclude>org.apache.commons:commons-lang3</exclude>
                                        <exclude>com.fasterxml.jackson.core:jackson-databind</exclude>
                                    </excludes>
                                </requireUpperBoundDeps>
                                <reactorModuleConvergence />
                                <!-- <requireExplicitDependencyScope /> -->
                                <requirePluginVersions />
                                <requireJavaVersion>
                                    <version>1.8</version>
                                </requireJavaVersion>
                                <requireMavenVersion>
                                    <version>3.9.5</version>
                                </requireMavenVersion>
                                <!-- Check that the properties defined in local.properties have sensible 
                                    values -->
                                <requireFilesExist>
                                    <files>
                                        <file>${server.nodejs-dir}/node.exe</file>
                                        <file>${server.print-script-path}</file>
                                    </files>
                                </requireFilesExist>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Datanucleus enhancer plugin -->
            <plugin>
                <groupId>org.datanucleus</groupId>
                <artifactId>datanucleus-maven-plugin</artifactId>
                <configuration>
                    <api>JDO</api>
                    <verbose>false</verbose>
                    <!-- This property is necessary since otherwise path becomes too long -->
                    <fork>false</fork>
                    <!-- Note that plugin uses old log4j 1 configuration -->
                    <log4jConfiguration>${basedir}/log4j2_enhancer.xml</log4jConfiguration>
                    <verbose>false</verbose>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Also create a jar containing test classes and resources -->
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                        <configuration>
                            <!-- Exclude config files that are only for serverlib local tests -->
                            <excludes>
                                <exclude>log4j2.xml</exclude>
                                <exclude>server.hjson</exclude>
                                <exclude>settings.hjson</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>

    <dependencies>
        <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-logging-frameworks</artifactId>
            <version>2.1.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-annotations</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.fasterxml.jackson.dataformat</groupId>
                        <artifactId>jackson-dataformat-csv</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.apache.commons</groupId>
                        <artifactId>commons-text</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.google.errorprone</groupId>
                        <artifactId>error_prone_annotations</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.checkerframework</groupId>
                        <artifactId>checker-qual</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.jetbrains</groupId>
                        <artifactId>annotations</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm-util</artifactId>
                    </exclusion>
                </exclusions>
        </dependency>

        <!-- Log4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <!-- Jersey -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-bean-validation</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- JSON Webtoken -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>${jsonwebtoken.version}</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>${jsonwebtoken.version}</version>
            <scope>compile</scope> <!-- Not runtime -->
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>${jsonwebtoken.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk18on</artifactId>
            <version>1.76</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Datanucleus and database -->
        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-accessplatform-jdo-rdbms</artifactId>
            <version>5.1.10</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.5</version>
        </dependency>

        <!-- AWS -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>auth</artifactId>
            <version>${aws.version}</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>kms</artifactId>
            <version>${aws.version}</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>secretsmanager</artifactId>
            <version>${aws.version}</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
            <version>${aws.version}</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3-transfer-manager</artifactId>
            <version>${aws.version}</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk.crt</groupId>
            <artifactId>aws-crt</artifactId>
            <version>0.21.17</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-encryption-sdk-java</artifactId>
            <version>2.4.0</version>
        </dependency>

        <!-- Freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>

        <!-- Util -->
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-bean</artifactId>
            <version>5.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-mail</artifactId>
            <version>5.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>1.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>26.0-jre</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.libphonenumber</groupId>
            <artifactId>libphonenumber</artifactId>
            <version>8.10.15</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20230618</version>
        </dependency>
        <dependency>
            <groupId>org.hjson</groupId>
            <artifactId>hjson</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.hakky54</groupId>
            <artifactId>sslcontext-kickstart</artifactId>
            <version>7.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-csv</artifactId>
            <version>2.15.0</version>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.javafaker</groupId>
            <artifactId>javafaker</artifactId>
            <version>1.0.2</version>
            <scope>test</scope>
        </dependency>

        <!-- Provided dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>org.eclipse.microprofile.openapi</groupId>
            <artifactId>microprofile-openapi-api</artifactId>
            <version>3.1.1</version>
            <scope>provided</scope>
        </dependency>
        
    </dependencies>

</project>

Solution

  • you've added rewrite-logging-frameworks as a regular dependency, instead of a plugin dependency. You will want to remove the following:

        <dependencies>
            <!-- remove this entire dependency -->
            <dependency>
                <groupId>org.openrewrite.recipe</groupId>
                <artifactId>rewrite-logging-frameworks</artifactId>
                <version>2.1.0</version>
            </dependency>
    

    And where you now configure the plugin like this:

            <plugins>
                <!-- Migrate Log4j2 to Logback -->
                <plugin>
                    <groupId>org.openrewrite.maven</groupId>
                    <artifactId>rewrite-maven-plugin</artifactId>
                    <configuration>
                        <activeRecipes>
                            <recipe>org.openrewrite.java.logging.logback.Log4jToLogback</recipe>
                        </activeRecipes>
                    </configuration>
                </plugin>
    

    You should instead add a plugin dependency there.

    <project>
      <build>
        <plugins>
          <plugin>
            <groupId>org.openrewrite.maven</groupId>
            <artifactId>rewrite-maven-plugin</artifactId>
            <version>5.13.0</version>
            <configuration>
              <activeRecipes>
                <recipe>org.openrewrite.java.logging.logback.Log4jToLogback</recipe>
              </activeRecipes>
            </configuration>
            <dependencies>
              <dependency>
                <groupId>org.openrewrite.recipe</groupId>
                <artifactId>rewrite-logging-frameworks</artifactId>
                <version>2.1.0</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
    </project>
    

    Or if you want to make things easier for yourself, just remove all the references to OpenRewrite from your pom.xml file and instead run this one off command:

    mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
      -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-logging-frameworks:RELEASE \
      -Drewrite.activeRecipes=org.openrewrite.java.logging.logback.Log4jToLogback