2015-02-23 64 views
0

我试图解决类似这里的问题解决了一个问题:https://github.com/pniederw/elastic-deps/blob/master/build.gradle如何在Gradle中的项目构造函数中指定配置?

从本质上讲,我有地图gradle这个项目的,以预先建好的产出,存储在一个名为PrebuiltArtifactMap.groovy(这是我从我的非生成文件gradle构建系统),并且我希望在项目地图具有合适的条目时将项目依赖项解析为项目依赖项,否则将其作为项目依赖项单独放置。

它工作正常,只要我使用我依赖的项目的默认配置,但是当我想使用指定的配置,我似乎无法使其工作。

我试图写:

def smartProject(String projectPath, String configuration=null) { 
    return PrebuiltArtifactMap.resolveProjectDep(project, projectPath, configuration) 
} 

class PrebuiltArtifactMap 
{ 
    static Map<String, String> projectToCoordinate = null; 


    static Object resolveDep(String mapKey, String itemKey) { 
     def map = projectToCoordinate[mapKey] 
     if (map == null) { 
      throw new GradleException("Prebuilt $mapKey doesn't exist.") 
     } 
     def artifact = map[itemKey] 
     return artifact 
    } 

    static Object resolveProjectDep(Project dependentProject, 
            String projectPath, 
            String configuration=null) { 
     // Attempt to locate gradle project in PrebuiltArtifactMap.groovy, 
     // and resolve it into an artifact dependency if present. 
     if (projectToCoordinate == null) { 
      if (configuration == null) { 
       return dependentProject.project(projectPath) 
      } else { 
       // *** THIS HERE doesn't work!! 
       return dependentProject.project(path: projectPath, 
               configuration: configuration) 
      } 
     } else { 
      def artifact = resolveDep('ProjectMap', projectPath) 
      if (artifact == null) { 
       throw new GradleException("Prebuilt ProjectMap doesn't define an artifact for $projectPath.") 
      } 
      println "Resolving ${projectPath} to ${artifact}" 
      if (artifact == 'LOCAL') { 
       if (configuration == null) { 
        return dependentProject.project(projectPath) 
       } else { 
        return dependentProject.project(path: projectPath, configuration: configuration) 
       } 
      } 
      return artifact 
     } 
    } 
} 

({ 
    def mapFile = file("PrebuiltArtifactMap.groovy") 
    if (mapFile.exists()) { 
     PrebuiltArtifactMap.projectToCoordinate = evaluate(mapFile) 
    } 
})() 

标记的部分***在这里,这不行!失败,因为Project对象没有这样的方法。

我想要做的是替换形式

dependencies { 
    myconfig project(":path:to:project", configuration: "archives") 
} 

与此的声明:

dependencies { 
    myconfig smartProject(":path:to:project", "archives") 
} 

什么是这样做的正确方法?

+0

注意,我可以通过强制人们使用“运行”作为相关项目的构件部分的配置解决的问题 - 其上引出了一个问题如何设置默认配置为“运行时”以外的东西... – 2015-02-23 21:50:18

+0

我不确定我完全理解这个问题。你能再详细一点吗?以下是我的理解:您想将方法'resolveProjectDep'附加到'Project' API?如果您可以在GitHub上创建Gist或示例项目来演示此问题,那也是有帮助的。 – 2015-02-27 00:10:35

+0

我添加了完整的代码示例,并希望澄清我的意图。 – 2015-02-27 18:37:34

回答

1

你在找什么是project方法DependencyHandler它返回Dependency。您可以通过方法getDependencies()Project实例访问DependencyHandler

在你的情况应该是这样的:

dependentProject.dependencies.project(path: projectPath, configuration: configuration) 
+0

谢谢...一旦你知道,这是如此简单:) – 2015-03-01 05:02:45

相关问题