Artifactory + Custom Gradle Plugin以编程方式

时间:2014-08-28 19:10:38

标签: groovy gradle artifactory

我正在编写一个自定义插件,以消除对以下内容的需求:

artifactory {
    contextUrl = "${artifactory_contextUrl}"   //The base Artifactory URL if not overridden by the publisher/resolver
    publish {
        repository {
            contextUrl = "${artifactory_contextUrl}"
            repoKey = 'android-dev'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true

        }
    }
    resolve {
        repository {
            contextUrl = "${artifactory_contextUrl}"
            repoKey = 'android-dev-distributions'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true

        }
    }
}

以下是我以编程方式执行此操作的方法:

 DefaultExtraPropertiesExtension ext = (((DefaultExtraPropertiesExtension) project.property("ext")));
        ext.setProperty("artifactory", new ArtifactoryDsl(contextUrl, publish, resolve));

这应该有效,对吗? ArtifactoryDsl 对象具有所有正确的字段(contextUrl,发布,解析及其各自的内部字段)。

这是错误:

14:46:49.752 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: java.lang.IllegalStateException:    Context URL cannot be empty
14:46:49.753 [ERROR] [org.gradle.BuildExceptionReporter]    at org.jfrog.build.client.ArtifactoryClientConfiguration.getContextUrl(ArtifactoryClientConfiguration.java:111)
14:46:49.754 [ERROR] [org.gradle.BuildExceptionReporter]    at org.jfrog.build.client.ArtifactoryClientConfiguration$PublisherHandler.getContextUrl(ArtifactoryClientConfiguration.java:225)
14:46:49.754 [ERROR] [org.gradle.BuildExceptionReporter]    at org.jfrog.gradle.plugin.artifactory.extractor.BuildInfoTask.prepareAndDeploy(BuildInfoTask.java:526)
14:46:49.755 [ERROR] [org.gradle.BuildExceptionReporter]    at org.jfrog.gradle.plugin.artifactory.extractor.BuildInfoTask.collectProjectBuildInfo(BuildInfoTask.java:440)
14:46:49.755 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
14:46:49.755 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:219)
14:46:49.756 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:212)
14:46:49.756 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:201)
14:46:49.757 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:533)
14:46:49.757 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:516)
14:46:49.758 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
14:46:49.777 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
14:46:49.778 [ERROR] [org.gradle.BuildExceptionReporter]    ... 66 more

3 个答案:

答案 0 :(得分:4)

不,这不会起作用。您的插件必须应用Artifactory插件,然后使用artifactory(Groovy)或project.artifactory(Java)获取project.getExtensions().getByName("artifactory")对象,然后配置该对象。

答案 1 :(得分:1)

对于任何好奇如何实现这一目标的人,我都是这样做的:

在build.gradle中,确保您可以访问Artifactory类:

compile "org.jfrog.buildinfo:build-info-extractor-gradle:2.2.4"

接下来在自定义插件的apply()方法中:

        // Set up plugins so we never need to add them to a build.gradle
        project.getPlugins().apply(MAVEN);
        project.getPlugins().apply(ARTIFACTORY);
        project.setGroup(GROUP);

        // Add Artifactory repo to the repositories
        project.getRepositories().maven(new ArtifactoryAction(contextUrl + ARTIFACTORY_REPO_ENDPOINT, user, pass));

        // We will define the plugin convention here so all of our libraries do not need to
        // declare the artifactory closure manually
        ArtifactoryPluginConvention pluginConvention =
                ArtifactoryPluginUtil.getArtifactoryConvention(project);
        pluginConvention.setContextUrl(contextUrl);

        PublisherConfig publisherConfig = new PublisherConfig(pluginConvention);
        publisherConfig.setContextUrl(contextUrl);
        pluginConvention.setPublisherConfig(publisherConfig);

        // Use reflection to access private field
        PublisherConfig.Repository repository = null;

        Field[] fields = PublisherConfig.class.getDeclaredFields();
        for(Field field : fields) {
            if(field.getName().equalsIgnoreCase("repository")) {
                try {
                    field.setAccessible(true);
                    repository = (PublisherConfig.Repository) field.get(publisherConfig);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if(repository != null) {
            repository.setPassword(pass);
            repository.setUsername(user);
            repository.setRepoKey(PUBLISHER_REPO_KEY);
            repository.setMavenCompatible(true);
        }

        GradleArtifactoryClientConfigUpdater.update(pluginConvention.getClientConfig(), project.getRootProject());

答案 2 :(得分:0)

非常感谢@agrosner,我也在寻找很长一段时间,您的回答挽救了我的生命!

对于任何想用groovy实现其插件的人,这是我根据先前的文章实现的:

PS:我正在使用org.jfrog.buildinfo:build-info-extractor-gradle:4.9.8mave-publish

PS 2:由于某种原因,有必要将Maven应用于根项目。否则,您将出现错误Extension of type 'PublishingExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension]

class Plugin implements Plugin<Project> {
    @Override
    void apply(Project project) {

        //Maven publication plugin needs to be applied to root also
        project.rootProject.pluginManager.apply "maven-publish"
        project.pluginManager.apply "maven-publish"

        project.plugins.apply "com.jfrog.artifactory"


        def artifactory_contextUrl = project.artifactory_contextUrl
        def artifactory_writer_user = project.artifactory_writer_user
        def artifactory_writer_password = project.artifactory_writer_password

        // We will define the plugin convention here so all of our libraries do not need to
        // declare the artifactory closure manually
        ArtifactoryPluginConvention pluginConvention = ArtifactoryPluginUtil.getArtifactoryConvention(project)
        //uses closures to set the configuration
        pluginConvention.artifactory {
            contextUrl = "${artifactory_contextUrl}"
            publish{
                repository {
                    repoKey = 'repo name here'
                    username = "${artifactory_writer_user}"
                    password = "${artifactory_writer_password}"
                    maven = true
                }
                defaults {
                    publications('plugin')
                }
            }
        }
//commits changes
GradleArtifactoryClientConfigUpdater.update(pluginConvention.clientConfig, project.rootProject)


    }
}