Skip to content

Commit 5dc3b36

Browse files
authored
Merge pull request #82 from jsonschema2dataclass/target-dir-fix
TargetDirectoryPrefix should be a folder
2 parents b869c8d + debc0b8 commit 5dc3b36

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Help wanted for this task
109109
| source | ConfigurableFileCollection | | Location of the JSON Schema file(s). Note: this may refer to a single file or a directory of files.
110110
| sourceSortOrder | String | OS | The sort order to be applied when recursively processing the source files. By default the OS can influence the processing order. Supported values: OS (Let the OS influence the order the source files are processed.) FILES_FIRST (Case sensitive sort, visit the files first. The source files are processed in a breadth first sort order.) SUBDIRS_FIRST (Case sensitive sort, visit the sub-directories before the files. The source files are processed in a depth first sort order.)
111111
| sourceType | String | jsonschema | The type of input documents that will be read Supported values: jsonschema (schema documents, containing formal rules that describe the structure of JSON data) json (documents that represent an example of the kind of JSON data that the generated Java types will be mapped to) yamlschema (JSON schema documents, represented as YAML) yaml (documents that represent an example of the kind of YAML (or JSON) data that the generated Java types will be mapped to)
112-
| targetDirecotryPrefix | String | generated/source/js2d | Directory prefix under build directory where underlying tool will generate sources |
112+
| targetDirecotryPrefix | String | $buildDir/generated/source/js2d | Directory prefix under build directory where underlying tool will generate sources
113113
| targetPackage | String | | Package name used for generated Java classes (for types where a fully qualified name has not been supplied in the schema using the 'javaType' property).
114114
| targetVersion | String | | The target version for generated source files.
115115
| timeType | String | | What type to use instead of string when adding string type fields of format time (not date-time) to generated Java types.

demo/androidDev/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jsonSchema2Pojo {
6565
//source = files("${sourceSets.main.output.resourcesDir}/json")
6666
source.setFrom files("${project.rootDir}/schema")
6767

68-
targetDirectoryPrefix = 'generated/source/jsonschema2dataclass'
68+
targetDirectoryPrefix = file("$buildDir/generated/source/jsonschema2dataclass")
6969
// Target directory for generated Java source files. The plugin will ignore this value for Android projects and will generate the classes into a generated source folder for each build variant and add this directory to the
7070
// java source set so the compiler will find and compile the newly generated source files.
7171
// targetDirectory = file("${project.rootDir}/build/generated-sources/js2p")

src/main/groovy/com/github/eirnym/js2p/JsonSchemaExtension.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ package com.github.eirnym.js2p
1717

1818
import groovy.transform.ToString
1919
import org.gradle.api.NamedDomainObjectContainer
20+
import org.gradle.api.file.DirectoryProperty
2021
import org.gradle.api.model.ObjectFactory
21-
import org.gradle.api.provider.Property
2222

2323
import javax.inject.Inject
2424

25-
import static com.github.eirnym.js2p.JsonSchemaPlugin.TARGET_FOLDER_BASE
25+
import static com.github.eirnym.js2p.JsonSchemaPlugin.DEFAULT_EXECUTION_NAME
2626

2727
@ToString
2828
class JsonSchemaExtension extends JsonSchema2PojoPluginConfiguration {
2929
final NamedDomainObjectContainer<JsonSchema2PojoPluginConfiguration> executions
30-
final Property<String> targetDirectoryPrefix
30+
final DirectoryProperty targetDirectoryPrefix
3131

3232
@Inject
3333
JsonSchemaExtension(ObjectFactory objectFactory) {
34-
super("main", objectFactory)
35-
targetDirectoryPrefix = objectFactory.property(String.class).convention(TARGET_FOLDER_BASE)
34+
super(DEFAULT_EXECUTION_NAME, objectFactory)
35+
targetDirectoryPrefix = objectFactory.directoryProperty()
3636
executions = objectFactory.domainObjectContainer(JsonSchema2PojoPluginConfiguration)
3737
}
3838
}

src/main/groovy/com/github/eirnym/js2p/JsonSchemaPlugin.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.gradle.api.Project
2323
import org.gradle.api.ProjectConfigurationException
2424
import org.gradle.api.Task
2525
import org.gradle.api.file.ConfigurableFileCollection
26+
import org.gradle.api.file.DirectoryProperty
2627
import org.gradle.api.model.ObjectFactory
2728
import org.gradle.api.plugins.JavaPluginConvention
2829
import org.gradle.util.GradleVersion
@@ -37,6 +38,7 @@ class JsonSchemaPlugin implements Plugin<Project> {
3738

3839
private static final String MINIMUM_GRADLE_VERSION = "6.0"
3940
public static String TARGET_FOLDER_BASE = 'generated/sources/js2d'
41+
public static String DEFAULT_EXECUTION_NAME = 'main'
4042
public static String TASK_NAME = 'generateJsonSchema2DataClass'
4143
public static String PLUGIN_ID = 'org.jsonschema2dataclass'
4244

@@ -46,6 +48,7 @@ class JsonSchemaPlugin implements Plugin<Project> {
4648

4749
project.extensions.create('jsonSchema2Pojo', JsonSchemaExtension)
4850
JsonSchemaExtension extension = project.extensions.getByType(JsonSchemaExtension)
51+
extension.targetDirectoryPrefix.convention(project.layout.buildDirectory.dir(TARGET_FOLDER_BASE))
4952
project.afterEvaluate {
5053
if (project.plugins.hasPlugin('java')) {
5154
applyJava(extension, project)
@@ -159,8 +162,9 @@ class JsonSchemaPlugin implements Plugin<Project> {
159162
project,
160163
taskNameSuffix,
161164
execId,
165+
execution.name,
162166
execution.source,
163-
extension.targetDirectoryPrefix.get(),
167+
extension.targetDirectoryPrefix,
164168
targetDirectorySuffix
165169
)
166170
postConfigure(task)
@@ -173,8 +177,9 @@ class JsonSchemaPlugin implements Plugin<Project> {
173177
Project project,
174178
String taskNameSuffix,
175179
int executionId,
180+
String executionName,
176181
ConfigurableFileCollection source,
177-
String targetDirectoryPrefix,
182+
DirectoryProperty targetDirectoryPrefix,
178183
String targetDirectorySuffix
179184
) {
180185

@@ -189,7 +194,7 @@ class JsonSchemaPlugin implements Plugin<Project> {
189194
task.execution = executionId
190195
task.sourceFiles.setFrom(source)
191196
task.targetDirectory.set(
192-
project.layout.buildDirectory.dir("${targetDirectoryPrefix}${executionId}${targetDirectorySuffix}")
197+
targetDirectoryPrefix.dir("${executionName}${targetDirectorySuffix}")
193198
)
194199
task.inputs.files({ task.sourceFiles.filter({ it.exists() }) })
195200
.skipWhenEmpty()

src/test/groovy/com/github/eirnym/js2p/JavaTaskFunctionalTest.groovy

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java.nio.file.Path
1616
import java.nio.file.Paths
1717
import java.util.stream.Stream
1818

19+
import static com.github.eirnym.js2p.JsonSchemaPlugin.DEFAULT_EXECUTION_NAME
1920
import static com.github.eirnym.js2p.JsonSchemaPlugin.TASK_NAME
2021
import static com.github.eirnym.js2p.JsonSchemaPlugin.TARGET_FOLDER_BASE
2122
import static com.github.eirnym.js2p.JsonSchemaPlugin.PLUGIN_ID
@@ -27,10 +28,17 @@ class JavaTaskFunctionalTest {
2728
private static final String COLON_TASK_NAME0 = COLON_TASK_NAME + '0'
2829
private static final String COLON_TASK_NAME1 = COLON_TASK_NAME + '1'
2930

30-
private static final String TARGET_FOLDER_BASE0 = TARGET_FOLDER_BASE + '0'
3131
private static final String TARGET_FOLDER_BASE_CUSTOM = TARGET_FOLDER_BASE + 's'
32-
private static final String TARGET_FOLDER_BASE_CUSTOM0 = TARGET_FOLDER_BASE_CUSTOM + '0'
33-
private static final String TARGET_FOLDER_BASE_CUSTOM1 = TARGET_FOLDER_BASE_CUSTOM + '1'
32+
33+
private static final String TARGET_FOLDER_CUSTOM = 'build/'+ TARGET_FOLDER_BASE_CUSTOM
34+
private static final String TARGET_FOLDER_DEFAULT = 'build/'+ TARGET_FOLDER_BASE
35+
private static final String PACKAGE_EMPTY = ''
36+
37+
private static final String EXECUTION_NAME_COM = 'com'
38+
private static final String PACKAGE_COM_EXAMPLE = 'com/example'
39+
40+
private static final String EXECUTION_NAME_ORG = 'org'
41+
private static final String PACKAGE_ORG_EXAMPLE = 'org/example'
3442
private static final List<String> GRADLE_RELEASES = [
3543
'6.8.3', '6.8', '6.7.1', '6.6.1', '6.5.1', '6.4.1', '6.3', '6.2.2', '6.2.1', '6.1.1', '6.0.1', // 6.x
3644
]
@@ -55,7 +63,11 @@ class JavaTaskFunctionalTest {
5563
def result = executeRunner(gradleVersion, testProjectDir)
5664

5765
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
58-
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE0}")
66+
addressJavaExists(
67+
testProjectDir,
68+
TARGET_FOLDER_DEFAULT,
69+
DEFAULT_EXECUTION_NAME,
70+
PACKAGE_EMPTY)
5971
}
6072

6173
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@@ -69,7 +81,11 @@ class JavaTaskFunctionalTest {
6981
def result = executeRunner(gradleVersion, testProjectDir)
7082

7183
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
72-
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE0}/com/example")
84+
addressJavaExists(
85+
testProjectDir,
86+
TARGET_FOLDER_DEFAULT,
87+
EXECUTION_NAME_COM,
88+
PACKAGE_COM_EXAMPLE)
7389
}
7490
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
7591
@NullSource
@@ -82,7 +98,11 @@ class JavaTaskFunctionalTest {
8298
def result = executeRunner(gradleVersion, testProjectDir)
8399

84100
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
85-
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE0}/com/example")
101+
addressJavaExists(
102+
testProjectDir,
103+
TARGET_FOLDER_DEFAULT,
104+
EXECUTION_NAME_COM,
105+
PACKAGE_COM_EXAMPLE)
86106
}
87107

88108
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@@ -96,7 +116,12 @@ class JavaTaskFunctionalTest {
96116
def result = executeRunner(gradleVersion, testProjectDir)
97117

98118
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
99-
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE0}/com/example")
119+
120+
addressJavaExists(
121+
testProjectDir,
122+
TARGET_FOLDER_DEFAULT,
123+
DEFAULT_EXECUTION_NAME,
124+
PACKAGE_COM_EXAMPLE)
100125
}
101126

102127
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@@ -112,8 +137,16 @@ class JavaTaskFunctionalTest {
112137
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
113138
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME1).outcome)
114139

115-
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE_CUSTOM0}/com/example")
116-
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE_CUSTOM1}/org/example")
140+
addressJavaExists(
141+
testProjectDir,
142+
TARGET_FOLDER_CUSTOM,
143+
EXECUTION_NAME_COM,
144+
PACKAGE_COM_EXAMPLE)
145+
addressJavaExists(
146+
testProjectDir,
147+
TARGET_FOLDER_CUSTOM,
148+
EXECUTION_NAME_ORG,
149+
PACKAGE_ORG_EXAMPLE)
117150
}
118151

119152
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@@ -188,7 +221,7 @@ class JavaTaskFunctionalTest {
188221
private void createBuildFilesMultiple() {
189222
Files.write(buildFile, (BUILD_FILE_HEADER + """
190223
|jsonSchema2Pojo{
191-
| targetDirectoryPrefix = "${TARGET_FOLDER_BASE_CUSTOM}"
224+
| targetDirectoryPrefix = project.file("\${buildDir}/${TARGET_FOLDER_BASE_CUSTOM}")
192225
| executions {
193226
| com{
194227
| targetPackage = 'com.example'
@@ -247,8 +280,8 @@ class JavaTaskFunctionalTest {
247280
assertTrue(file.exists())
248281
}
249282

250-
private static void addressJavaExists(Path testProjectDir, String targetDirectory) {
251-
def js2pDir = testProjectDir.resolve(targetDirectory)
283+
private static void addressJavaExists(Path testProjectDir, String targetDirectoryPrefix, String executionName, String subfolder) {
284+
def js2pDir = testProjectDir.resolve(targetDirectoryPrefix).resolve(executionName).resolve(subfolder)
252285
assertExists(js2pDir.toFile())
253286
assertExists(js2pDir.resolve("Address.java").toFile())
254287
}

0 commit comments

Comments
 (0)