使用 添加plugin
1 2 3 plugins { id 'maven-publish' }
添加extension
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 publishing { publications { maven(MavenPublication) { groupId 'com.miniapp.tools' artifactId 'sample' version '0.0.1' from components.java pom { } artifact { } versionMapping { } } myMaven(MavenPublication) { } } repositories { mavenLocal() maven { name 'remote' url mvn_repo credentials { username = mvn_username password = mvn_password } } } }
原理 初始化 publishing tasks
1 ./gradlew : amap_miniapp_compiler: tasks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 generateMetadataFileForMavenPublication - Generates the Gradle metadata file for publication 'maven' . generatePomFileForMavenPublication - Generates the Maven POM file for publication 'maven' . publish - Publishes all publications produced by this project. publishAllPublicationsToMavenLocalRepository - Publishes all Maven publications produced by this project to the MavenLocal repository. publishAllPublicationsToRemoteRepository - Publishes all Maven publications produced by this project to the remote repository. publishMavenPublicationToMavenLocal - Publishes Maven publication 'maven' to the local Maven repository. publishMavenPublicationToMavenLocalRepository - Publishes Maven publication 'maven' to Maven repository 'MavenLocal' . publishMavenPublicationToRemoteRepository - Publishes Maven publication 'maven' to Maven repository 'remote' . publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.
MavenPublishPlugin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 public class MavenPublishPlugin implements Plugin <Project > { @Override public void apply (final Project project) { project.getPluginManager().apply(PublishingPlugin.class); final TaskContainer tasks = project.getTasks(); tasks.register(PUBLISH_LOCAL_LIFECYCLE_TASK_NAME, publish -> { publish.setDescription("Publishes all Maven publications produced by this project to the local Maven cache." ); publish.setGroup(PublishingPlugin.PUBLISH_TASK_GROUP); }); project.getExtensions().configure(PublishingExtension.class, extension -> { extension.getPublications().registerFactory(MavenPublication.class, new MavenPublicationFactory( dependencyMetaDataProvider, instantiatorFactory.decorateLenient(), fileResolver, project.getPluginManager(), project.getExtensions())); realizePublishingTasksLater(project, extension); }); } private void realizePublishingTasksLater (final Project project, final PublishingExtension extension) { final NamedDomainObjectSet<MavenPublicationInternal> mavenPublications = extension.getPublications().withType(MavenPublicationInternal.class); final TaskContainer tasks = project.getTasks(); final DirectoryProperty buildDirectory = project.getLayout().getBuildDirectory(); final TaskProvider<Task> publishLifecycleTask = tasks.named(PublishingPlugin.PUBLISH_LIFECYCLE_TASK_NAME); final TaskProvider<Task> publishLocalLifecycleTask = tasks.named(PUBLISH_LOCAL_LIFECYCLE_TASK_NAME); final NamedDomainObjectList<MavenArtifactRepository> repositories = extension.getRepositories().withType(MavenArtifactRepository.class); repositories.all(repository -> tasks.register(publishAllToSingleRepoTaskName(repository), publish -> { publish.setDescription("Publishes all Maven publications produced by this project to the " + repository.getName() + " repository." ); publish.setGroup(PublishingPlugin.PUBLISH_TASK_GROUP); })); mavenPublications.all(publication -> { createGenerateMetadataTask(tasks, publication, mavenPublications, buildDirectory); createGeneratePomTask(tasks, publication, buildDirectory, project); createLocalInstallTask(tasks, publishLocalLifecycleTask, publication); createPublishTasksForEachMavenRepo(tasks, publishLifecycleTask, publication, repositories); }); } private String publishAllToSingleRepoTaskName (MavenArtifactRepository repository) { return "publishAllPublicationsTo" + capitalize(repository.getName()) + "Repository" ; } }
上面的代码主要是解析 publishing (PublishingExtension),并且根据我们设置的publications和repositories 构建task,构建的task类型,我们在上一部分中已经列出。
开头发现在apply方法中调用了PublishingPlugin的apply方法,我们继续查看PublishingPlugin
PublishingPlugin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class PublishingPlugin implements Plugin <Project > { @Override public void apply (final Project project) { RepositoryHandler repositories = publicationServices.createRepositoryHandler(); PublicationContainer publications = instantiator.newInstance(DefaultPublicationContainer.class, instantiator, collectionCallbackActionDecorator); PublishingExtension extension = project.getExtensions().create(PublishingExtension.class, PublishingExtension.NAME, DefaultPublishingExtension.class, repositories, publications); project.getTasks().register(PUBLISH_LIFECYCLE_TASK_NAME, task -> { task.setDescription("Publishes all publications produced by this project." ); task.setGroup(PUBLISH_TASK_GROUP); }); extension.getPublications().all(publication -> { PublicationInternal<?> internalPublication = Cast.uncheckedNonnullCast(publication); ProjectInternal projectInternal = (ProjectInternal) project; projectPublicationRegistry.registerPublication(projectInternal, internalPublication); }); bridgeToSoftwareModelIfNeeded((ProjectInternal) project); validatePublishingModelWhenComplete(project, extension); } }
初始化完成后,每个task都可以独立执行,这里分析publishMavenPublicationToMavenLocalRepository和publishMavenPublicationToRemoteRepository;构建task过程如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 private void createPublishTasksForEachMavenRepo (final TaskContainer tasks, final TaskProvider<Task> publishLifecycleTask, final MavenPublicationInternal publication, final NamedDomainObjectList<MavenArtifactRepository> repositories) { final String publicationName = publication.getName(); repositories.all(repository -> { final String repositoryName = repository.getName(); final String publishTaskName = "publish" + capitalize(publicationName) + "PublicationTo" + capitalize(repositoryName) + "Repository" ; tasks.register(publishTaskName, PublishToMavenRepository.class, publishTask -> { publishTask.setPublication(publication); publishTask.setRepository(repository); publishTask.setGroup(PublishingPlugin.PUBLISH_TASK_GROUP); publishTask.setDescription("Publishes Maven publication '" + publicationName + "' to Maven repository '" + repositoryName + "'." ); }); publishLifecycleTask.configure(task -> task.dependsOn(publishTaskName)); tasks.named(publishAllToSingleRepoTaskName(repository), publish -> publish.dependsOn(publishTaskName)); }); }
这里的代码是帮助构建task,并且让publish和publishAllxxxxxx 依赖这个task
Task执行 PublishToMavenRepository (publishMavenPublicationToMavenLocalRepository)
1 ./gradlew : amap_miniapp_compiler: publishMavenPublicationToMavenLocalRepository
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 abstract class AbstractMavenPublisher implements MavenPublisher { protected void publish (MavenNormalizedPublication publication, ExternalResourceRepository repository, URI rootUri, boolean localRepo) { String groupId = publication.getGroupId(); String artifactId = publication.getArtifactId(); String version = publication.getVersion(); ModuleArtifactPublisher artifactPublisher = new ModuleArtifactPublisher(repository, localRepo, rootUri, groupId, artifactId, version); if (isSnapshot(version)) { ... } if (publication.getMainArtifact() != null ) { artifactPublisher.publish(null , publication.getMainArtifact().getExtension(), publication.getMainArtifact().getFile()); } artifactPublisher.publish(null , "pom" , publication.getPomArtifact().getFile()); for (MavenArtifact artifact : publication.getAdditionalArtifacts()) { artifactPublisher.publish(artifact.getClassifier(), artifact.getExtension(), artifact.getFile()); } ExternalResourceName externalResource = artifactPublisher.getMetadataLocation(); Metadata metadata = createMetadata(groupId, artifactId, version, repository, externalResource); artifactPublisher.publish(externalResource, writeMetadataToTmpFile(metadata, "module-maven-metadata.xml" )); } }