Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit b702e35

Browse files
Merge pull request #1 from michaelboyles/develop
v1.0.0
2 parents 00dae84 + 8caf6e8 commit b702e35

File tree

8 files changed

+379
-2
lines changed

8 files changed

+379
-2
lines changed

.github/workflows/maven.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Java CI with Maven
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
- master
8+
pull_request:
9+
branches:
10+
- develop
11+
- master
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Set up JDK 1.8
20+
uses: actions/setup-java@v1
21+
with:
22+
java-version: 1.8
23+
- name: Build with Maven
24+
run: mvn -B verify --file pom.xml -P run-its

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
*.iml
3+
target/

README.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
1-
# dgs-codegen-maven-plugin
2-
A Maven port of Netflix's DGS codegen gradle plugin
1+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/michaelboyles/dgs-codegen-maven-plugin/Java%20CI%20with%20Maven) ![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/michaelboyles/dgs-codegen-maven-plugin?sort=semver) ![License](https://img.shields.io/github/license/michaelboyles/dgs-codegen-maven-plugin)
2+
3+
A Maven port of Netflix's [DGS codegen gradle plugin](https://github.com/Netflix/dgs-codegen).
4+
The core code to generate the classes [already exists as its own module](https://github.com/Netflix/dgs-codegen/tree/master/graphql-dgs-codegen-core),
5+
so this is plugin is just a thin wrapper around that.
6+
7+
## Sample Usage
8+
9+
Add the following plugin to your Maven build:
10+
11+
```xml
12+
<plugin>
13+
<groupId>com.github.michaelboyles</groupId>
14+
<artifactId>dgs-codegen-maven-plugin</artifactId>
15+
<version>1.0.0</version>
16+
<executions>
17+
<execution>
18+
<goals>
19+
<goal>generate</goal>
20+
</goals>
21+
<configuration>
22+
<packageName>com.foo.bar</packageName>
23+
</configuration>
24+
</execution>
25+
</executions>
26+
</plugin>
27+
```
28+
29+
The dependency is available from [jitpack.io](https://jitpack.io/). You can use the following repo declaration:
30+
31+
```xml
32+
<pluginRepository>
33+
<id>jitpack.io</id>
34+
<url>https://jitpack.io</url>
35+
</pluginRepository>
36+
```

pom.xml

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github.michaelboyles</groupId>
8+
<artifactId>dgs-codegen-maven-plugin</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>maven-plugin</packaging>
11+
12+
<properties>
13+
<maven.compiler.source>8</maven.compiler.source>
14+
<maven.compiler.target>8</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<build>
19+
<pluginManagement>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-plugin-plugin</artifactId>
24+
<version>3.6.0</version>
25+
</plugin>
26+
</plugins>
27+
</pluginManagement>
28+
</build>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.apache.maven</groupId>
33+
<artifactId>maven-plugin-api</artifactId>
34+
<version>3.0</version>
35+
<scope>provided</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.apache.maven</groupId>
39+
<artifactId>maven-project</artifactId>
40+
<version>2.2.1</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.maven.plugin-tools</groupId>
45+
<artifactId>maven-plugin-annotations</artifactId>
46+
<version>3.4</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.netflix.graphql.dgs.codegen</groupId>
51+
<artifactId>graphql-dgs-codegen-core</artifactId>
52+
<version>4.1.3</version>
53+
</dependency>
54+
</dependencies>
55+
56+
<repositories>
57+
<repository> <!-- for graphql-dgs-codegen-core -->
58+
<id>jcenter</id>
59+
<url>https://jcenter.bintray.com/</url>
60+
</repository>
61+
</repositories>
62+
63+
<profiles>
64+
<profile>
65+
<id>run-its</id>
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-invoker-plugin</artifactId>
71+
<version>3.2.2</version>
72+
<executions>
73+
<execution>
74+
<id>integration-test</id>
75+
<goals>
76+
<goal>install</goal>
77+
<goal>integration-test</goal>
78+
<goal>verify</goal>
79+
</goals>
80+
<configuration>
81+
<goals>
82+
<goal>clean</goal>
83+
<goal>package</goal>
84+
</goals>
85+
<showErrors>true</showErrors>
86+
<localRepositoryPath>${project.build.directory}/.m2/</localRepositoryPath>
87+
</configuration>
88+
</execution>
89+
</executions>
90+
</plugin>
91+
</plugins>
92+
</build>
93+
</profile>
94+
</profiles>
95+
</project>

src/it/basicTest/pom.xml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.github.michaelboyles</groupId>
6+
<artifactId>dgs-codegen-maven-plugin-integration-test</artifactId>
7+
<version>1.0.0</version>
8+
<name>Integration test</name>
9+
<packaging>jar</packaging>
10+
<description>Integration test</description>
11+
12+
<properties>
13+
<maven.compiler.source>8</maven.compiler.source>
14+
<maven.compiler.target>8</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>com.github.michaelboyles</groupId>
22+
<artifactId>dgs-codegen-maven-plugin</artifactId>
23+
<version>@project.version@</version>
24+
<executions>
25+
<execution>
26+
<goals>
27+
<goal>generate</goal>
28+
</goals>
29+
<configuration>
30+
<packageName>com.github.michaeboyles.dgs.generated</packageName>
31+
</configuration>
32+
</execution>
33+
</executions>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>4.12</version>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type Person {
2+
name: String
3+
age: Int
4+
likesDogs: Boolean
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.michaelboyles.dgs.it;
2+
3+
import org.junit.Test;
4+
5+
import com.github.michaeboyles.dgs.generated.DgsConstants;
6+
import com.github.michaeboyles.dgs.generated.types.Person;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertTrue;
10+
11+
public class GeneratedTest {
12+
@Test
13+
public void constantName() {
14+
assertEquals("Person", DgsConstants.PERSON.TYPE_NAME);
15+
}
16+
17+
@Test
18+
public void builder() {
19+
Person person = Person.newBuilder()
20+
.name("Michael")
21+
.age(28)
22+
.likesDogs(true)
23+
.build();
24+
25+
assertEquals("Michael", person.getName());
26+
assertEquals(Integer.valueOf(28), person.getAge());
27+
assertTrue(person.getLikesDogs());
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.github.michaeboyles.dgs;
2+
3+
import com.netflix.graphql.dgs.codegen.CodeGen;
4+
import com.netflix.graphql.dgs.codegen.CodeGenConfig;
5+
import com.netflix.graphql.dgs.codegen.Language;
6+
import org.apache.maven.plugin.AbstractMojo;
7+
import org.apache.maven.plugins.annotations.LifecyclePhase;
8+
import org.apache.maven.plugins.annotations.Mojo;
9+
import org.apache.maven.plugins.annotations.Parameter;
10+
import org.apache.maven.project.MavenProject;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
import java.util.Collections;
17+
import java.util.HashMap;
18+
import java.util.HashSet;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Set;
22+
import java.util.stream.Collectors;
23+
24+
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
25+
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal", "MismatchedQueryAndUpdateOfCollection", "unused","MismatchedReadAndWriteOfArray"})
26+
public class GenerateMojo extends AbstractMojo
27+
{
28+
@Parameter(defaultValue = "${project.build.sourceDirectory}/../resources/schema")
29+
private File[] schemaPaths = {};
30+
31+
@Parameter(required = true)
32+
private String packageName;
33+
34+
@Parameter(defaultValue = "client")
35+
private String subPackageNameClient;
36+
37+
@Parameter(defaultValue = "datafetchers")
38+
private String subPackageNameDatafetchers;
39+
40+
@Parameter(defaultValue = "types")
41+
private String subPackageNameTypes;
42+
43+
// Gradle plugin also checks this: project.plugins.hasPlugin(KotlinPluginWrapper::class.java)
44+
// but I don't know the equivalent and it seems good enough
45+
@Parameter
46+
private String language = hasKotlinPluginWrapperClass() ? "KOTLIN" : "JAVA";
47+
48+
@Parameter
49+
private Map<String, String> typeMapping = new HashMap<>();
50+
51+
@Parameter
52+
private boolean generateBoxedTypes = false;
53+
54+
@Parameter
55+
private boolean generateClient = false;
56+
57+
@Parameter(defaultValue = "${project.build.directory}/generated-sources/annotations/")
58+
private File outputDir;
59+
60+
@Parameter(defaultValue = "${project.build.directory}/generated-examples")
61+
private File examplesOutputDir;
62+
63+
@Parameter
64+
private List<String> includeQueries = new ArrayList<>();
65+
66+
@Parameter
67+
private List<String> includeMutations = new ArrayList<>();
68+
69+
@Parameter
70+
private boolean skipEntityQueries = false;
71+
72+
@Parameter
73+
private boolean shortProjectionNames = false;
74+
75+
/* These are available in an unreleased version
76+
@Parameter
77+
private boolean generateDataTypes = true;
78+
@Parameter
79+
private int maxProjectionDepth = 10;
80+
*/
81+
82+
@Parameter(defaultValue = "${project}")
83+
private MavenProject project;
84+
85+
@Override
86+
public void execute() {
87+
88+
Set<File> schemaPaths = Arrays.stream(this.schemaPaths)
89+
.map(this::getCanonicalFile)
90+
.collect(Collectors.toSet());
91+
92+
for (File schemaPath : schemaPaths) {
93+
if (!schemaPath.exists()) {
94+
getLog().warn("Schema location " + schemaPath + " does not exist");
95+
}
96+
}
97+
98+
CodeGenConfig config = new CodeGenConfig(
99+
Collections.emptySet(),
100+
schemaPaths,
101+
getCanonicalFile(outputDir).toPath(),
102+
getCanonicalFile(examplesOutputDir).toPath(),
103+
true,
104+
packageName,
105+
subPackageNameClient,
106+
subPackageNameDatafetchers,
107+
subPackageNameTypes,
108+
Language.valueOf(language.toUpperCase()),
109+
generateBoxedTypes,
110+
generateClient,
111+
typeMapping,
112+
new HashSet<>(includeQueries),
113+
new HashSet<>(includeMutations),
114+
skipEntityQueries,
115+
shortProjectionNames
116+
);
117+
118+
getLog().info("Codegen config: " + config);
119+
120+
new CodeGen(config).generate();
121+
project.addCompileSourceRoot(getCanonicalFile(outputDir).getAbsolutePath());
122+
}
123+
124+
private File getCanonicalFile(File file) {
125+
try {
126+
return file.getCanonicalFile();
127+
} catch (IOException e) {
128+
throw new RuntimeException(e);
129+
}
130+
}
131+
132+
private boolean hasKotlinPluginWrapperClass() {
133+
try {
134+
getClass().getClassLoader().loadClass("org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper");
135+
return true;
136+
}
137+
catch (Exception e) {
138+
return false;
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)