Skip to content

Commit 6cb9854

Browse files
committed
Add instruction for using Smithy CLI
1 parent 173a536 commit 6cb9854

1 file changed

Lines changed: 47 additions & 28 deletions

File tree

smithy-trait-codegen/README.md

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Smithy build plugin that generates Java trait classes from customized Smithy tra
2525

2626
## Configuration
2727

28-
The generator supports the following configuration options:
28+
The generator supports the following configuration options:
2929

3030
| Option | Description | Required |
3131
|--------|-------------|----------|
@@ -38,26 +38,55 @@ An example for `smithy-build.json`:
3838

3939
```json
4040
{
41-
"version": "1.0",
42-
"plugins": {
43-
"trait-codegen": {
44-
"package": "com.example.traits",
45-
"namespace": "com.example.traits",
46-
"header": ["Copyright Example Corp"],
47-
"excludeTags": ["internal"]
48-
}
41+
"version": "1.0",
42+
"plugins": {
43+
"trait-codegen": {
44+
"package": "com.example.traits",
45+
"namespace": "com.example.traits",
46+
"header": ["Copyright Example Corp"],
47+
"excludeTags": ["internal"]
4948
}
49+
}
5050
}
5151
```
5252

5353
## Getting Started
54+
### Configure `smithy-build.json`
55+
To generate Java code for your customized traits, you will add the `trait-codegen` plugin to the
56+
[plugin configuration](https://smithy.io/2.0/guides/smithy-build-json.html) in `smithy-build.json`:
5457

55-
First, create a gradle-based Smithy model project. You can also use Smithy CLI to generate a [complete custom trait example](https://github.com/smithy-lang/smithy-examples/tree/main/custom-trait-examples/custom-trait): `smithy init -o custom-trait -t custom-trait`.
58+
```json
59+
{
60+
"version": "1.0",
61+
"sources": ["model"],
62+
"plugins": {
63+
"trait-codegen": {
64+
"package": "io.examples.traits",
65+
"namespace": "example.traits"
66+
}
67+
}
68+
}
69+
```
70+
### Add dependency
5671

57-
> [!NOTE]
58-
> The generator currently cannot be run with non-gradle-based projects.
72+
#### **Using Smithy CLI**
5973

60-
In `build.gradle.kts`, add `id("software.amazon.smithy.gradle.smithy-trait-package").version("1.3.0")` to the `plugin` section.
74+
If you are using Smithy CLI, you will add dependency in `smithy-build.json`:
75+
```json
76+
{
77+
"version": "1.0",
78+
"sources": ["model"],
79+
"maven": {
80+
"dependencies": [
81+
"software.amazon.smithy:smithy-trait-codegen:1.61.0"
82+
]
83+
}
84+
"...": "..."
85+
}
86+
```
87+
Then you can run `smithy build` to build the model and generate Java code for your customized traits.
88+
#### **Using Gradle**
89+
If you are using Gradle, you will add dependency `id("software.amazon.smithy.gradle.smithy-trait-package").version("1.3.0")` to the `plugin` section in `build.gradle.kts`:
6190
```kotlin
6291
plugins {
6392
id("software.amazon.smithy.gradle.smithy-trait-package").version("1.3.0")
@@ -73,20 +102,10 @@ repositories {
73102
mavenCentral()
74103
}
75104
```
105+
Then you can run `gradle build` to build the model and generate Java code for your customized traits.
76106

77-
Next, add the `trait-codegen` plugin to the
78-
[plugin configuration](https://smithy.io/2.0/guides/smithy-build-json.html) in `smithy-build.json`.
79-
80-
```json
81-
"plugins": {
82-
"trait-codegen": {
83-
"package": "io.examples.traits",
84-
"namespace": "example.traits",
85-
}
86-
}
87-
```
88-
89-
Finally, build the model with Gradle: `gradle build` and you can find the the generated Java classes under `/build/smithyprojections/trait-codegen/`
107+
### Check the Generated Code
108+
Finally you can find the the generated Java classes under `/build/smithyprojections/trait-codegen/`
90109

91110
The generated Java class for `annotationTrait` from `custom-trait` template would be:
92111
```java
@@ -101,8 +120,8 @@ public final class AnnotationTrait extends AbstractTrait implements ToSmithyBuil
101120
@Override
102121
protected Node createNode() {
103122
return Node.objectNodeBuilder()
104-
.sourceLocation(getSourceLocation())
105-
.build();
123+
.sourceLocation(getSourceLocation())
124+
.build();
106125
}
107126

108127
public static AnnotationTrait fromNode(Node node) {

0 commit comments

Comments
 (0)