Skip to content

Commit

Permalink
Changes GradleSourceFilesConfiguration to warn instead of error when…
Browse files Browse the repository at this point in the history
… classes not found. (GoogleContainerTools#462)
  • Loading branch information
coollog authored Jun 28, 2018
1 parent 508bcda commit a057f28
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
3 changes: 3 additions & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ All notable changes to this project will be documented in this file.
- Container creation date set to timestamp 0 ([#341](https://github.com/GoogleContainerTools/jib/issues/341))
- Does not authenticate base image pull unless necessary - reduces build time by about 500ms ([#414](https://github.com/GoogleContainerTools/jib/pull/414))
- `jvmFlags`, `mainClass`, `args`, and `format` are now grouped under `container` configuration object ([#384](https://github.com/GoogleContainerTools/jib/issues/384))
- Warns instead of errors when classes not found ([#462](https://github.com/GoogleContainerTools/jib/pull/462))

### Fixed

- Using Azure Container Registry now works - define credentials in `jib.to.auth`/`jib.from.auth` ([#415](https://github.com/GoogleContainerTools/jib/issues/415))
- Supports `access_token` as alias to `token` in registry authentication ([#420](https://github.com/GoogleContainerTools/jib/pull/420))
- Docker context export for Groovy project ([#459](https://github.com/GoogleContainerTools/jib/pull/459))
- Visibility of `jib.to.image` ([#460](https://github.com/GoogleContainerTools/jib/pull/460))

## 0.9.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ static GradleProjectProperties getForProject(
Project project, GradleBuildLogger gradleBuildLogger) {
try {
return new GradleProjectProperties(
project, gradleBuildLogger, GradleSourceFilesConfiguration.getForProject(project));
project,
gradleBuildLogger,
GradleSourceFilesConfiguration.getForProject(project, gradleBuildLogger));

} catch (IOException ex) {
throw new GradleException(
"Obtaining project build output files failed; make sure you have compiled your project "
+ "before trying to build the image. (Did you accidentally run \"gradle clean "
+ "jib\" instead of \"gradle clean compileJava jib\"?)",
ex);
throw new GradleException("Obtaining project build output files failed", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ class GradleSourceFilesConfiguration implements SourceFilesConfiguration {
private static final String MAIN_SOURCE_SET_NAME = "main";

/** Resolves the source files configuration for a Gradle {@link Project}. */
static GradleSourceFilesConfiguration getForProject(Project project) throws IOException {
return new GradleSourceFilesConfiguration(project);
static GradleSourceFilesConfiguration getForProject(
Project project, GradleBuildLogger gradleBuildLogger) throws IOException {
return new GradleSourceFilesConfiguration(project, gradleBuildLogger);
}

private final ImmutableList<Path> dependenciesFiles;
private final ImmutableList<Path> resourcesFiles;
private final ImmutableList<Path> classesFiles;

/** Instantiate with {@link #getForProject}. */
private GradleSourceFilesConfiguration(Project project) throws IOException {
private GradleSourceFilesConfiguration(Project project, GradleBuildLogger gradleBuildLogger)
throws IOException {
JavaPluginConvention javaPluginConvention =
project.getConvention().getPlugin(JavaPluginConvention.class);

Expand All @@ -59,10 +61,19 @@ private GradleSourceFilesConfiguration(Project project) throws IOException {
// Adds each file in each classes output directory to the classes files list.
FileCollection classesOutputDirectories = mainSourceSet.getOutput().getClassesDirs();
for (File classesOutputDirectory : classesOutputDirectories) {
if (Files.notExists(classesOutputDirectory.toPath())) {
// Warns that output directory was not found.
gradleBuildLogger.warn(
"Could not find build output directory '" + classesOutputDirectory + "'");
continue;
}
try (Stream<Path> classFileStream = Files.list(classesOutputDirectory.toPath())) {
classFileStream.forEach(classesFiles::add);
}
}
if (classesFiles.isEmpty()) {
gradleBuildLogger.warn("No classes files were found - did you compile your project?");
}

// Adds each file in the resources output directory to the resources files list.
Path resourcesOutputDirectory = mainSourceSet.getOutput().getResourcesDir().toPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

Expand Down Expand Up @@ -65,17 +66,18 @@ public Set<File> getFiles() {
}
}

@Mock private Project mockProject;
@Mock private Convention mockConvention;
@Mock private JavaPluginConvention mockJavaPluginConvention;
@Mock private SourceSetContainer mockSourceSetContainer;
@Mock private SourceSet mockMainSourceSet;
@Mock private SourceSetOutput mockMainSourceSetOutput;
@Mock private GradleBuildLogger mockGradleBuildLogger;

private GradleSourceFilesConfiguration testGradleSourceFilesConfiguration;

@Before
public void setUp() throws URISyntaxException, IOException {
Project mockProject = Mockito.mock(Project.class);
Convention mockConvention = Mockito.mock(Convention.class);
JavaPluginConvention mockJavaPluginConvention = Mockito.mock(JavaPluginConvention.class);
SourceSetContainer mockSourceSetContainer = Mockito.mock(SourceSetContainer.class);
SourceSet mockMainSourceSet = Mockito.mock(SourceSet.class);
SourceSetOutput mockMainSourceSetOutput = Mockito.mock(SourceSetOutput.class);

Set<File> classesFiles =
ImmutableSet.of(Paths.get(Resources.getResource("application/classes").toURI()).toFile());
FileCollection classesFileCollection = new TestFileCollection(classesFiles);
Expand Down Expand Up @@ -103,7 +105,8 @@ public void setUp() throws URISyntaxException, IOException {
Mockito.when(mockMainSourceSetOutput.getResourcesDir()).thenReturn(resourcesOutputDir);
Mockito.when(mockMainSourceSet.getRuntimeClasspath()).thenReturn(runtimeFileCollection);

testGradleSourceFilesConfiguration = GradleSourceFilesConfiguration.getForProject(mockProject);
testGradleSourceFilesConfiguration =
GradleSourceFilesConfiguration.getForProject(mockProject, mockGradleBuildLogger);
}

@Test
Expand Down Expand Up @@ -132,6 +135,21 @@ public void test_correctFiles() throws URISyntaxException {
Assert.assertEquals(expectedClassesFiles, testGradleSourceFilesConfiguration.getClassesFiles());
}

@Test
public void test_noClassesFiles() throws IOException {
File nonexistentFile = new File("/nonexistent/file");
Mockito.when(mockMainSourceSetOutput.getClassesDirs())
.thenReturn(new TestFileCollection(ImmutableSet.of(nonexistentFile)));

testGradleSourceFilesConfiguration =
GradleSourceFilesConfiguration.getForProject(mockProject, mockGradleBuildLogger);

Mockito.verify(mockGradleBuildLogger)
.warn("Could not find build output directory '" + nonexistentFile + "'");
Mockito.verify(mockGradleBuildLogger)
.warn("No classes files were found - did you compile your project?");
}

@Test
public void test_correctPathsOnImage() {
Assert.assertEquals(
Expand Down

0 comments on commit a057f28

Please sign in to comment.