You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2025/04/10 14:51:05 UTC

(logging-log4j-samples) branch main updated: Add example to test transitive compile-time dependencies in Gradle (#279)

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j-samples.git


The following commit(s) were added to refs/heads/main by this push:
     new b12af68  Add example to test transitive compile-time dependencies in Gradle (#279)
b12af68 is described below

commit b12af688a6c32ae70780b8b667893d9aa51d26bb
Author: Jendrik Johannes <je...@gmail.com>
AuthorDate: Thu Apr 10 16:51:00 2025 +0200

    Add example to test transitive compile-time dependencies in Gradle (#279)
    
    * Add example to test transitive compile-time dependencies in Gradle
    
    This adds an example that show that the Gradle Metadata published
    for Log4j has the desired effect on the compile classpath when
    Log4j is used in Gradle builds.
    
    This changes the "Android test pipeline" to a "Gradle test pipeline"
    to run all Gradle-based tests. Which are at the moment the
    "Android test" and this newly added test.
    
    Signed-off-by: Jendrik Johannes <je...@gmail.com>
    
    * Downgrade Android plugin to a supported version
    
    ---------
    
    Signed-off-by: Jendrik Johannes <je...@gmail.com>
---
 ...eusable-test.yaml => gradle-reusable-test.yaml} |  20 ++++--
 .github/workflows/integration-test.yaml            |   4 +-
 .gitignore                                         |   3 +
 README.adoc                                        |   3 +
 .../gradle.properties => gradle.properties         |   4 ++
 .../gradle => gradle}/wrapper/gradle-wrapper.jar   | Bin
 .../wrapper/gradle-wrapper.properties              |   0
 log4j-samples-android/gradlew => gradlew           |   0
 log4j-samples-android/gradlew.bat => gradlew.bat   |   0
 log4j-samples-android/README.adoc                  |   4 +-
 log4j-samples-android/app/build.gradle             |   2 +-
 .../README.adoc                                    |  24 ++-----
 log4j-samples-gradle-metadata/build.gradle         |  72 +++++++++++++++++++++
 .../src/main/java/module-info.java                 |   7 +-
 .../src/main/java/org/example/App.java             |  15 ++++-
 .../settings.gradle => settings.gradle             |   8 ++-
 16 files changed, 131 insertions(+), 35 deletions(-)

diff --git a/.github/workflows/android-reusable-test.yaml b/.github/workflows/gradle-reusable-test.yaml
similarity index 90%
rename from .github/workflows/android-reusable-test.yaml
rename to .github/workflows/gradle-reusable-test.yaml
index b44a698..d6d5b8e 100644
--- a/.github/workflows/android-reusable-test.yaml
+++ b/.github/workflows/gradle-reusable-test.yaml
@@ -38,7 +38,7 @@ permissions: read-all
 
 jobs:
 
-  android-test:
+  gradle-test:
 
     runs-on: ubuntu-latest
 
@@ -64,6 +64,17 @@ jobs:
           develocity-url: https://develocity.apache.org
           develocity-plugin-version: 3.18.2
 
+      - name: Test log4j-samples-gradle-metadata
+        id: build
+        shell: bash
+        env:
+          LOG4J_VERSION: ${{ inputs.log4j-version }}
+          LOG4J_REPOSITORY_URL: ${{ inputs.log4j-repository-url }}
+        run: |
+          ./gradlew \
+          -Plog4j.version=$LOG4J_VERSION \
+          -Plog4j.repository.url=$LOG4J_REPOSITORY_URL \
+          :app:build :app:connectedCheck
 
       - name: Enable KVM
         run: |
@@ -112,18 +123,17 @@ jobs:
             sleep 1
           done
 
-      - name: Build
+      - name: Test log4j-samples-android
         id: build
         shell: bash
         env:
           LOG4J_VERSION: ${{ inputs.log4j-version }}
           LOG4J_REPOSITORY_URL: ${{ inputs.log4j-repository-url }}
         run: |
-          log4j-samples-android/gradlew -p log4j-samples-android \
-          --console plain \
+          ./gradlew \
           -Plog4j.version=$LOG4J_VERSION \
           -Plog4j.repository.url=$LOG4J_REPOSITORY_URL \
-          build connectedCheck
+          :app:build :app:connectedCheck
 
       - name: Remove AVD Device
         if: ${{ always() && env.EMULATOR_STARTED == 'true' }}
diff --git a/.github/workflows/integration-test.yaml b/.github/workflows/integration-test.yaml
index a2f8670..b06b88d 100644
--- a/.github/workflows/integration-test.yaml
+++ b/.github/workflows/integration-test.yaml
@@ -65,8 +65,8 @@ jobs:
       ref: ${{ inputs.samples-ref }}
       test-report-suffix: '-it'
 
-  android-test:
-    uses: apache/logging-log4j-samples/.github/workflows/android-reusable-test.yaml@main
+  gradle-test:
+    uses: apache/logging-log4j-samples/.github/workflows/gradle-reusable-test.yaml@main
     with:
       log4j-version: ${{ inputs.log4j-version }}
       log4j-repository-url: ${{ inputs.log4j-repository-url }}
diff --git a/.gitignore b/.gitignore
index 9b67970..d6f101f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,6 +18,9 @@ target/
 .flattened-pom.xml
 /.mvn/wrapper/maven-wrapper.jar
 /.mvn/extensions.xml
+# Gradle
+.gradle/
+build/
 # IDEA
 .idea/
 *.iml
diff --git a/README.adoc b/README.adoc
index 3da3cc9..c1ddd7f 100644
--- a/README.adoc
+++ b/README.adoc
@@ -33,6 +33,9 @@ Explains how to send logs to https://flume.apache.org[Apache Flume].
 xref:log4j-samples-graalvm/README.adoc[`log4j-samples-graalvm`]::
 Explains how to use Log4j API and its implementation to create native GraalVM images.
 
+xref:log4j-samples-gradle-metadata/README.adoc[`log4j-samples-gradle-metadata`]::
+Shows how strict compile classpath or module path checking can be enabled for compilation in Gradle when Log4j is used and that all required annotation libraries are available.
+
 xref:log4j-samples-jlink/README.adoc[`log4j-samples-jlink`]::
 An example of JLink custom JRE.
 
diff --git a/log4j-samples-android/gradle.properties b/gradle.properties
similarity index 95%
rename from log4j-samples-android/gradle.properties
rename to gradle.properties
index 0962e86..a715335 100644
--- a/log4j-samples-android/gradle.properties
+++ b/gradle.properties
@@ -15,6 +15,10 @@
 # limitations under the License.
 #
 ##
+
+# Enable assertions
+org.gradle.jvmargs=-ea
+
 # Required by AndroidX dependencies
 android.useAndroidX = true
 
diff --git a/log4j-samples-android/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
similarity index 100%
rename from log4j-samples-android/gradle/wrapper/gradle-wrapper.jar
rename to gradle/wrapper/gradle-wrapper.jar
diff --git a/log4j-samples-android/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
similarity index 100%
rename from log4j-samples-android/gradle/wrapper/gradle-wrapper.properties
rename to gradle/wrapper/gradle-wrapper.properties
diff --git a/log4j-samples-android/gradlew b/gradlew
similarity index 100%
rename from log4j-samples-android/gradlew
rename to gradlew
diff --git a/log4j-samples-android/gradlew.bat b/gradlew.bat
similarity index 100%
rename from log4j-samples-android/gradlew.bat
rename to gradlew.bat
diff --git a/log4j-samples-android/README.adoc b/log4j-samples-android/README.adoc
index cfb318c..7a78588 100644
--- a/log4j-samples-android/README.adoc
+++ b/log4j-samples-android/README.adoc
@@ -25,10 +25,10 @@ To build the project, run:
 
 [source,shell]
 ----
-./gradlew build
+./gradlew :app:build
 ----
 
-inside this directory.
+in the samples repository root.
 
 Since version `2.25.0` this application is part of the integration tests for a Log4j release.
 
diff --git a/log4j-samples-android/app/build.gradle b/log4j-samples-android/app/build.gradle
index 6f2e5cf..383951f 100644
--- a/log4j-samples-android/app/build.gradle
+++ b/log4j-samples-android/app/build.gradle
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 plugins {
-    id 'com.android.application'
+    id 'com.android.application' version '8.2.0'
 }
 
 android {
diff --git a/log4j-samples-android/README.adoc b/log4j-samples-gradle-metadata/README.adoc
similarity index 50%
copy from log4j-samples-android/README.adoc
copy to log4j-samples-gradle-metadata/README.adoc
index cfb318c..a3a89a7 100644
--- a/log4j-samples-android/README.adoc
+++ b/log4j-samples-gradle-metadata/README.adoc
@@ -14,27 +14,17 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 ////
-= Apache Log4j Samples: Android application
+= Apache Log4j Samples: Gradle Metadata Consumption
 
-This project is an example of Android application that uses
-https://logging.apache.org/log4j/2.x/manual/api.html[the Log4j API]
-for logging together with its reference implementation
-https://logging.apache.org/log4j/2.x/manual/implementation.html[Log4j Core].
+This project is an example that validates that, when used in Gradle, Log4j provides transitive dependencies to annotation libraries on the compile classpath.
+The example also checks that these libraries are visible when used on the Module Path (JPMS), by including a module-info.java file.
+This project is part of the integration tests for a Log4j release.
 
-To build the project, run:
+To build the project, and with that run all validations, run:
 
 [source,shell]
 ----
-./gradlew build
+./gradlew :log4j-samples-gradle-metadata:build
 ----
 
-inside this directory.
-
-Since version `2.25.0` this application is part of the integration tests for a Log4j release.
-
-== Known Android limitations
-
-The Android platform does not implement all the features of a standard JRE, which will prevent you from using all the features offered by Log4j Core.
-For a list of known limitations, see
-https://logging.apache.org/log4j/2.x/faq.html#android[our Android F.A.Q. entry].
-
+in the samples repository root.
diff --git a/log4j-samples-gradle-metadata/build.gradle b/log4j-samples-gradle-metadata/build.gradle
new file mode 100644
index 0000000..297b9c9
--- /dev/null
+++ b/log4j-samples-gradle-metadata/build.gradle
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+plugins {
+    id("application")
+}
+
+def log4jVersion = providers.gradleProperty("log4j.version").getOrElse("2.+")
+
+application {
+    mainModule = "org.example.log4j.metadata"
+    mainClass = "org.example.App" // see: src/main/java/org/example/App.java
+}
+
+dependencies {
+    implementation("org.apache.logging.log4j:log4j-api:$log4jVersion")
+}
+
+tasks.withType(JavaCompile).configureEach {
+    options.compilerArgs.add("-Werror") // treat all warnings as errors
+    options.compilerArgs.add("-Xlint:all") // includes 'classfile' check
+}
+
+// Check the classpaths
+def expectedRuntimeClasspath = [
+    "log4j-api"
+]
+def expectedCompileClasspath = [
+    "biz.aQute.bnd.annotation",
+    "error_prone_annotations",
+    "jspecify",
+    "jsr305",
+    "log4j-api",
+    "org.osgi.annotation.bundle",
+    "org.osgi.annotation.versioning",
+    "org.osgi.resource",
+    "org.osgi.service.serviceloader",
+    "spotbugs-annotations"
+]
+
+tasks.register("assertRuntimeClasspath") {
+    inputs.files(configurations.runtimeClasspath)
+    doLast {
+        def actual = inputs.files.collect { it.name.replaceAll("-[0-9].*", "") }
+        assert actual.sort() == expectedRuntimeClasspath : "Unexpected runtime classpath: $actual"
+    }
+}
+tasks.register("assertCompileClasspath") {
+    inputs.files(configurations.compileClasspath)
+    doLast {
+        def actual = inputs.files.collect { it.name.replaceAll("-[0-9].*", "") }
+        assert actual.sort() == expectedCompileClasspath : "Unexpected compile classpath: $actual"
+    }
+}
+
+tasks.named("check") {
+    dependsOn(tasks.named("assertRuntimeClasspath"))
+    dependsOn(tasks.named("assertCompileClasspath"))
+}
diff --git a/log4j-samples-android/build.gradle b/log4j-samples-gradle-metadata/src/main/java/module-info.java
similarity index 86%
copy from log4j-samples-android/build.gradle
copy to log4j-samples-gradle-metadata/src/main/java/module-info.java
index cf72bc4..b5c6c9c 100644
--- a/log4j-samples-android/build.gradle
+++ b/log4j-samples-gradle-metadata/src/main/java/module-info.java
@@ -14,7 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-plugins {
-    id 'com.android.application' version '8.8.2' apply false
-    id 'com.android.library' version '8.8.0' apply false
-}
+module org.example.log4j.metadata {
+    requires org.apache.logging.log4j;
+}
\ No newline at end of file
diff --git a/log4j-samples-android/build.gradle b/log4j-samples-gradle-metadata/src/main/java/org/example/App.java
similarity index 59%
rename from log4j-samples-android/build.gradle
rename to log4j-samples-gradle-metadata/src/main/java/org/example/App.java
index cf72bc4..ef45021 100644
--- a/log4j-samples-android/build.gradle
+++ b/log4j-samples-gradle-metadata/src/main/java/org/example/App.java
@@ -14,7 +14,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-plugins {
-    id 'com.android.application' version '8.8.2' apply false
-    id 'com.android.library' version '8.8.0' apply false
+package org.example;
+
+public class App {
+    org.apache.logging.log4j.message.ParameterizedMessage m; // needs errorprone
+    org.apache.logging.log4j.status.StatusData d; // needs spotbugs
+    org.apache.logging.log4j.util.SystemPropertiesPropertySource s; // needs aQute.bnd
+    org.apache.logging.log4j.util.Activator a; // needs osgi
+    org.apache.logging.log4j.spi.LoggerRegistry<?> r; // needs jspecify
+
+    public static void main(String[] args) {
+        System.out.println("Hello from: " + App.class.getModule().getName());
+    }
 }
diff --git a/log4j-samples-android/settings.gradle b/settings.gradle
similarity index 90%
rename from log4j-samples-android/settings.gradle
rename to settings.gradle
index 2c5fb34..9b3d409 100644
--- a/log4j-samples-android/settings.gradle
+++ b/settings.gradle
@@ -48,5 +48,11 @@ dependencyResolutionManagement {
         }
     }
 }
-rootProject.name = "Log4j Core Android"
+rootProject.name = 'logging-log4j-samples'
+
+// Android example
 include ':app'
+project(':app').projectDir = file('log4j-samples-android/app')
+
+// Gradle Metadata example
+include ':log4j-samples-gradle-metadata'