admin管理员组

文章数量:1400360

Question

How can we overwrite the SMITHY_DEPENDENCY_MODE variable for smithyBuild Gradle task runs, to enable importing Smithy types from other packages?

Goal

I have two API Smithy model packages, and I would like the second ("child-api-models") to be able to reuse common Smithy types from the first ("parent-api-models").

Research and tests so far

.0/guides/smithy-build-json.html indicates that this can be done by updating your smithy-build.json file to include maven dependencies and repositories, where:

Defines Java Maven dependencies needed to build the model. Dependencies are used to bring in model imports, build plugins, validators, transforms, and other extensions.

However, when I add the parent Smithy models package as a dependency in my build.gradle.kts and smithy-build.json files, gradle build fails the smithyBuild task with error:

Task :smithyBuild FAILED

Running smithy build

SMITHY_DEPENDENCY_MODE is set to 'forbid', but the following Maven dependencies are defined in smithy-build.json: [REDACTED]. Dependencies are forbidden in this configuration.

SMITHY_DEPENDENCY_MODE doesn't appear in the Smithy docs, but from a GitHub code search it looks like Smithy expects values of "forbid", "standard", or "ignore".

This Smithy code comment indicates that "standard" is the correct value to enable importing Smithy models from dependencies.

The following all result in the same SMITHY_DEPENDENCY_MODE is set to 'forbid', but the following Maven dependencies are defined error:

  • Running export SMITHY_DEPENDENCY_MODE="standard" before gradle build
  • Running SMITHY_DEPENDENCY_MODE="standard" gradle build
  • Adding SMITHY_DEPENDENCY_MODE="standard" to gradle.properties
  • Adding the following config to build.gradle.kts:
tasks.named("smithyBuild") {
  doFirst {
    System.setProperty("SMITHY_DEPENDENCY_MODE", "standard")
  }
}

Running environment("SMITHY_DEPENDENCY_MODE", "standard") within the doFirst block results in the following error:

Script compilation error:

Line 121: environment("SMITHY_DEPENDENCY_MODE", "standard")

            ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:

                public inline fun <T : AbstractExecTask<*>> AbstractExecTask<TypeVariable(T)>.environment(vararg environmentVariables: Pair<String, Any?>): TypeVariable(T) defined in .gradle.kotlin.dsl

                public inline fun JavaExec.environment(vararg environmentVariables: Pair<String, Any?>): JavaExec defined in .gradle.kotlin.dsl

                public inline fun Test.environment(vararg environmentVariables: Pair<String, Any?>): Test defined in .gradle.kotlin.dsl

                public inline fun ProcessForkOptions.environment(vararg environmentVariables: Pair<String, Any?>): ProcessForkOptions defined in .gradle.kotlin.dsl

Hopefully there's something obvious I'm missing, since it seems like it should be a common scenario to have shared Smithy models between packages.

Question

How can we overwrite the SMITHY_DEPENDENCY_MODE variable for smithyBuild Gradle task runs, to enable importing Smithy types from other packages?

Goal

I have two API Smithy model packages, and I would like the second ("child-api-models") to be able to reuse common Smithy types from the first ("parent-api-models").

Research and tests so far

https://smithy.io/2.0/guides/smithy-build-json.html indicates that this can be done by updating your smithy-build.json file to include maven dependencies and repositories, where:

Defines Java Maven dependencies needed to build the model. Dependencies are used to bring in model imports, build plugins, validators, transforms, and other extensions.

However, when I add the parent Smithy models package as a dependency in my build.gradle.kts and smithy-build.json files, gradle build fails the smithyBuild task with error:

Task :smithyBuild FAILED

Running smithy build

SMITHY_DEPENDENCY_MODE is set to 'forbid', but the following Maven dependencies are defined in smithy-build.json: [REDACTED]. Dependencies are forbidden in this configuration.

SMITHY_DEPENDENCY_MODE doesn't appear in the Smithy docs, but from a GitHub code search it looks like Smithy expects values of "forbid", "standard", or "ignore".

This Smithy code comment indicates that "standard" is the correct value to enable importing Smithy models from dependencies.

The following all result in the same SMITHY_DEPENDENCY_MODE is set to 'forbid', but the following Maven dependencies are defined error:

  • Running export SMITHY_DEPENDENCY_MODE="standard" before gradle build
  • Running SMITHY_DEPENDENCY_MODE="standard" gradle build
  • Adding SMITHY_DEPENDENCY_MODE="standard" to gradle.properties
  • Adding the following config to build.gradle.kts:
tasks.named("smithyBuild") {
  doFirst {
    System.setProperty("SMITHY_DEPENDENCY_MODE", "standard")
  }
}

Running environment("SMITHY_DEPENDENCY_MODE", "standard") within the doFirst block results in the following error:

Script compilation error:

Line 121: environment("SMITHY_DEPENDENCY_MODE", "standard")

            ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:

                public inline fun <T : AbstractExecTask<*>> AbstractExecTask<TypeVariable(T)>.environment(vararg environmentVariables: Pair<String, Any?>): TypeVariable(T) defined in .gradle.kotlin.dsl

                public inline fun JavaExec.environment(vararg environmentVariables: Pair<String, Any?>): JavaExec defined in .gradle.kotlin.dsl

                public inline fun Test.environment(vararg environmentVariables: Pair<String, Any?>): Test defined in .gradle.kotlin.dsl

                public inline fun ProcessForkOptions.environment(vararg environmentVariables: Pair<String, Any?>): ProcessForkOptions defined in .gradle.kotlin.dsl

Hopefully there's something obvious I'm missing, since it seems like it should be a common scenario to have shared Smithy models between packages.

Share Improve this question asked Mar 24 at 6:50 Mark SaysonMark Sayson 4254 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Found a work-around that avoids the SMITHY_DEPENDENCY_MODE is set to 'forbid' errors.

Extracted common Smithy models to a Gradle package that has the following smithy-build.json content:

{
  "version": "1.0",
  "projections": {
    "package": {
    }
  }
}

the following gradle.properties content:

smithyVersion=1.49.0
smithyGradleVersion=1.0.0

the following settings.gradle.kts content:

pluginManagement {
  val smithyGradleVersion: String by settings

  plugins {
    id("software.amazon.smithy.gradle.smithy-jar").version(smithyGradleVersion)
  }

  repositories {
    mavenCentral()
  }
}

rootProject.name = "my-common-smithy-models"

the following build.gradle.kts content to enable publishing the package to GitHub Packages (you can use Maven Central or anything else depending on your use case):

description = "Common Smithy models for [...]"

plugins {
  `java-library`
  `maven-publish`
  id("software.amazon.smithy.gradle.smithy-jar")
}

repositories {
  mavenCentral()
}

smithy {
  // Use the `package` projection from smithy-build.json
  sourceProjection.set("package")
  // Set tags so other smithy packages can include this in their built packages
  tags.addAll("common")
}

dependencies {
  val smithyVersion: String by project

  smithyCli("software.amazon.smithy:smithy-cli:$smithyVersion")
  implementation("software.amazon.smithy:smithy-linters:$smithyVersion")
  implementation("software.amazon.smithy:smithy-model:$smithyVersion")
}

// Publish jar to GitHub Packages so can import into other repositories
publishing {
  repositories {
    maven {
      name = "GitHubPackages"
      url = uri("https://maven.pkg.github/[My Organization]/[Shared Smithy Models Repo Name]")
      credentials {
        username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_USERNAME")
        password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN")
      }
    }
  }

  publications {
    register<MavenPublication>("gpr") {
      groupId = "my.group.id"
      artifactId = "my-artifact-id"
      version = "1.0.0"

      from(components["java"])
    }
  }
}

and after running ./gradlew publish to publish the common Smithy models to GitHub Packages, I was able to successfully import the models into the specific service models package without issues.

In the child Smithy models package, I didn't need any changes to smithy-build.json, just merged the following into the build.gradle.kts file:

repositories {
  repositories {
    maven {
      url = uri("https://maven.pkg.github/[My Organization]/[Shared Smithy Models Repo Name]")
      credentials {
        username = project.findProperty("gpr.usr") as String? ?: System.getenv("GITHUB_USERNAME")
        password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN")
      }
    }
  }
}

dependencies {
  implementation("my.group.id:my-artifact-id:1.0.0")
}

At that point I was able to successfully reference the common Smithy models in the child Smithy package without issue.

本文标签: How to set SMITHYDEPENDENCYMODE in Gradle builds to enable Smithy dependenciesStack Overflow