Skip to content

Commit

Permalink
fix: added publishing jar with depedencies
Browse files Browse the repository at this point in the history
  • Loading branch information
gsidhwani-nr committed Oct 16, 2024
1 parent 77aacb5 commit 479b0e5
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 12 deletions.
25 changes: 13 additions & 12 deletions custom-log4j-appender/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'maven-publish'
}

Expand All @@ -16,14 +15,13 @@ dependencies {
}

jar {
enabled = false // Disable the standard JAR task
}

shadowJar {
archiveClassifier.set('') // Remove the 'all' classifier
mergeServiceFiles()
manifest {
attributes 'Main-Class': 'com.newrelic.labs.logforwarder.MAIN'
attributes(
'Implementation-Title': 'Custom Log4j Appender',
'Implementation-Vendor': 'New Relic Labs',
'Implementation-Vendor-Id': 'com.newrelic.labs',
'Implementation-Version': '1.0.2'
)
}
}

Expand All @@ -41,20 +39,23 @@ task sourcesJar(type: Jar) {
from sourceSets.main.allSource
}



publishing {
publications {
mavenJava(MavenPublication) {
artifact shadowJar // Only include Shadow JAR
from components.java // Include standard JAR

artifact javadocJar
artifact sourcesJar

groupId = 'io.github.newrelic-experimental'
artifactId = 'custom-log4j-appender'
version = '1.0.0'
version = '1.0.2'

pom {
name = 'Custom Log4j Appender'
description = 'A custom Log4j appender for New Relic Labs.'
description = 'A custom Log4j appender for New Relic.'
url = 'https://github.com/newrelic-experimental/newrelic-java-log4j-appender'
licenses {
license {
Expand All @@ -66,7 +67,7 @@ publishing {
developer {
id = 'newrelic'
name = 'New Relic'
email = 'info@newrelic.com'
email = 'gsidhwani@newrelic.com'
}
}
scm {
Expand Down
31 changes: 31 additions & 0 deletions custom-log4j-appender/pom-template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>{{GROUP_ID}}</groupId>
<artifactId>{{ARTIFACT_ID}}</artifactId>
<version>{{VERSION}}</version>
<name>Custom Log4j Appender</name>
<description>A custom Log4j appender for New Relic Labs.</description>
<url>https://github.com/newrelic-experimental/newrelic-java-log4j-appender</url>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>newrelic</id>
<name>New Relic</name>
<email>[email protected]</email>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/newrelic-experimental/newrelic-java-log4j-appender.git</connection>
<developerConnection>scm:git:ssh://github.com/newrelic-experimental/newrelic-java-log4j-appender.git</developerConnection>
<url>https://github.com/newrelic-experimental/newrelic-java-log4j-appender</url>
</scm>
<dependencies>
{{DEPENDENCIES}}
</dependencies>
</project>
159 changes: 159 additions & 0 deletions custom-log4j-appender/publish-jar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/bin/bash

# Set variables
GROUP_ID="io.github.newrelic-experimental"
ARTIFACT_ID="custom-log4j-appender"
VERSION="1.0.2"
KEY_ID="0ED9FD74E81E6D83FAE25F235640EA0B1C631C6F" # Replace with your actual key ID

# Get the current directory (assuming the script is run from the custom-log4j-appender directory)
CURRENT_DIR=$(pwd)
PROJECT_ROOT=$(dirname "$CURRENT_DIR")

echo "Current Directory: $CURRENT_DIR"
echo "Project Root: $PROJECT_ROOT"

# Clean and build the project
cd "$PROJECT_ROOT"
./gradlew clean build jar javadocJar sourcesJar generatePomFileForMavenJavaPublication

# Navigate to the directory containing the artifacts
cd "$CURRENT_DIR/build/libs"

# List the contents of the build/libs directory for debugging
echo "Contents of build/libs:"
ls -la

# Find the files without version numbers
JAR_FILE="custom-log4j-appender.jar"
JAVADOC_FILE="custom-log4j-appender-javadoc.jar"
SOURCES_FILE="custom-log4j-appender-sources.jar"
POM_FILE="custom-log4j-appender-$VERSION.pom"

echo "JAR File: $JAR_FILE"
echo "Javadoc File: $JAVADOC_FILE"
echo "Sources File: $SOURCES_FILE"
echo "POM File: $POM_FILE"

# Check if the files exist before proceeding
if [ ! -f "$JAR_FILE" ] || [ ! -f "$JAVADOC_FILE" ] || [ ! -f "$SOURCES_FILE" ]; then
echo "One or more expected files are missing in build/libs"
ls -la
exit 1
fi

# Prepare the directory structure
TARGET_DIR="$CURRENT_DIR/io/github/newrelic-experimental/custom-log4j-appender/$VERSION"
mkdir -p $TARGET_DIR

# Copy the built artifacts to the appropriate directory with version numbers
echo "Copying artifacts to $TARGET_DIR"
cp $JAR_FILE $TARGET_DIR/custom-log4j-appender-$VERSION.jar
cp $JAVADOC_FILE $TARGET_DIR/custom-log4j-appender-$VERSION-javadoc.jar
cp $SOURCES_FILE $TARGET_DIR/custom-log4j-appender-$VERSION-sources.jar

# Navigate to the target directory
cd $TARGET_DIR

# Generate checksums for the copied files
for file in custom-log4j-appender-$VERSION.jar custom-log4j-appender-$VERSION-javadoc.jar custom-log4j-appender-$VERSION-sources.jar; do
echo "Generating checksums for $file"
md5sum $file | awk '{ print $1 }' > $file.md5
sha1sum $file | awk '{ print $1 }' > $file.sha1
done

# Generate GPG signatures using the specific key ID
for file in custom-log4j-appender-$VERSION.jar custom-log4j-appender-$VERSION-javadoc.jar custom-log4j-appender-$VERSION-sources.jar; do
echo "Generating GPG signature for $file"
gpg --local-user $KEY_ID --armor --detach-sign $file
done

# Generate the dependencies section for the POM file
DEPENDENCIES=$(cat <<EOF
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
<scope>runtime</scope>
</dependency>
EOF
)

# Read the POM template, replace placeholders, and save the final POM file
POM_TEMPLATE=$(<"$CURRENT_DIR/pom-template.xml")
POM_CONTENT="${POM_TEMPLATE//\{\{GROUP_ID\}\}/$GROUP_ID}"
POM_CONTENT="${POM_CONTENT//\{\{ARTIFACT_ID\}\}/$ARTIFACT_ID}"
POM_CONTENT="${POM_CONTENT//\{\{VERSION\}\}/$VERSION}"
POM_CONTENT="${POM_CONTENT//\{\{DEPENDENCIES\}\}/$DEPENDENCIES}"

echo "$POM_CONTENT" > "$TARGET_DIR/$POM_FILE"

# Generate checksums and signatures for the POM file
echo "Generating checksums and GPG signature for $POM_FILE"
md5sum $POM_FILE | awk '{ print $1 }' > $POM_FILE.md5
sha1sum $POM_FILE | awk '{ print $1 }' > $POM_FILE.sha1
gpg --local-user $KEY_ID --armor --detach-sign $POM_FILE

# Verify checksums
echo "Verifying checksums"
for file in custom-log4j-appender-$VERSION.jar custom-log4j-appender-$VERSION-javadoc.jar custom-log4j-appender-$VERSION-sources.jar $POM_FILE; do
if [ -f "$file" ]; then
echo "Verifying checksum for $file"
md5sum -c <(echo "$(cat $file.md5) $file")
sha1sum -c <(echo "$(cat $file.sha1) $file")
else
echo "File $file does not exist in the target directory $TARGET_DIR."
ls -la $TARGET_DIR
fi
done

# Navigate back to the custom-log4j-appender directory
cd "$CURRENT_DIR"

# Create a ZIP file containing the entire directory structure
echo "Creating ZIP file custom-log4j-appender-$VERSION.zip"
zip -r custom-log4j-appender-$VERSION.zip io

echo "Artifacts prepared and zipped successfully. You can now upload custom-log4j-appender-$VERSION.zip to Sonatype OSSRH."

0 comments on commit 479b0e5

Please sign in to comment.