Skip to content

Commit

Permalink
maven deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
datYori committed Jun 12, 2024
1 parent fb08e86 commit 6578a8c
Showing 1 changed file with 108 additions and 23 deletions.
131 changes: 108 additions & 23 deletions deps/others/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def updatePom(pomFile: File, newVersion: String): Unit = {
}

// Task to patch dependencies
val patchDependencies = taskKey[Unit]("Patch dependencies")
val patchAndLocalPublish = taskKey[Unit]("Patch dependencies")

patchDependencies := {
patchAndLocalPublish := {
val log = streams.value.log
val updateReport = update.value

Expand Down Expand Up @@ -114,12 +114,33 @@ patchDependencies := {
val copiedPomFile = new File(pomFile.getParent, s"$artifactID-$version-rawlabs.pom")
Files.copy(pomFile.toPath, copiedPomFile.toPath, StandardCopyOption.REPLACE_EXISTING)
log.info(s"Updated POM file for $artifactID with version $version-rawlabs")

val publishCommand = s"mvn install:install-file -Dfile=${newJarFile.getAbsolutePath} -DpomFile=${copiedPomFile.getAbsolutePath} -DgroupId=$groupID -DartifactId=$artifactID -Dversion=$version-rawlabs -Dpackaging=jar"
val localPublishCommand = s"""mvn install:install-file
|-Dfile=${newJarFile.getAbsolutePath}
|-DpomFile=${copiedPomFile.getAbsolutePath}
|-DgroupId=$groupID
|-DartifactId=$artifactID
|-Dversion=$version-rawlabs
|-Dpackaging=jar""".stripMargin.replaceAll("\n", " ")
val localPublishExitCode = localPublishCommand.!
if (localPublishExitCode == 0) {
log.info(s"Published locally patched JAR $newName with updated POM $artifactID-$version-rawlabs.")
} else {
log.error(s"Failed to publish locally JAR $newName and POM $artifactID-$version-rawlabs.")
}
val publishCommand = s"""mvn deploy:deploy-file
|-Dfile=${newJarFile.getAbsolutePath}
|-DpomFile=${copiedPomFile.getAbsolutePath}
|-DgroupId=$groupID
|-DartifactId=$artifactID
|-Dversion=$version-rawlabs
|-Dpackaging=jar
|-DrepositoryId=githubraw
|-Durl=https://maven.pkg.github.com/raw-labs/raw""".stripMargin.replaceAll("\n", " ")
val publishExitCode = publishCommand.!
if (publishExitCode == 0) {
log.info(s"Published patched JAR $newName with updated POM $artifactID-$version-rawlabs.")
} else {
log.error(s"Failed to run $publishCommand")
log.error(s"Failed to publish JAR $newName and POM $artifactID-$version-rawlabs.")
}
}
Expand All @@ -132,29 +153,93 @@ patchDependencies := {
}
}

// Task to patch dependencies
val patchAndLocalPublish = taskKey[Unit]("Patch dependencies")

val createS3SyncScript = taskKey[Unit]("Create a bash script for syncing dependencies to S3")
patchAndLocalPublish := {
val log = streams.value.log
val updateReport = update.value

updateReport.configurations.flatMap(_.modules).distinct.foreach { module =>
val groupID = module.module.organization
val artifactID = module.module.name
val version = module.module.revision
val scalaVersionSuffixPattern = "_2\\.\\d{1,2}".r
val normalizedArtifactId = scalaVersionSuffixPattern.replaceFirstIn(artifactID, "")
if (moduleNames.contains(normalizedArtifactId)) {
log.info(s"Found module $artifactID:$version")
module.artifacts.find(_._1.extension == "jar").foreach { case (_, jarFile) =>
try {
val pomPath = jarFile.getAbsolutePath.replace(".jar", ".pom")
val pomFile = new File(pomPath)
if (!pomFile.exists()) {
log.error(s"Expected POM file does not exist for $artifactID:$version at $pomPath")
} else {
log.info(s"Found POM file for $artifactID:$version at $pomPath")

val scriptFile = "s3-sync-deps.sh"
val newName = jarFile.getName.replace(".jar", "-rawlabs.jar")
val newJarFile = new File(jarFile.getParent, newName)
Files.copy(jarFile.toPath, newJarFile.toPath, StandardCopyOption.REPLACE_EXISTING)
log.info(s"Patched JAR file created: $newName")

createS3SyncScript := {
val dependencies = libraryDependencies.value
// Handling the manifest
val manifest = new File("manifest.txt")
val bw = new BufferedWriter(new FileWriter(manifest))
try {
bw.write(s"Automatic-Module-Name: ${moduleNames(normalizedArtifactId)}\n")
} finally {
bw.close()
}

val writer = new BufferedWriter(new FileWriter(scriptFile))
// Update the JAR file with the new manifest
val patchCommand = s"jar --update --file ${newJarFile.getAbsolutePath} --manifest=manifest.txt"
val patchExitCode = patchCommand.!
if (patchExitCode == 0) {
log.info(s"JAR file $newName patched successfully with new manifest.")
} else {
log.error(s"Failed to patch JAR file $newName with new manifest.")
}

try {
writer.write("#!/bin/bash\n\n")
dependencies.foreach { dep =>
writer.write(s"aws s3 sync $$M2_HOME s3://$$BUCKET/maven --exclude '*' --include '**${dep.name}**'\n")
// Update and publish the POM file
updatePom(pomFile, version)
val copiedPomFile = new File(pomFile.getParent, s"$artifactID-$version-rawlabs.pom")
Files.copy(pomFile.toPath, copiedPomFile.toPath, StandardCopyOption.REPLACE_EXISTING)
log.info(s"Updated POM file for $artifactID with version $version-rawlabs")
val localPublishCommand = s"""mvn install:install-file
|-Dfile=${newJarFile.getAbsolutePath}
|-DpomFile=${copiedPomFile.getAbsolutePath}
|-DgroupId=$groupID
|-DartifactId=$artifactID
|-Dversion=$version-rawlabs
|-Dpackaging=jar""".stripMargin.replaceAll("\n", " ")
val localPublishExitCode = localPublishCommand.!
if (localPublishExitCode == 0) {
log.info(s"Published locally patched JAR $newName with updated POM $artifactID-$version-rawlabs.")
} else {
log.error(s"Failed to publish locally JAR $newName and POM $artifactID-$version-rawlabs.")
}
val publishCommand = s"""mvn deploy:deploy-file
|-Dfile=${newJarFile.getAbsolutePath}
|-DpomFile=${copiedPomFile.getAbsolutePath}
|-DgroupId=$groupID
|-DartifactId=$artifactID
|-Dversion=$version-rawlabs
|-Dpackaging=jar
|-DrepositoryId=githubraw
|-Durl=https://maven.pkg.github.com/raw-labs/raw""".stripMargin.replaceAll("\n", " ")
val publishExitCode = publishCommand.!
if (publishExitCode == 0) {
log.info(s"Published patched JAR $newName with updated POM $artifactID-$version-rawlabs.")
} else {
log.error(s"Failed to run $publishCommand")
log.error(s"Failed to publish JAR $newName and POM $artifactID-$version-rawlabs.")
}
}
} catch {
case e: Exception =>
log.error(s"Error during the patching process for $artifactID:$version: ${e.getMessage}")
}
}
}
} finally {
// Always close the writer to release resources
writer.close()
}

// Make the script executable
new File(scriptFile).setExecutable(true)

// Notify that the task is completed
println(s"Bash script created: $scriptFile")
}
}

0 comments on commit 6578a8c

Please sign in to comment.