Ejemplo n.º 1
0
  def build_pants_refs(self, deps):
    # HACK This is horrible but works around a circular dependency.
    from pom_utils import PomUtils
    pants_refs = []
    for dep in deps:
      dep_target = "{groupId}.{artifactId}".format(groupId=dep['groupId'],
                                                   artifactId=dep['artifactId'])
      if PomUtils.is_local_dep(dep_target):
        # find the POM that contains this artifact
        poms = self._pom_provides_target.find_target(dep_target)
        if len(poms) > 0:
          project_root = os.path.dirname(poms[0])
        else:
          project_root = dep['artifactId']
        if project_root in self.exclude_project_targets:
          continue
        if dep.has_key('type') and dep['type'] == 'test-jar':
          target_prefix = 'src/test/'
        else:
          target_prefix = "src/main/"
        target_name = self.get_closest_match(project_root, target_prefix)
        if target_name:
          pants_refs.append("'{0}'".format(target_name))

    # Print 3rdparty dependencies after the local deps
    for dep in deps:
      dep_target = "{groupId}.{artifactId}".format(groupId=dep['groupId'],
                                                   artifactId=dep['artifactId'])
      if PomUtils.is_local_dep(dep_target):
        continue
      is_in_thirdparty = PomUtils.is_third_party_dep(dep_target, rootdir=self._rootdir)
      if is_in_thirdparty and not dep.get('exclusions'):
        logger.debug("dep_target {target} is not local".format(target=dep_target))
        pants_refs.append("'3rdparty:{target}'".format(target=dep_target))
        continue
      if not dep.has_key('version'):
        if is_in_thirdparty:
          dep['version'] = PomUtils.third_party_dep_targets(rootdir=self._rootdir)[dep_target]
        else:
          raise Exception(
            "Expected artifact {artifactId} group {groupId} in pom {pom_file} to have a version."
            .format(artifactId=dep['artifactId'],
                    groupId=dep['groupId'],
                    pom_file=self._source_file_name))
      jar_excludes = []
      for jar_exclude in dep.get('exclusions', ()):
        jar_excludes.append("exclude(org='{groupId}', name='{artifactId}')".format(
          groupId=jar_exclude['groupId'], artifactId=jar_exclude['artifactId']))

      classifier = dep.get('classifier') # Important to use 'get', so we default to None.
      if dep.get('type') == 'test-jar':
        # In our repo, this is special, *or* ivy doesn't translate this correctly.
        # Transform this into a classifier named 'tests' instead
        classifier = 'tests'
        type_ = None
      else:
        type_ = dep.get('type')

      dep_url = dep.get('systemPath')
      if dep_url:
        dep_url = 'file://{}'.format(dep_url)

      pants_refs.append(Target.jar.format(
        org=dep['groupId'],
        name=dep['artifactId'],
        rev=dep['version'],
        classifier=classifier,
        type_=type_,
        url=dep_url,
        excludes=jar_excludes or None,
      ))
    return pants_refs