2014-08-28 90 views
0

我寫一個自定義插件,以消除需要: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)); 

這應該工作,對不對?對象具有所有正確的字段(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

不,這是行不通的。您的插件必須應用Artifactory插件,然後使用project.artifactory(Groovy)或project.getExtensions().getByName("artifactory")(Java)獲取artifactory對象,然後配置該對象。

1

對於任何人誰是好奇,如何實現這一目標,我做了這種方式:

在你的build.gradle確保您可以訪問Artifactory的類:

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

下一頁您的自定義插件的應用()方法:

 // 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());