Skip to content

Commit

Permalink
Prefix pom.xml with apache
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Jan 24, 2024
1 parent b2ae0a3 commit 108df67
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 4 deletions.
1 change: 0 additions & 1 deletion .scalafix.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ DisableSyntax.noNulls = true
DisableSyntax.noReturns = true
DisableSyntax.noWhileLoops = true
DisableSyntax.noIsInstanceOf = true
DisableSyntax.noXml = true
DisableSyntax.noFinalVal = true
DisableSyntax.noFinalize = true
DisableSyntax.noValPatterns = true
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ if you want to set `apacheSonatypeDisclaimerFile` to point to a `DISCLAIMER` fil
* `apacheSonatypeCredentialsLogLevel`: The log level to be used when logging about potential problems in resolving
credentials, defaults to `Level.Debug`. If you are trying to diagnose issues with resolving credentials then increase
the log level to either `Level.Warn` or `Level.Error`.
* `apacheSonatypeArtifactNamePrefix`: Apache guidelines mandate that the `<name>` field in the POM xml has to have a
specific prefix.

### Keys for Github Actions

Expand Down
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ inThisBuild(
)
)
)

libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.17" % Test
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ trait ApacheSonatypeKeys {
settingKey[File]("The NOTICE file which needs to be included in published artifact")
lazy val apacheSonatypeDisclaimerFile: SettingKey[Option[File]] =
settingKey[Option[File]]("The DISCLAIMER file which can optionally be included in published artifact")

lazy val apacheSonatypeArtifactNamePrefix: SettingKey[Option[String]] =
settingKey[Option[String]]("A prefix that will be added before the artifact name")
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ object ApacheSonatypePlugin extends AutoPlugin {
apacheSonatypeCredentialsPasswordEnvVar.value
)
},
apacheSonatypeCredentialsLogLevel := Level.Debug
apacheSonatypeCredentialsLogLevel := Level.Debug,
apacheSonatypeArtifactNamePrefix := Some("apache")
)

private[sonatype] lazy val sbtSonatypeBuildSettings: Seq[Setting[_]] = Seq(
Expand All @@ -54,6 +55,21 @@ object ApacheSonatypePlugin extends AutoPlugin {

private[sonatype] lazy val baseDir = LocalRootProject / baseDirectory

import scala.xml._

private[sonatype] def prefixNameInPom(prefix: String): Node => Node = (pomXml: Node) =>
new transform.RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case node @ projectNode if projectNode.label == "project" =>
val modifiedChildren = projectNode.child.collect {
case <name>{currentName}</name> => <name>{s"$prefix-$currentName"}</name>
case other => other
}
Elem(node.prefix, node.label, node.attributes, node.scope, false, modifiedChildren: _*)
case other => other
}
}.transform(pomXml).head

private[sonatype] lazy val sbtMavenBuildSettings: Seq[Setting[_]] = Seq(
credentials ++= {
val log = sLog.value
Expand All @@ -68,7 +84,14 @@ object ApacheSonatypePlugin extends AutoPlugin {
},
organization := s"org.apache.${apacheSonatypeProjectProfile.value}",
organizationName := apacheSonatypeOrganizationName.value,
organizationHomepage := Some(apacheSonatypeOrganizationHomePage.value)
organizationHomepage := Some(apacheSonatypeOrganizationHomePage.value),
pomPostProcess := {
apacheSonatypeArtifactNamePrefix.value match {
case Some(prefix) =>
pomPostProcess.value andThen prefixNameInPom(prefix)
case None => pomPostProcess.value
}
}
)

private[sonatype] lazy val sbtMavenProjectSettings: Seq[Setting[_]] = Seq(
Expand Down
9 changes: 9 additions & 0 deletions src/sbt-test/sbt-apache-sonatype/simple/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ThisBuild / scalaVersion := "2.13.10"
ThisBuild / apacheSonatypeProjectProfile := "project"
name := "some-name"

TaskKey[Unit]("check-organization") := {
val org = "org.apache.project"
Expand Down Expand Up @@ -60,3 +61,11 @@ TaskKey[Unit]("extract-packageDoc-contents") := {

IO.unzip(sourcesJar, targetDir)
}

TaskKey[Unit]("check-artifact-name") := {
val file = makePom.value
val xml = scala.xml.XML.loadFile(file)
val nameNode = (xml \ "name").text
if (nameNode != "apache-some-name")
sys.error(s"expected artifact name to be apache-some-name, instead got ${nameNode}")
}
2 changes: 2 additions & 0 deletions src/sbt-test/sbt-apache-sonatype/simple/test
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ $ must-mirror target/scala-2.13/packageDoc/META-INF/NOTICE NOTICE
> checkPomIncludeRepository
> checkPomLicenseField
> checkPublishToField
> checkArtifactName

73 changes: 73 additions & 0 deletions src/test/resources/after.pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.pekko</groupId>
<artifactId>pekko-stream_3</artifactId>
<packaging>jar</packaging>
<description>Apache Pekko is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.</description>
<url>https://pekko.apache.org/</url>
<version>1.0.2</version>
<licenses>
<license>
<name>Apache-2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<name>apache-pekko-stream</name>
<inceptionYear>2022</inceptionYear>
<organization>
<name>Apache Software Foundation</name>
<url>https://www.apache.org</url>
</organization>
<scm>
<url>https://github.com/apache/incubator-pekko</url>
<connection>scm:git:https://github.com/apache/incubator-pekko.git</connection>
<developerConnection>scm:git:[email protected]:apache/incubator-pekko.git</developerConnection>
</scm>
<developers>
<developer>
<id>pekko-contributors</id>
<name>Apache Pekko Contributors</name>
<url>https://github.com/apache/incubator-pekko/graphs/contributors</url>
<email>[email protected]</email>
</developer>
</developers>
<properties>
<info.apiURL>https://pekko.apache.org/api/pekko/1.0.2</info.apiURL>
<info.versionScheme>semver-spec</info.versionScheme>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.pekko</groupId>
<artifactId>pekko-actor_3</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.pekko</groupId>
<artifactId>pekko-protobuf-v3_3</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_3</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>ssl-config-core_3</artifactId>
<version>0.6.1</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_3</artifactId>
<version>3.2.14</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
73 changes: 73 additions & 0 deletions src/test/resources/before.pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.pekko</groupId>
<artifactId>pekko-stream_3</artifactId>
<packaging>jar</packaging>
<description>Apache Pekko is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.</description>
<url>https://pekko.apache.org/</url>
<version>1.0.2</version>
<licenses>
<license>
<name>Apache-2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<name>pekko-stream</name>
<inceptionYear>2022</inceptionYear>
<organization>
<name>Apache Software Foundation</name>
<url>https://www.apache.org</url>
</organization>
<scm>
<url>https://github.com/apache/incubator-pekko</url>
<connection>scm:git:https://github.com/apache/incubator-pekko.git</connection>
<developerConnection>scm:git:[email protected]:apache/incubator-pekko.git</developerConnection>
</scm>
<developers>
<developer>
<id>pekko-contributors</id>
<name>Apache Pekko Contributors</name>
<url>https://github.com/apache/incubator-pekko/graphs/contributors</url>
<email>[email protected]</email>
</developer>
</developers>
<properties>
<info.apiURL>https://pekko.apache.org/api/pekko/1.0.2</info.apiURL>
<info.versionScheme>semver-spec</info.versionScheme>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.pekko</groupId>
<artifactId>pekko-actor_3</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.pekko</groupId>
<artifactId>pekko-protobuf-v3_3</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_3</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>ssl-config-core_3</artifactId>
<version>0.6.1</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_3</artifactId>
<version>3.2.14</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mdedetrich.apache.sonatype

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers

import scala.xml._

class ApacheSonatypePluginSpec extends AnyFlatSpec with Matchers {
"ApacheSonatypePlugin" must "replace node in prefixNameInPom" in {
val before = XML.load(getClass.getResource("/before.pom.xml").getPath)
val after = XML.load(getClass.getResource("/after.pom.xml").getPath)

ApacheSonatypePlugin.prefixNameInPom("apache")(before).text mustEqual after.text
}
}

0 comments on commit 108df67

Please sign in to comment.