|
| 1 | +package org.shipkit.internal.gradle.android; |
| 2 | + |
| 3 | +import com.jfrog.bintray.gradle.BintrayExtension; |
| 4 | + |
| 5 | +import org.gradle.api.GradleException; |
| 6 | +import org.gradle.api.Plugin; |
| 7 | +import org.gradle.api.Project; |
| 8 | +import org.gradle.api.Task; |
| 9 | +import org.gradle.api.component.SoftwareComponent; |
| 10 | +import org.gradle.api.logging.Logger; |
| 11 | +import org.gradle.api.logging.Logging; |
| 12 | +import org.gradle.api.publish.maven.MavenPublication; |
| 13 | +import org.shipkit.gradle.configuration.AndroidPublishConfiguration; |
| 14 | +import org.shipkit.gradle.configuration.ShipkitConfiguration; |
| 15 | +import org.shipkit.internal.gradle.configuration.ShipkitConfigurationPlugin; |
| 16 | +import org.shipkit.internal.gradle.snapshot.LocalSnapshotPlugin; |
| 17 | +import org.shipkit.internal.gradle.util.GradleDSLHelper; |
| 18 | +import org.shipkit.internal.gradle.util.PomCustomizer; |
| 19 | + |
| 20 | +import static org.shipkit.internal.gradle.configuration.DeferredConfiguration.deferredConfiguration; |
| 21 | +import static org.shipkit.internal.gradle.java.JavaPublishPlugin.MAVEN_LOCAL_TASK; |
| 22 | +import static org.shipkit.internal.gradle.java.JavaPublishPlugin.PUBLICATION_NAME; |
| 23 | + |
| 24 | +/** |
| 25 | + * Publishing Android libraries using 'maven-publish' plugin. |
| 26 | + * Intended to be applied in individual Android library submodule. |
| 27 | + * Applies following plugins and tasks and configures them: |
| 28 | + * |
| 29 | + * <ul> |
| 30 | + * <li>maven-publish</li> |
| 31 | + * </ul> |
| 32 | + * |
| 33 | + * Other features: |
| 34 | + * <ul> |
| 35 | + * <li>Configures Gradle's publications to publish Android library</li> |
| 36 | + * <li>Configures 'build' task to depend on 'publishJavaLibraryToMavenLocal' |
| 37 | + * to flesh out publication issues during the build</li> |
| 38 | + * <li>Configures 'snapshot' task to depend on 'publishJavaLibraryToMavenLocal'</li> |
| 39 | + * </ul> |
| 40 | + */ |
| 41 | +public class AndroidPublishPlugin implements Plugin<Project> { |
| 42 | + |
| 43 | + private final static Logger LOG = Logging.getLogger(AndroidPublishPlugin.class); |
| 44 | + private final static String ANDROID_PUBLISH_EXTENSION = "androidPublish"; |
| 45 | + |
| 46 | + public void apply(final Project project) { |
| 47 | + final AndroidPublishConfiguration androidPublishConfiguration = project.getExtensions().create(ANDROID_PUBLISH_EXTENSION, AndroidPublishConfiguration.class); |
| 48 | + |
| 49 | + final ShipkitConfiguration conf = project.getPlugins().apply(ShipkitConfigurationPlugin.class).getConfiguration(); |
| 50 | + |
| 51 | + project.getPlugins().apply(LocalSnapshotPlugin.class); |
| 52 | + Task snapshotTask = project.getTasks().getByName(LocalSnapshotPlugin.SNAPSHOT_TASK); |
| 53 | + snapshotTask.dependsOn(MAVEN_LOCAL_TASK); |
| 54 | + |
| 55 | + project.getPlugins().apply("maven-publish"); |
| 56 | + |
| 57 | + BintrayExtension bintray = project.getExtensions().getByType(BintrayExtension.class); |
| 58 | + bintray.setPublications(PUBLICATION_NAME); |
| 59 | + |
| 60 | + project.getPlugins().withId("com.android.library", plugin -> { |
| 61 | + deferredConfiguration(project, () -> { |
| 62 | + GradleDSLHelper.publications(project, publications -> { |
| 63 | + MavenPublication p = publications.create(PUBLICATION_NAME, MavenPublication.class, publication -> { |
| 64 | + publication.setArtifactId(androidPublishConfiguration.getArtifactId()); |
| 65 | + |
| 66 | + SoftwareComponent releaseComponent = project.getComponents().findByName("release"); |
| 67 | + if (releaseComponent == null) { |
| 68 | + throw new GradleException("'release' component not found in project. " + |
| 69 | + "Make sure you are using Android Gradle Plugin 3.6.0-beta05 or newer."); |
| 70 | + } |
| 71 | + publication.from(releaseComponent); |
| 72 | + PomCustomizer.customizePom(project, conf, publication); |
| 73 | + }); |
| 74 | + LOG.info("{} - configured '{}' publication", project.getPath(), p.getArtifactId()); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + //so that we flesh out problems with maven publication during the build process |
| 79 | + project.getTasks().getByName("build").dependsOn(MAVEN_LOCAL_TASK); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
0 commit comments