Skip to content

Commit 9ec5d54

Browse files
committed
Add "Always transfer paths" function
1 parent 04632b6 commit 9ec5d54

File tree

7 files changed

+51
-12
lines changed

7 files changed

+51
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.2.0
2+
* Provide an option to always transfer specific paths
3+
* Always transfer Dockerfile - as it is required for building - by default
4+
15
# 1.1.1
26
* Migrated deployment to _Sonatype Maven Central Portal_ [#155](https://github.com/xdev-software/standard-maven-template/issues/155)
37

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.xdev</groupId>
88
<artifactId>testcontainers-advanced-imagebuilder-root</artifactId>
9-
<version>1.1.2-SNAPSHOT</version>
9+
<version>1.2.0-SNAPSHOT</version>
1010
<packaging>pom</packaging>
1111

1212
<organization>

testcontainers-advanced-imagebuilder-demo/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<parent>
88
<groupId>software.xdev</groupId>
99
<artifactId>testcontainers-advanced-imagebuilder-root</artifactId>
10-
<version>1.1.2-SNAPSHOT</version>
10+
<version>1.2.0-SNAPSHOT</version>
1111
</parent>
1212

1313
<artifactId>testcontainers-advanced-imagebuilder-demo</artifactId>
14-
<version>1.1.2-SNAPSHOT</version>
14+
<version>1.2.0-SNAPSHOT</version>
1515
<packaging>jar</packaging>
1616

1717
<organization>

testcontainers-advanced-imagebuilder-dummy-app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.xdev</groupId>
88
<artifactId>testcontainers-advanced-imagebuilder-dummy-app</artifactId>
9-
<version>1.1.2-SNAPSHOT</version>
9+
<version>1.2.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<organization>

testcontainers-advanced-imagebuilder/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.xdev</groupId>
88
<artifactId>testcontainers-advanced-imagebuilder</artifactId>
9-
<version>1.1.2-SNAPSHOT</version>
9+
<version>1.2.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>testcontainers-advanced-imagebuilder</name>

testcontainers-advanced-imagebuilder/src/main/java/software/xdev/testcontainers/imagebuilder/AdvancedImageFromDockerFile.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.List;
3131
import java.util.Map;
3232
import java.util.Map.Entry;
33+
import java.util.Objects;
3334
import java.util.Optional;
3435
import java.util.Set;
3536
import java.util.concurrent.CompletableFuture;
@@ -96,6 +97,8 @@ public class AdvancedImageFromDockerFile
9697
protected Optional<Path> baseDir = Optional.empty();
9798
protected Optional<Path> baseDirRelativeIgnoreFile = Optional.of(Paths.get(".gitignore"));
9899
protected List<String> additionalIgnoreLines = new ArrayList<>();
100+
protected boolean alwaysTransferDockerfilePath = true;
101+
protected Set<Path> alwaysTransferPaths = Set.of();
99102
protected Optional<String> target = Optional.empty();
100103
protected final Set<Consumer<BuildImageCmd>> buildImageCmdModifiers = new LinkedHashSet<>();
101104
protected Set<String> externalDependencyImageNames = Collections.emptySet();
@@ -326,7 +329,16 @@ protected void configure(final BuildImageCmd buildImageCmd)
326329
final TransferFilesCreator tfc =
327330
new TransferFilesCreator(this.baseDir.get(), this.baseDirRelativeIgnoreFile.orElse(null));
328331

329-
final List<Path> filesToTransfer = tfc.getFilesToTransfer(this.additionalIgnoreLines);
332+
final Set<Path> alwaysIncludePaths = new HashSet<>(this.alwaysTransferPaths);
333+
if(this.alwaysTransferDockerfilePath)
334+
{
335+
final Path determinedDockerfilePath = this.dockerFilePath.orElse(Path.of("Dockerfile"));
336+
alwaysIncludePaths.add(this.baseDir
337+
.map(basePath -> basePath.relativize(determinedDockerfilePath))
338+
.orElse(determinedDockerfilePath));
339+
}
340+
341+
final List<Path> filesToTransfer = tfc.getFilesToTransfer(this.additionalIgnoreLines, alwaysIncludePaths);
330342

331343
this.log().info("{}x files will be transferred", filesToTransfer.size());
332344
if(this.log().isDebugEnabled())
@@ -472,6 +484,18 @@ public AdvancedImageFromDockerFile withAdditionalIgnoreLines(final String... add
472484
return this;
473485
}
474486

487+
public AdvancedImageFromDockerFile withAlwaysTransferPaths(final Set<Path> alwaysTransferPaths)
488+
{
489+
this.alwaysTransferPaths = new HashSet<>(Objects.requireNonNull(alwaysTransferPaths));
490+
return this;
491+
}
492+
493+
public AdvancedImageFromDockerFile withAlwaysTransferDockerfilePath(final boolean alwaysTransferDockerfilePath)
494+
{
495+
this.alwaysTransferDockerfilePath = alwaysTransferDockerfilePath;
496+
return this;
497+
}
498+
475499
public AdvancedImageFromDockerFile withTarget(final String target)
476500
{
477501
this.target = Optional.of(target);

testcontainers-advanced-imagebuilder/src/main/java/software/xdev/testcontainers/imagebuilder/TransferFilesCreator.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ public class TransferFilesCreator
5454
private final Path baseDir;
5555
private final Path ignoreFileRelativeToBaseDir;
5656

57-
public TransferFilesCreator(final Path baseDir, final Path ignoreFileRelativeToBaseDir)
57+
public TransferFilesCreator(
58+
final Path baseDir,
59+
final Path ignoreFileRelativeToBaseDir)
5860
{
5961
this.baseDir = Objects.requireNonNull(baseDir);
6062
this.ignoreFileRelativeToBaseDir = ignoreFileRelativeToBaseDir;
6163
}
6264

63-
public List<Path> getFilesToTransfer(final Collection<String> additionalIgnoreLines)
65+
public List<Path> getFilesToTransfer(
66+
final Collection<String> additionalIgnoreLines,
67+
final Set<Path> alwaysIncludePaths)
6468
{
6569
try
6670
{
@@ -76,7 +80,7 @@ public List<Path> getFilesToTransfer(final Collection<String> additionalIgnoreLi
7680
.map(s -> FileSystems.getDefault().getPathMatcher("glob:" + s))
7781
.toList();
7882

79-
return this.walkFilesAndDetermineTransfer(ignoreMatchers);
83+
return this.walkFilesAndDetermineTransfer(ignoreMatchers, alwaysIncludePaths);
8084
}
8185
catch(final IOException ioe)
8286
{
@@ -115,15 +119,22 @@ else if(!s.startsWith("/") && !s.startsWith("*") && !s.startsWith("!"))
115119
.toList();
116120
}
117121

118-
protected List<Path> walkFilesAndDetermineTransfer(final List<PathMatcher> ignoreMatchers) throws IOException
122+
protected List<Path> walkFilesAndDetermineTransfer(
123+
final List<PathMatcher> ignoreMatchers,
124+
final Set<Path> alwaysIncludePaths) throws IOException
119125
{
120126
try(final Stream<Path> walk = Files.walk(this.baseDir))
121127
{
122128
return walk
123129
.filter(Files::isRegularFile)
124130
.filter(file -> {
125-
final Path relativePath = Paths.get("/").resolve(this.baseDir.relativize(file));
126-
return ignoreMatchers.stream().noneMatch(m -> m.matches(relativePath));
131+
final Path relativePath = this.baseDir.relativize(file);
132+
if(alwaysIncludePaths.contains(relativePath))
133+
{
134+
return true;
135+
}
136+
final Path rootRelativePath = Paths.get("/").resolve(relativePath);
137+
return ignoreMatchers.stream().noneMatch(m -> m.matches(rootRelativePath));
127138
})
128139
.toList();
129140
}

0 commit comments

Comments
 (0)