|
9 | 9 |
|
10 | 10 | import org.gradle.api.Plugin;
|
11 | 11 | import org.gradle.api.Project;
|
12 |
| -import org.gradle.api.logging.Logger; |
13 |
| -import org.gradle.api.logging.Logging; |
14 | 12 | import org.gradle.api.publish.PublishingExtension;
|
15 | 13 | import org.gradle.api.publish.maven.MavenPublication;
|
16 |
| -import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; |
17 | 14 |
|
18 | 15 | import java.nio.file.Path;
|
19 | 16 | import org.gradle.api.Task;
|
| 17 | +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; |
20 | 18 |
|
21 | 19 | public class Publish implements Plugin<Project> {
|
22 | 20 |
|
23 |
| - private static final Logger LOGGER = Logging.getLogger(Publish.class); |
24 |
| - |
25 |
| - public final static String EXTENSION_NAME = "zipmavensettings"; |
| 21 | + // public final static String PLUGIN_ZIP_PUBLISH_POM_TASK = "generatePomFileForPluginZipPublication"; |
26 | 22 | public final static String PUBLICATION_NAME = "pluginZip";
|
27 | 23 | public final static String STAGING_REPO = "zipStaging";
|
28 |
| - public final static String PLUGIN_ZIP_PUBLISH_POM_TASK = "generatePomFileForPluginZipPublication"; |
29 |
| - public final static String LOCALMAVEN = "publishToMavenLocal"; |
30 | 24 | public final static String LOCAL_STAGING_REPO_PATH = "/build/local-staging-repo";
|
31 |
| - public String zipDistributionLocation = "/build/distributions/"; |
| 25 | + // TODO: Does the path ^^ need to use platform dependant file separators ? |
| 26 | + |
| 27 | + private boolean isZipPublicationPresent(Project project) { |
| 28 | + PublishingExtension pe = project.getExtensions().findByType(PublishingExtension.class); |
| 29 | + if (pe == null) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + return pe.getPublications().findByName(PUBLICATION_NAME) != null; |
| 33 | + } |
32 | 34 |
|
33 |
| - public static void configMaven(Project project) { |
| 35 | + private void addLocalMavenRepo(Project project) { |
34 | 36 | final Path buildDirectory = project.getRootDir().toPath();
|
35 |
| - project.getPluginManager().apply(MavenPublishPlugin.class); |
36 | 37 | project.getExtensions().configure(PublishingExtension.class, publishing -> {
|
37 | 38 | publishing.repositories(repositories -> {
|
38 | 39 | repositories.maven(maven -> {
|
39 | 40 | maven.setName(STAGING_REPO);
|
40 | 41 | maven.setUrl(buildDirectory.toString() + LOCAL_STAGING_REPO_PATH);
|
41 | 42 | });
|
42 | 43 | });
|
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + private void addZipArtifact(Project project) { |
| 48 | + project.getExtensions().configure(PublishingExtension.class, publishing -> { |
43 | 49 | publishing.publications(publications -> {
|
44 | 50 | MavenPublication mavenZip = (MavenPublication) publications.findByName(PUBLICATION_NAME);
|
45 |
| - |
46 |
| - if (mavenZip == null) { |
47 |
| - mavenZip = publications.create(PUBLICATION_NAME, MavenPublication.class); |
| 51 | + if (mavenZip != null) { |
| 52 | + mavenZip.artifact(project.getTasks().named("bundlePlugin")); |
48 | 53 | }
|
49 |
| - |
50 |
| - String groupId = mavenZip.getGroupId(); |
51 |
| - if (groupId == null) { |
52 |
| - // The groupId is not customized thus we get the value from "project.group". |
53 |
| - // See https://docs.gradle.org/current/userguide/publishing_maven.html#sec:identity_values_in_the_generated_pom |
54 |
| - groupId = getProperty("group", project); |
55 |
| - } |
56 |
| - |
57 |
| - String artifactId = project.getName(); |
58 |
| - String pluginVersion = getProperty("version", project); |
59 |
| - mavenZip.artifact(project.getTasks().named("bundlePlugin")); |
60 |
| - mavenZip.setGroupId(groupId); |
61 |
| - mavenZip.setArtifactId(artifactId); |
62 |
| - mavenZip.setVersion(pluginVersion); |
63 | 54 | });
|
64 | 55 | });
|
65 | 56 | }
|
66 | 57 |
|
67 |
| - static String getProperty(String name, Project project) { |
68 |
| - if (project.hasProperty(name)) { |
69 |
| - Object property = project.property(name); |
70 |
| - if (property != null) { |
71 |
| - return property.toString(); |
72 |
| - } |
73 |
| - } |
74 |
| - return null; |
75 |
| - } |
76 |
| - |
77 | 58 | @Override
|
78 | 59 | public void apply(Project project) {
|
| 60 | + project.getPluginManager().apply("nebula.maven-base-publish"); |
| 61 | + project.getPluginManager().apply(MavenPublishPlugin.class); |
79 | 62 | project.afterEvaluate(evaluatedProject -> {
|
80 |
| - configMaven(project); |
81 |
| - Task validatePluginZipPom = project.getTasks().findByName("validatePluginZipPom"); |
82 |
| - if (validatePluginZipPom != null) { |
83 |
| - project.getTasks().getByName("validatePluginZipPom").dependsOn("generatePomFileForNebulaPublication"); |
84 |
| - } |
85 |
| - Task publishPluginZipPublicationToZipStagingRepository = project.getTasks() |
86 |
| - .findByName("publishPluginZipPublicationToZipStagingRepository"); |
87 |
| - if (publishPluginZipPublicationToZipStagingRepository != null) { |
88 |
| - publishPluginZipPublicationToZipStagingRepository.dependsOn("generatePomFileForNebulaPublication"); |
| 63 | + if (isZipPublicationPresent(project)) { |
| 64 | + addLocalMavenRepo(project); |
| 65 | + addZipArtifact(project); |
| 66 | + Task validatePluginZipPom = project.getTasks().findByName("validatePluginZipPom"); |
| 67 | + if (validatePluginZipPom != null) { |
| 68 | + validatePluginZipPom.dependsOn("generatePomFileForNebulaPublication"); |
| 69 | + } |
| 70 | + Task publishPluginZipPublicationToZipStagingRepository = project.getTasks() |
| 71 | + .findByName("publishPluginZipPublicationToZipStagingRepository"); |
| 72 | + if (publishPluginZipPublicationToZipStagingRepository != null) { |
| 73 | + publishPluginZipPublicationToZipStagingRepository.dependsOn("generatePomFileForNebulaPublication"); |
| 74 | + } |
| 75 | + } else { |
| 76 | + project.getLogger() |
| 77 | + .warn(String.format("Plugin 'opensearch.pluginzip' is applied but no '%s' publication is defined.", PUBLICATION_NAME)); |
89 | 78 | }
|
90 | 79 | });
|
91 | 80 | }
|
|
0 commit comments