From c3cc5ddad260c1c19db5ac2b73d67996860df7b2 Mon Sep 17 00:00:00 2001 From: Anton Tanasenko Date: Mon, 23 May 2016 14:52:43 +0300 Subject: [PATCH] Option to skip creation of aggregated output if input set is empty Signed-off-by: Anton Tanasenko --- .../aggregator/AggregatorBuildContext.java | 2 ++ .../internal/DefaultAggregatorBuildContext.java | 7 ++++++- .../aggregator/internal/DefaultInputSet.java | 10 +++++++++- .../internal/MavenAggregatorBuildContext.java | 5 +++++ .../spi/DefaultAggregatorBuildContextTest.java | 14 ++++++++++++++ 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/AggregatorBuildContext.java b/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/AggregatorBuildContext.java index 1521d4b4..a74139b4 100644 --- a/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/AggregatorBuildContext.java +++ b/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/AggregatorBuildContext.java @@ -27,4 +27,6 @@ public interface AggregatorBuildContext { public InputSet newInputSet(); + public InputSet newInputSet(boolean createOutputIfEmpty); + } diff --git a/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/internal/DefaultAggregatorBuildContext.java b/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/internal/DefaultAggregatorBuildContext.java index c5c2dc03..6a66a44d 100644 --- a/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/internal/DefaultAggregatorBuildContext.java +++ b/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/internal/DefaultAggregatorBuildContext.java @@ -38,7 +38,12 @@ public DefaultAggregatorBuildContext(Workspace workspace, File stateFile, @Override public DefaultInputSet newInputSet() { - return new DefaultInputSet(this); + return newInputSet(true); + } + + @Override + public DefaultInputSet newInputSet(boolean createOutputIfEmpty) { + return new DefaultInputSet(this, createOutputIfEmpty); } private File registerOutput(File outputFile) { diff --git a/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/internal/DefaultInputSet.java b/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/internal/DefaultInputSet.java index e8e3eb87..1bf77b34 100644 --- a/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/internal/DefaultInputSet.java +++ b/incrementalbuild/src/main/java/io/takari/incrementalbuild/aggregator/internal/DefaultInputSet.java @@ -17,9 +17,11 @@ public class DefaultInputSet implements InputSet { private final DefaultAggregatorBuildContext context; private final Set inputs = new LinkedHashSet<>(); + private final boolean createOutputIfEmpty; - DefaultInputSet(DefaultAggregatorBuildContext context) { + DefaultInputSet(DefaultAggregatorBuildContext context, boolean createOutputIfEmpty) { this.context = context; + this.createOutputIfEmpty = createOutputIfEmpty; } @Override @@ -44,12 +46,18 @@ public Iterable addInputs(File basedir, Collection includes, @Override public boolean aggregateIfNecessary(File outputFile, InputAggregator aggregator) throws IOException { + if (inputs.isEmpty() && !createOutputIfEmpty) { + return false; + } return context.aggregateIfNecessary(inputs, outputFile, aggregator); } @Override public boolean aggregateIfNecessary(File outputFile, MetadataAggregator aggregator) throws IOException { + if (inputs.isEmpty() && !createOutputIfEmpty) { + return false; + } return context.aggregateIfNecessary(inputs, outputFile, aggregator); } diff --git a/incrementalbuild/src/main/java/io/takari/incrementalbuild/maven/internal/MavenAggregatorBuildContext.java b/incrementalbuild/src/main/java/io/takari/incrementalbuild/maven/internal/MavenAggregatorBuildContext.java index df0e65e6..a5476d41 100644 --- a/incrementalbuild/src/main/java/io/takari/incrementalbuild/maven/internal/MavenAggregatorBuildContext.java +++ b/incrementalbuild/src/main/java/io/takari/incrementalbuild/maven/internal/MavenAggregatorBuildContext.java @@ -38,4 +38,9 @@ public InputSet newInputSet() { return provider.get().newInputSet(); } + @Override + public InputSet newInputSet(boolean createOutputIfEmpty) { + return provider.get().newInputSet(createOutputIfEmpty); + } + } diff --git a/incrementalbuild/src/test/java/io/takari/incrementalbuild/spi/DefaultAggregatorBuildContextTest.java b/incrementalbuild/src/test/java/io/takari/incrementalbuild/spi/DefaultAggregatorBuildContextTest.java index 50e67289..12bf8425 100644 --- a/incrementalbuild/src/test/java/io/takari/incrementalbuild/spi/DefaultAggregatorBuildContextTest.java +++ b/incrementalbuild/src/test/java/io/takari/incrementalbuild/spi/DefaultAggregatorBuildContextTest.java @@ -133,4 +133,18 @@ public void testEmpty() throws Exception { Assert.assertEquals(1, indexer.outputs.size()); } + @Test + public void testEmptyNoCreate() throws Exception { + File outputFile = new File(temp.getRoot(), "output"); + File basedir = temp.newFolder(); + + FileIndexer indexer = new FileIndexer(); + DefaultAggregatorBuildContext actx = newContext(); + DefaultInputSet output = actx.newInputSet(false); + output.addInputs(basedir, null, null); + output.aggregateIfNecessary(outputFile, indexer); + actx.commit(null); + Assert.assertFalse(outputFile.exists()); + Assert.assertEquals(0, indexer.outputs.size()); + } }