Skip to content

Commit 1fca9c5

Browse files
feat: create custom merger task (#45)
* feat: create custom merger task * pr remarks
1 parent 2da8545 commit 1fca9c5

File tree

12 files changed

+212
-7
lines changed

12 files changed

+212
-7
lines changed

plugins/edc-build/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
implementation(project(":plugins:autodoc:autodoc-plugin"))
1717
implementation(project(":plugins:test-summary"))
1818
implementation(project(":plugins:module-names"))
19+
implementation(project(":plugins:openapi-merger"))
1920

2021
implementation("com.autonomousapps:dependency-analysis-gradle-plugin:1.13.1")
2122
implementation("io.github.gradle-nexus:publish-plugin:1.1.0")

plugins/edc-build/src/main/java/org/eclipse/edc/plugins/edcbuild/EdcBuildBasePlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
package org.eclipse.edc.plugins.edcbuild;
1616

1717
import com.autonomousapps.DependencyAnalysisPlugin;
18-
import com.rameshkp.openapi.merger.gradle.plugin.OpenApiMergerGradlePlugin;
1918
import io.github.gradlenexus.publishplugin.NexusPublishPlugin;
2019
import org.eclipse.edc.plugins.autodoc.AutodocPlugin;
2120
import org.eclipse.edc.plugins.modulenames.ModuleNamesPlugin;
21+
import org.eclipse.edc.plugins.openapimerger.OpenApiMergerPlugin;
2222
import org.eclipse.edc.plugins.testsummary.TestSummaryPlugin;
2323
import org.gradle.api.Plugin;
2424
import org.gradle.api.Project;
@@ -49,7 +49,7 @@ private static void defineCapabilities(Project target) {
4949
if (target == target.getRootProject()) {
5050
target.getPlugins().apply(ChecksumPlugin.class);
5151
target.getPlugins().apply(NexusPublishPlugin.class);
52-
target.getPlugins().apply(OpenApiMergerGradlePlugin.class);
52+
target.getPlugins().apply(OpenApiMergerPlugin.class);
5353
target.getPlugins().apply(ModuleNamesPlugin.class);
5454
target.getPlugins().apply(DependencyAnalysisPlugin.class);
5555
}

plugins/edc-build/src/main/java/org/eclipse/edc/plugins/edcbuild/EdcBuildPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public void apply(Project target) {
7070
dependencyAnalysis(),
7171
tests(),
7272
jar(),
73-
7473
swagger()
7574
).forEach(c -> c.apply(project));
7675
});
7776

77+
7878
}
7979
}

plugins/edc-build/src/main/java/org/eclipse/edc/plugins/edcbuild/conventions/ConventionFunctions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import static java.util.Optional.ofNullable;
2323

24-
class ConventionFunctions {
24+
public class ConventionFunctions {
2525

2626
/**
2727
* Supplies a generic {@link GradleException} with the given message.
@@ -33,11 +33,11 @@ public static Supplier<GradleException> gradleException(String message) {
3333
/**
3434
* Supplies a {@link GradleException} that has a specific message indicating that a particular extension is missing.
3535
*/
36-
public static Supplier<GradleException> extensionException(Class<?> extensionClass) {
36+
static Supplier<GradleException> extensionException(Class<?> extensionClass) {
3737
return gradleException(extensionClass.getSimpleName() + " expected but was null");
3838
}
3939

40-
static <A> A requireExtension(Project target, Class<A> extensionClass) {
40+
public static <A> A requireExtension(Project target, Class<A> extensionClass) {
4141
return ofNullable(target.getExtensions().findByType(extensionClass))
4242
.orElseThrow(extensionException(extensionClass));
4343
}

plugins/edc-build/src/main/java/org/eclipse/edc/plugins/edcbuild/conventions/SwaggerGeneratorConvention.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import io.swagger.v3.plugins.gradle.tasks.ResolveTask;
1818
import org.eclipse.edc.plugins.edcbuild.extensions.BuildExtension;
19+
import org.eclipse.edc.plugins.edcbuild.tasks.PrintApiGroupTask;
1920
import org.gradle.api.Project;
2021
import org.gradle.api.plugins.JavaPlugin;
2122
import org.gradle.api.plugins.JavaPluginExtension;
@@ -42,6 +43,9 @@ public void apply(Project target) {
4243
}
4344

4445
target.getPluginManager().withPlugin("io.swagger.core.v3.swagger-gradle-plugin", appliedPlugin -> {
46+
47+
target.getTasks().register("apiGroups", PrintApiGroupTask.class);
48+
4549
target.getDependencies().add(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME,
4650
"io.swagger.core.v3:swagger-jaxrs2-jakarta:2.2.2");
4751
target.getDependencies().add(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2022 Microsoft Corporation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Microsoft Corporation - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.plugins.edcbuild.tasks;
16+
17+
import org.eclipse.edc.plugins.edcbuild.extensions.BuildExtension;
18+
import org.gradle.api.DefaultTask;
19+
import org.gradle.api.tasks.TaskAction;
20+
21+
import static org.eclipse.edc.plugins.edcbuild.conventions.ConventionFunctions.requireExtension;
22+
23+
24+
public class PrintApiGroupTask extends DefaultTask {
25+
26+
@TaskAction
27+
public void printApiGroup() {
28+
var buildExt = requireExtension(getProject(), BuildExtension.class);
29+
getProject().getLogger().lifecycle("API Group: {}", buildExt.getSwagger().getApiGroup().getOrElse(""));
30+
}
31+
}

plugins/openapi-merger/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module contains a gradle plugin that allows to merge partial OpenAPI spec files into one file.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
plugins {
2+
`java-gradle-plugin`
3+
id("org.gradle.crypto.checksum") version "1.4.0"
4+
}
5+
6+
val jupiterVersion: String by project
7+
val assertj: String by project
8+
val groupId: String by project
9+
10+
dependencies {
11+
// contains the actual merger task
12+
implementation("com.rameshkp:openapi-merger-gradle-plugin:1.0.4")
13+
// needed for the OpenApiDataInvalidException:
14+
implementation("com.rameshkp:openapi-merger-app:1.0.4")
15+
}
16+
17+
gradlePlugin {
18+
// Define the plugin
19+
plugins {
20+
create("openapi-merger") {
21+
displayName = "openapi-merger"
22+
description =
23+
"Plugin to several OpenAPI spec files into one"
24+
id = "${groupId}.openapi-merger"
25+
implementationClass = "org.eclipse.edc.plugins.openapimerger.OpenApiMergerPlugin"
26+
}
27+
}
28+
}
29+
30+
pluginBundle {
31+
website = "https://projects.eclipse.org/proposals/eclipse-dataspace-connector"
32+
vcsUrl = "https://github.com/eclipse-dataspaceconnector/GradlePlugins.git"
33+
version = version
34+
tags = listOf("build", "openapi", "merge", "documentation")
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2022 Microsoft Corporation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Microsoft Corporation - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.plugins.openapimerger;
16+
17+
import com.rameshkp.openapi.merger.gradle.plugin.OpenApiMergerGradlePlugin;
18+
import org.eclipse.edc.plugins.openapimerger.tasks.MergeApiSpecByPathTask;
19+
import org.gradle.api.Plugin;
20+
import org.gradle.api.Project;
21+
22+
/**
23+
* Custom grade plugin to avoid module name duplications.
24+
* Checks between modules with a gradle build file that their names are unique in the whole project.
25+
* `samples` and `system-tests` modules are excluded.
26+
* <p>
27+
* Ref: <a href="https://github.com/gradle/gradle/issues/847">Github Issue</a>
28+
*/
29+
public class OpenApiMergerPlugin implements Plugin<Project> {
30+
31+
@Override
32+
public void apply(Project project) {
33+
34+
if (project == project.getRootProject()) {
35+
project.getPlugins().apply(OpenApiMergerGradlePlugin.class);
36+
project.getTasks().register(MergeApiSpecByPathTask.NAME, MergeApiSpecByPathTask.class);
37+
}
38+
}
39+
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2022 Microsoft Corporation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Microsoft Corporation - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.plugins.openapimerger.tasks;
16+
17+
import com.rameshkp.openapi.merger.gradle.task.OpenApiMergerTask;
18+
import org.gradle.api.tasks.options.Option;
19+
20+
import java.io.File;
21+
22+
/**
23+
* Customization of the {@link OpenApiMergerTask}, which allows to pass in the input and output directories via command line.
24+
*/
25+
public class MergeApiSpecByPathTask extends OpenApiMergerTask {
26+
27+
public static final String NAME = "mergeApiSpec";
28+
29+
@Option(option = "output", description = "Output directory where the merged spec file is stores. Optional.")
30+
public void setOutputDir(String outputDirectory) {
31+
// inject the command line arg into the merger task's config
32+
getOutputFileProperty().set(new File(outputDirectory));
33+
}
34+
35+
@Option(option = "input", description = "Input directory where to look for partial specs. Required")
36+
public void setInputDir(String inputDir) {
37+
// inject the command line arg into the merger task's config
38+
getInputDirectory().set(new File(inputDir));
39+
}
40+
}
41+

0 commit comments

Comments
 (0)