From 97af5ed27e850726b02fff4c23bf8472ced1cba3 Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 23 Oct 2014 18:08:47 -0400 Subject: [PATCH] Add the ability for JVM targets to refuse to allow themselves to be cached in the artifact cache. Sometimes, we'll later generate resources that ought to be added to an artifact (e.g. args-apt in commons). In the long run, the artifact cache will be able to cache resources and this will be moot. Testing Done: Ran new tests. Reviewed at https://rbcommons.com/s/twitter/r/1206/ --- .../pants/backend/jvm/targets/jvm_target.py | 5 +++++ .../jvm/tasks/jvm_compile/jvm_compile.py | 10 +++++++--- .../java/com/pants/testproject/nocache/BUILD | 11 +++++++++++ .../pants/testproject/nocache/Goodbye.java | 12 ++++++++++++ .../com/pants/testproject/nocache/Hello.java | 12 ++++++++++++ .../java/test_java_compile_integration.py | 19 +++++++++++++++++++ 6 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 testprojects/src/java/com/pants/testproject/nocache/BUILD create mode 100644 testprojects/src/java/com/pants/testproject/nocache/Goodbye.java create mode 100644 testprojects/src/java/com/pants/testproject/nocache/Hello.java diff --git a/src/python/pants/backend/jvm/targets/jvm_target.py b/src/python/pants/backend/jvm/targets/jvm_target.py index 31a9065a7b2..ccb6650fe2b 100644 --- a/src/python/pants/backend/jvm/targets/jvm_target.py +++ b/src/python/pants/backend/jvm/targets/jvm_target.py @@ -39,6 +39,7 @@ def __init__(self, excludes=None, resources=None, configurations=None, + no_cache=False, **kwargs): """ :param configurations: One or more ivy configurations to resolve for this target. @@ -49,6 +50,7 @@ def __init__(self, :param sources: Source code files to build. Paths are relative to the BUILD file's directory. :type sources: ``Fileset`` (from globs or rglobs) or list of strings + :param no_cache: If True, this should not be stored in the artifact cache """ if sources_rel_path is None: sources_rel_path = address.spec_path @@ -61,8 +63,11 @@ def __init__(self, 'configurations': ConfigurationsField(self.assert_list(configurations)), }) self._resource_specs = self.assert_list(resources) + super(JvmTarget, self).__init__(address=address, payload=payload, **kwargs) self.add_labels('jvm') + if no_cache: + self.add_labels('no_cache') _jar_dependencies = None @property diff --git a/src/python/pants/backend/jvm/tasks/jvm_compile/jvm_compile.py b/src/python/pants/backend/jvm/tasks/jvm_compile/jvm_compile.py index 9e12c6d3d5e..1f9514309e2 100644 --- a/src/python/pants/backend/jvm/tasks/jvm_compile/jvm_compile.py +++ b/src/python/pants/backend/jvm/tasks/jvm_compile/jvm_compile.py @@ -562,13 +562,15 @@ def post_process_cached_vts(cached_vts): def _write_to_artifact_cache(self, analysis_file, vts, sources_by_target): vt_by_target = dict([(vt.target, vt) for vt in vts.versioned_targets]) + vts_targets = [t for t in vts.targets if not t.has_label('no_cache')] + split_analysis_files = [ - JvmCompile._analysis_for_target(self._analysis_tmpdir, t) for t in vts.targets] + JvmCompile._analysis_for_target(self._analysis_tmpdir, t) for t in vts_targets] portable_split_analysis_files = [ - JvmCompile._portable_analysis_for_target(self._analysis_tmpdir, t) for t in vts.targets] + JvmCompile._portable_analysis_for_target(self._analysis_tmpdir, t) for t in vts_targets] # Set up args for splitting the analysis into per-target files. - splits = zip([sources_by_target.get(t, []) for t in vts.targets], split_analysis_files) + splits = zip([sources_by_target.get(t, []) for t in vts_targets], split_analysis_files) splits_args_tuples = [(analysis_file, splits)] # Set up args for rebasing the splits. @@ -578,6 +580,8 @@ def _write_to_artifact_cache(self, analysis_file, vts, sources_by_target): vts_artifactfiles_pairs = [] classes_by_source = self._compute_classes_by_source(analysis_file) for target, sources in sources_by_target.items(): + if target.has_label('no_cache'): + continue artifacts = [] for source in sources: artifacts.extend(classes_by_source.get(source, [])) diff --git a/testprojects/src/java/com/pants/testproject/nocache/BUILD b/testprojects/src/java/com/pants/testproject/nocache/BUILD new file mode 100644 index 00000000000..79a3a95136d --- /dev/null +++ b/testprojects/src/java/com/pants/testproject/nocache/BUILD @@ -0,0 +1,11 @@ +# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). +# Licensed under the Apache License, Version 2.0 (see LICENSE). + +java_library(name='nocache', + sources = ['Hello.java'], + no_cache = True +) + +java_library(name='cache_me', + sources = ['Goodbye.java'], +) diff --git a/testprojects/src/java/com/pants/testproject/nocache/Goodbye.java b/testprojects/src/java/com/pants/testproject/nocache/Goodbye.java new file mode 100644 index 00000000000..00c588f8212 --- /dev/null +++ b/testprojects/src/java/com/pants/testproject/nocache/Goodbye.java @@ -0,0 +1,12 @@ +// Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). +// Licensed under the Apache License, Version 2.0 (see LICENSE). + +package com.pants.testproject.nocache; + + +public class Goodbye { + + public static void main(String[] args) { + System.out.println("This one is OK to cache."); + } +} diff --git a/testprojects/src/java/com/pants/testproject/nocache/Hello.java b/testprojects/src/java/com/pants/testproject/nocache/Hello.java new file mode 100644 index 00000000000..844f8ff5e25 --- /dev/null +++ b/testprojects/src/java/com/pants/testproject/nocache/Hello.java @@ -0,0 +1,12 @@ +// Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). +// Licensed under the Apache License, Version 2.0 (see LICENSE). + +package com.pants.testproject.nocache; + + +public class Hello { + + public static void main(String[] args) { + System.out.println("For some reason, we don't want to cache this in the build cache"); + } +} diff --git a/tests/python/pants_test/tasks/jvm_compile/java/test_java_compile_integration.py b/tests/python/pants_test/tasks/jvm_compile/java/test_java_compile_integration.py index a0f4de99ae5..4e7210f1439 100644 --- a/tests/python/pants_test/tasks/jvm_compile/java/test_java_compile_integration.py +++ b/tests/python/pants_test/tasks/jvm_compile/java/test_java_compile_integration.py @@ -38,6 +38,25 @@ def test_java_compile_produces_valid_analysis_file_second_time(self): self._java_compile_produces_valid_analysis_file(workdir) self._java_compile_produces_valid_analysis_file(workdir) + def test_nocache(self): + with temporary_dir() as cache_dir: + bad_artifact_dir = os.path.join(cache_dir, 'JavaCompile', + 'testprojects.src.java.com.pants.testproject.nocache.nocache') + good_artifact_dir = os.path.join(cache_dir, 'JavaCompile', + 'testprojects.src.java.com.pants.testproject.nocache.cache_me') + config = {'java-compile': {'write_artifact_caches': [cache_dir]}} + + pants_run = self.run_pants( + ['goal', 'compile', 'testprojects/src/java/com/pants/testproject/nocache::'], + config) + self.assert_success(pants_run) + + # The nocache target is labeled with no_cache so it should not be written to the + # artifact cache. + self.assertFalse(os.path.exists(bad_artifact_dir)) + # But cache_me should be written. + self.assertEqual(len(os.listdir(good_artifact_dir)), 1) + def test_java_compile_produces_different_artifact_depending_on_java_version(self): # Ensure that running java compile with java 6 and then java 7 # produces two different artifacts.