Skip to content

Commit 2186553

Browse files
authored
Fixes MavenSourceFilesConfiguration adds resource directories into the classes layer. (GoogleContainerTools#320)
1 parent a9585ec commit 2186553

6 files changed

Lines changed: 36 additions & 30 deletions

File tree

‎jib-maven-plugin/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ All notable changes to this project will be documented in this file.
2121
- Changed configuration schema to be more like configuration for `jib-gradle-plugin` - NOT compatible with prior versions of `jib-maven-plugin` ([#212](https://github.com/GoogleContainerTools/jib/issues/212))
2222
- `jib:dockercontext` has been changed to `jib:dockerContext` ([#321](https://github.com/GoogleContainerTools/jib/issues/321))
2323

24+
### Fixed
25+
- Directories in resources are added to classes layer ([#318](https://github.com/GoogleContainerTools/jib/issues/318))
26+
2427
## 0.1.7
2528
### Fixed
2629
- Using base images that lack entrypoints ([#284](https://github.com/GoogleContainerTools/jib/pull/284)

‎jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfiguration.java‎

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ static MavenSourceFilesConfiguration getForProject(MavenProject project) throws
4242
return new MavenSourceFilesConfiguration(project);
4343
}
4444

45-
/** If the {@code path} has extension {@code .class}, replace the extension with {@code .java}. */
46-
private static Path replaceClassExtensionWithJava(Path path) {
47-
if (FileSystems.getDefault().getPathMatcher("glob:**.class").matches(path)) {
48-
// If is a class file, replace extension with .java.
49-
return path.resolveSibling(
50-
path.getFileName().toString().replaceAll("(.*?)\\.class", "$1.java"));
51-
}
52-
return path;
53-
}
54-
5545
private final List<Path> dependenciesFiles = new ArrayList<>();
5646
private final List<Path> resourcesFiles = new ArrayList<>();
5747
private final List<Path> classesFiles = new ArrayList<>();
@@ -70,20 +60,9 @@ private MavenSourceFilesConfiguration(MavenProject project) throws IOException {
7060
// files by matching them against the .java source files. All other files are deemed resources.
7161
try (Stream<Path> classFileStream = Files.list(classesOutputDirectory)) {
7262
classFileStream.forEach(
73-
classFile -> {
74-
Path javaFile = replaceClassExtensionWithJava(classFile);
75-
76-
// Resolves the file in the source directory.
77-
Path correspondingSourceDirFile =
78-
classesSourceDirectory.resolve(classesOutputDirectory.relativize(javaFile));
79-
if (Files.exists(correspondingSourceDirFile)) {
80-
// Adds the file as a classes file since it is in the source directory.
81-
classesFiles.add(classFile);
82-
} else {
83-
// Adds the file as a resource since it is not in the source directory.
84-
resourcesFiles.add(classFile);
85-
}
86-
});
63+
classFile ->
64+
addFileToResourcesOrClasses(
65+
classesSourceDirectory, classesOutputDirectory, classFile));
8766
}
8867

8968
// Sort all files by path for consistent ordering.
@@ -121,4 +100,27 @@ public String getResourcesPathOnImage() {
121100
public String getClassesPathOnImage() {
122101
return CLASSES_PATH_ON_IMAGE;
123102
}
103+
104+
/**
105+
* Adds {@code file} to {@link #classesFiles} if it is a {@code .class} file or is a directory
106+
* that also exists in the classes source directory; otherwise, adds {@code file} to {@link
107+
* #resourcesFiles}.
108+
*/
109+
private void addFileToResourcesOrClasses(
110+
Path classesSourceDirectory, Path classesOutputDirectory, Path file) {
111+
// If is a directory, checks if there is corresponding directory in source directory.
112+
if (Files.isDirectory(file)
113+
&& Files.exists(classesSourceDirectory.resolve(classesOutputDirectory.relativize(file)))) {
114+
classesFiles.add(file);
115+
return;
116+
}
117+
118+
// Checks if is .class file.
119+
if (FileSystems.getDefault().getPathMatcher("glob:**.class").matches(file)) {
120+
classesFiles.add(file);
121+
return;
122+
}
123+
124+
resourcesFiles.add(file);
125+
}
124126
}

‎jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenSourceFilesConfigurationTest.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ public void test_correctFiles() throws URISyntaxException {
7474
Paths.get("application", "dependencies", "libraryB.jar"));
7575
List<Path> expectedResourcesFiles =
7676
Arrays.asList(
77-
Paths.get(Resources.getResource("application/output").toURI()).resolve("resourceA"),
78-
Paths.get(Resources.getResource("application/output").toURI()).resolve("resourceB"),
79-
Paths.get(Resources.getResource("application/output").toURI()).resolve("world"));
77+
Paths.get(Resources.getResource("application/output/directory").toURI()),
78+
Paths.get(Resources.getResource("application/output/resourceA").toURI()),
79+
Paths.get(Resources.getResource("application/output/resourceB").toURI()),
80+
Paths.get(Resources.getResource("application/output/world").toURI()));
8081
List<Path> expectedClassesFiles =
8182
Arrays.asList(
82-
Paths.get(Resources.getResource("application/output").toURI())
83-
.resolve("HelloWorld.class"),
84-
Paths.get(Resources.getResource("application/output").toURI()).resolve("some.class"));
83+
Paths.get(Resources.getResource("application/output/HelloWorld.class").toURI()),
84+
Paths.get(Resources.getResource("application/output/package").toURI()),
85+
Paths.get(Resources.getResource("application/output/some.class").toURI()));
8586

8687
Assert.assertEquals(
8788
expectedDependenciesFiles, testMavenSourceFilesConfiguration.getDependenciesFiles());

‎jib-maven-plugin/src/test/resources/application/output/directory/somefile‎

Whitespace-only changes.

‎jib-maven-plugin/src/test/resources/application/output/package/some.class‎

Whitespace-only changes.

‎jib-maven-plugin/src/test/resources/application/source/package/some.java‎

Whitespace-only changes.

0 commit comments

Comments
 (0)