Example #1
0
  def execute(self):
    jar_import_products = self.context.products.get_data(JarImportProducts,
                                                         init_func=JarImportProducts)

    # Gather all targets that are both capable of importing jars and actually declare some imports.
    targets = self.context.targets(predicate=self.has_imports)
    if not targets:
      return

    # Create a list of all of these targets plus the list of JarDependencies they depend on.
    all_targets = set(targets)
    for target in targets:
      all_targets.update(target.imported_targets)

    imports_classpath = ClasspathProducts(self.get_options().pants_workdir)
    self.resolve(executor=self.create_java_executor(),
                 targets=all_targets,
                 classpath_products=imports_classpath,
                 invalidate_dependents=True)

    for target in targets:
      cp_entries = imports_classpath.get_classpath_entries_for_targets(target.closure(bfs=True))
      for conf, cp_entry in cp_entries:
        if isinstance(cp_entry, ArtifactClasspathEntry):
          jar_import_products.imported(target, cp_entry.coordinate, cp_entry.path)
Example #2
0
    def execute(self):
        jar_import_products = self.context.products.get_data(
            JarImportProducts, init_func=JarImportProducts)

        # Gather all targets that are both capable of importing jars and actually declare some imports.
        targets = self.context.targets(predicate=self.has_imports)
        if not targets:
            return

        # Create a list of all of these targets plus the list of JarDependencies they depend on.
        all_targets = set(targets)
        for target in targets:
            all_targets.update(target.imported_jar_libraries)

        imports_classpath = ClasspathProducts(self.get_options().pants_workdir)
        self.resolve(executor=self.create_java_executor(),
                     targets=all_targets,
                     classpath_products=imports_classpath,
                     invalidate_dependents=True)

        for target in targets:
            cp_entries = imports_classpath.get_classpath_entries_for_targets(
                target.closure(bfs=True))
            for conf, cp_entry in cp_entries:
                if isinstance(cp_entry, ArtifactClasspathEntry):
                    jar_import_products.imported(target, cp_entry.coordinate,
                                                 cp_entry.path)
    def test_get_classpath_entries_for_targets_intransitive(self):
        b = self.make_target('b',
                             JvmTarget,
                             excludes=[Exclude('com.example', 'lib')])
        a = self.make_target('a', JvmTarget, dependencies=[b])

        classpath_product = ClasspathProducts(self.pants_workdir)
        example_jar_path = self._example_jar_path()
        resolved_jar = self.add_jar_classpath_element_for_path(
            classpath_product, a, example_jar_path)

        classpath_product.add_for_target(
            b, [('default', self.path('b/loose/classes/dir'))])
        classpath_product.add_for_target(
            a, [('default', self.path('a/loose/classes/dir')),
                ('default', self.path('an/internally/generated.jar'))])

        classpath = classpath_product.get_classpath_entries_for_targets([a])
        self.assertEqual(
            [('default',
              ArtifactClasspathEntry(example_jar_path, resolved_jar.coordinate,
                                     resolved_jar.cache_path)),
             ('default', ClasspathEntry(self.path('a/loose/classes/dir'))),
             ('default',
              ClasspathEntry(self.path('an/internally/generated.jar')))],
            classpath)
Example #4
0
 def _bootstrap_classpath(self, jvm_tool, targets):
     self._check_underspecified_tools(jvm_tool, targets)
     self.context.log.debug(f"Bootstrapping {jvm_tool.key}")
     classpath_holder = ClasspathProducts(self.get_options().pants_workdir)
     CoursierMixin.resolve(
         self, targets, classpath_holder, sources=False, javadoc=False, executor=None
     )
     return [
         cp_entry for _, cp_entry in classpath_holder.get_classpath_entries_for_targets(targets)
     ]
  def test_get_classpath_entries_for_targets_respect_excludes(self):
    a = self.make_target('a', JvmTarget, excludes=[Exclude('com.example', 'lib')])

    classpath_product = ClasspathProducts()
    example_jar_path = self._example_jar_path()
    self.add_jar_classpath_element_for_path(classpath_product, a, example_jar_path)
    self.add_excludes_for_targets(classpath_product, a)

    classpath = classpath_product.get_classpath_entries_for_targets([a])

    self.assertEqual([], classpath)
    def test_get_classpath_entries_for_targets_dedup(self):
        b = self.make_target("b", JvmTarget)
        a = self.make_target("a", JvmTarget, dependencies=[b])
        classpath_product = ClasspathProducts(self.pants_workdir)
        example_jar_path = self._example_jar_path()

        # resolved_jar is added for both a and b but should return only as one classpath entry
        resolved_jar = self.add_jar_classpath_element_for_path(classpath_product, a, example_jar_path, conf="fred-conf")
        self.add_jar_classpath_element_for_path(classpath_product, b, example_jar_path, conf="fred-conf")
        classpath_target_tuples = classpath_product.get_classpath_entries_for_targets([a], respect_excludes=False)

        expected_entry = ArtifactClasspathEntry(example_jar_path, resolved_jar.coordinate, resolved_jar.cache_path)
        self.assertEqual([("fred-conf", expected_entry)], classpath_target_tuples)
  def test_get_classpath_entries_for_targets_ignore_excludes(self):
    a = self.make_target('a', JvmTarget, excludes=[Exclude('com.example', 'lib')])

    classpath_product = ClasspathProducts()
    example_jar_path = self._example_jar_path()
    resolved_jar = self.add_jar_classpath_element_for_path(classpath_product, a, example_jar_path,
                                                           conf='fred-conf')
    self.add_excludes_for_targets(classpath_product, a)

    classpath = classpath_product.get_classpath_entries_for_targets([a], respect_excludes=False)

    expected_entry = ArtifactClasspathEntry(example_jar_path,
                                            resolved_jar.coordinate,
                                            resolved_jar.cache_path)
    self.assertEqual([('fred-conf', expected_entry)], list(classpath))
Example #8
0
  def test_get_classpath_entries_for_targets_dedup(self):
    b = self.make_target('b', JvmTarget)
    a = self.make_target('a', JvmTarget, dependencies=[b])
    classpath_product = ClasspathProducts(self.pants_workdir)
    example_jar_path = self._example_jar_path()

    # resolved_jar is added for both a and b but should return only as one classpath entry
    resolved_jar = self.add_jar_classpath_element_for_path(classpath_product, a, example_jar_path,
                                                           conf='fred-conf')
    self.add_jar_classpath_element_for_path(classpath_product, b, example_jar_path,
                                            conf='fred-conf')
    classpath_target_tuples = classpath_product.get_classpath_entries_for_targets([a], respect_excludes=False)

    expected_entry = ArtifactClasspathEntry(example_jar_path,
                                            resolved_jar.coordinate,
                                            resolved_jar.cache_path)
    self.assertEqual([('fred-conf', expected_entry)], classpath_target_tuples)
  def test_get_classpath_entries_for_targets_intransitive(self):
    b = self.make_target('b', JvmTarget, excludes=[Exclude('com.example', 'lib')])
    a = self.make_target('a', JvmTarget, dependencies=[b])

    classpath_product = ClasspathProducts()
    example_jar_path = self._example_jar_path()
    resolved_jar = self.add_jar_classpath_element_for_path(classpath_product, a, example_jar_path)

    classpath_product.add_for_target(b, [('default', self.path('b/loose/classes/dir'))])
    classpath_product.add_for_target(a, [('default', self.path('a/loose/classes/dir')),
                                         ('default', self.path('an/internally/generated.jar'))])

    classpath = classpath_product.get_classpath_entries_for_targets([a], transitive=False)
    self.assertEqual([('default', ArtifactClasspathEntry(example_jar_path,
                                                         resolved_jar.coordinate,
                                                         resolved_jar.cache_path)),
                      ('default', ClasspathEntry(self.path('a/loose/classes/dir'))),
                      ('default', ClasspathEntry(self.path('an/internally/generated.jar')))],
                     classpath)
Example #10
0
 def _bootstrap_classpath(self, jvm_tool, targets):
     self._check_underspecified_tools(jvm_tool, targets)
     self.context.log.debug(f"Bootstrapping {jvm_tool.key}")
     classpath_holder = ClasspathProducts(self.get_options().pants_workdir)
     if JvmResolveSubsystem.global_instance().get_options(
     ).resolver == "ivy":
         self.resolve(executor=None,
                      targets=targets,
                      classpath_products=classpath_holder)
     else:
         CoursierMixin.resolve(self,
                               targets,
                               classpath_holder,
                               sources=False,
                               javadoc=False,
                               executor=None)
     return [
         cp_entry for _, cp_entry in
         classpath_holder.get_classpath_entries_for_targets(targets)
     ]
Example #11
0
    def execute(self):
        deprecated_conditional(
            lambda: True,
            removal_version="1.30.0.dev0",
            entity_description="The `imports` goal",
            hint_message="Contact the Pants team on Slack or [email protected] "
            "if you need this functionality.",
        )
        jar_import_products = self.context.products.get_data(
            JarImportProducts, init_func=JarImportProducts
        )

        # Gather all targets that are both capable of importing jars and actually declare some imports.
        targets = self.context.targets(predicate=self.has_imports)
        if not targets:
            return

        # Create a list of all of these targets plus the list of JarDependencies they depend on.
        all_targets = set(targets)
        for target in targets:
            all_targets.update(target.imported_targets)

        imports_classpath = ClasspathProducts(self.get_options().pants_workdir)
        self.resolve(
            targets=all_targets,
            compile_classpath=imports_classpath,
            sources=False,
            javadoc=False,
            executor=self.create_java_executor(),
        )

        for target in targets:
            cp_entries = imports_classpath.get_classpath_entries_for_targets(
                target.closure(bfs=True)
            )
            for conf, cp_entry in cp_entries:
                if isinstance(cp_entry, ArtifactClasspathEntry):
                    jar_import_products.imported(target, cp_entry.coordinate, cp_entry.path)