Skip to content

Commit

Permalink
Add the ability for JVM targets to refuse to allow themselves to be
Browse files Browse the repository at this point in the history
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/
  • Loading branch information
dturner-tw committed Oct 23, 2014
1 parent 39988f9 commit 97af5ed
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/python/pants/backend/jvm/targets/jvm_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions src/python/pants/backend/jvm/tasks/jvm_compile/jvm_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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, []))
Expand Down
11 changes: 11 additions & 0 deletions testprojects/src/java/com/pants/testproject/nocache/BUILD
Original file line number Diff line number Diff line change
@@ -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'],
)
12 changes: 12 additions & 0 deletions testprojects/src/java/com/pants/testproject/nocache/Goodbye.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
12 changes: 12 additions & 0 deletions testprojects/src/java/com/pants/testproject/nocache/Hello.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 97af5ed

Please sign in to comment.