|
2 | 2 |
|
3 | 3 | import java.nio.file.Path; |
4 | 4 | import java.nio.file.Paths; |
| 5 | +import java.time.Duration; |
5 | 6 | import java.util.List; |
6 | | -import java.util.concurrent.TimeUnit; |
7 | | -import java.util.concurrent.TimeoutException; |
8 | 7 |
|
9 | 8 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
10 | 9 | import org.slf4j.LoggerFactory; |
|
16 | 15 |
|
17 | 16 | public final class Application |
18 | 17 | { |
19 | | - @SuppressWarnings({"java:S106", "PMD.SystemPrintln"}) // Just a demo |
| 18 | + @SuppressWarnings({ |
| 19 | + "java:S106", |
| 20 | + "java:S4507", |
| 21 | + "PMD.SystemPrintln", |
| 22 | + "PMD.AvoidPrintStackTrace" |
| 23 | + }) // Just a demo |
20 | 24 | public static void main(final String[] args) |
21 | 25 | { |
22 | 26 | final AdvancedImageFromDockerFile builder = new AdvancedImageFromDockerFile("dynamically-built") |
23 | 27 | .withLoggerForBuild(LoggerFactory.getLogger("container.build")) |
24 | | - .withPostGitIgnoreLines( |
25 | | - // Ignore files that aren't related to the built code |
26 | | - ".git/**", |
27 | | - ".config/**", |
28 | | - ".github/**", |
29 | | - ".idea/**", |
30 | | - ".run/**", |
31 | | - ".md", |
32 | | - ".cmd", |
33 | | - "/renovate.json5", |
34 | | - "testcontainers-advanced-imagebuilder/**", |
35 | | - "testcontainers-advanced-imagebuilder-demo/**" |
36 | | - ) |
37 | 28 | .withDockerFilePath(Paths.get("../testcontainers-advanced-imagebuilder-demo/Dockerfile")) |
38 | 29 | .withBaseDir(Paths.get("../")) |
39 | | - .withDockerFileLinesModifier(new DockerfileCOPYParentsEmulator()) |
40 | | - // Only copy the required maven modules and remove the not required ones |
41 | | - .withTransferArchiveTARCompressorCustomizer(c -> c.withContentModifier( |
42 | | - new FileLinesContentModifier() |
43 | | - { |
44 | | - @Override |
45 | | - public boolean shouldApply( |
46 | | - final Path sourcePath, |
47 | | - final String targetPath, |
48 | | - final TarArchiveEntry tarArchiveEntry) |
| 30 | + .configureFilesToTransferHandler(h -> h |
| 31 | + .withDockerFileLinesModifier(new DockerfileCOPYParentsEmulator()) |
| 32 | + .withPostGitIgnoreLines( |
| 33 | + // Ignore files that aren't related to the built code |
| 34 | + ".git/**", |
| 35 | + ".config/**", |
| 36 | + ".github/**", |
| 37 | + ".idea/**", |
| 38 | + ".run/**", |
| 39 | + ".md", |
| 40 | + ".cmd", |
| 41 | + "/renovate.json5", |
| 42 | + "testcontainers-advanced-imagebuilder/**", |
| 43 | + "testcontainers-advanced-imagebuilder-demo/**" |
| 44 | + ) |
| 45 | + // Only copy the required maven modules and remove the not required ones |
| 46 | + .withTransferArchiveTARCompressorCustomizer(c -> c.withContentModifier( |
| 47 | + new FileLinesContentModifier() |
49 | 48 | { |
50 | | - return "pom.xml".equals(targetPath); |
| 49 | + @Override |
| 50 | + public boolean shouldApply( |
| 51 | + final Path sourcePath, |
| 52 | + final String targetPath, |
| 53 | + final TarArchiveEntry tarArchiveEntry) |
| 54 | + { |
| 55 | + return "pom.xml".equals(targetPath); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public List<String> modify( |
| 60 | + final List<String> lines, |
| 61 | + final Path sourcePath, |
| 62 | + final String targetPath, |
| 63 | + final TarArchiveEntry tarArchiveEntry) |
| 64 | + { |
| 65 | + return lines.stream() |
| 66 | + // Only keep the dummy-app submodule as this is only needed for building |
| 67 | + .filter(s -> !(s.contains("<module>testcontainers-advanced-imagebuilder") |
| 68 | + && !s.contains("<module>testcontainers-advanced-imagebuilder-dummy-app"))) |
| 69 | + .toList(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean isIdentical(final List<String> original, final List<String> created) |
| 74 | + { |
| 75 | + return original.size() == created.size(); |
| 76 | + } |
51 | 77 | } |
52 | | - |
53 | | - @Override |
54 | | - public List<String> modify( |
55 | | - final List<String> lines, |
56 | | - final Path sourcePath, |
57 | | - final String targetPath, |
58 | | - final TarArchiveEntry tarArchiveEntry) |
59 | | - { |
60 | | - return lines.stream() |
61 | | - // Only keep the dummy-app submodule as this is only needed for building |
62 | | - .filter(s -> !(s.contains("<module>testcontainers-advanced-imagebuilder") |
63 | | - && !s.contains("<module>testcontainers-advanced-imagebuilder-dummy-app"))) |
64 | | - .toList(); |
65 | | - } |
66 | | - |
67 | | - @Override |
68 | | - public boolean isIdentical(final List<String> original, final List<String> created) |
69 | | - { |
70 | | - return original.size() == created.size(); |
71 | | - } |
72 | | - } |
73 | | - )); |
| 78 | + )) |
| 79 | + ); |
74 | 80 |
|
75 | | - final String imageName; |
76 | 81 | try |
77 | 82 | { |
78 | | - imageName = builder.get(5, TimeUnit.MINUTES); |
| 83 | + final String imageName = builder.build(Duration.ofMinutes(5)); |
| 84 | + System.out.println("Successfully build " + imageName); |
79 | 85 | } |
80 | | - catch(final TimeoutException e) |
| 86 | + catch(final Exception e) |
81 | 87 | { |
82 | | - System.out.println("Timed out: " + e.getMessage()); |
| 88 | + e.printStackTrace(); |
83 | 89 | System.exit(1); |
84 | | - return; |
85 | 90 | } |
86 | | - |
87 | | - System.out.println("Successfully build " + imageName); |
88 | 91 | } |
89 | 92 |
|
90 | 93 | private Application() |
|
0 commit comments