Repository: GoogleContainerTools/jib
File: jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java (Java)

Code before:
        new CommandLine(new JibCli())
            .execute(
                "jar",
                "--target=docker://cli-gcr-base",
                "--from=gcr.io/google-appengine/openjdk:8",
                jarPath.toString());
    assertThat(exitCode).isEqualTo(0);
    String output = new Command("docker", "run", "--rm", "cli-gcr-base").run();
    assertThat(output).isEqualTo("Hello World");
  }

  public static void createJarFile(String name, String className, String classPath, String mainClass) throws IOException, URISyntaxException {
    Path javaFilePath = Paths.get(Resources.getResource("jarTest/standard/" + className + ".java").toURI());
    Path workingDir = Paths.get(Resources.getResource("jarTest/standard/").toURI());
    System.out.println("Working dir: " + workingDir);

    // compile the java file
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    assertThat(compiler).isNotNull();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(
        Collections.singletonList(javaFilePath.toFile()));
    Iterable<String> options = Arrays.asList("-source", "1.8", "-target", "1.8");
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits);
    boolean success = task.call();
    assertThat(success).isTrue();

    // Create a manifest file
    Manifest manifest = new Manifest();
    Attributes attributes = new Attributes();
    attributes.putValue("Manifest-Version", "1.0");
    if (classPath != null) {
      attributes.putValue("Class-Path", classPath);
    }
    if (mainClass != null) {
      attributes.putValue("Main-Class", mainClass);
    }
    manifest.getMainAttributes().putAll(attributes);

    // Create JAR
    File jarFile = workingDir.resolve(name).toFile();
    jarFile.getParentFile().mkdirs();
    FileOutputStream fileOutputStream = new FileOutputStream(jarFile);
    JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream, manifest);
    ZipEntry zipEntry = new ZipEntry(className + ".class");
    jarOutputStream.putNextEntry(zipEntry);
    jarOutputStream.write(Files.readAllBytes(workingDir.resolve(className + ".class")));
    jarOutputStream.closeEntry();
    jarOutputStream.close();
    fileOutputStream.close();
  }

Review comment (bug):
How about we use an if-else statement that proceeds with the jar building stage below if `task.call()` is true otherwise logs an error message  if the compilation fails?

Code after:
    Path jarPath = Paths.get(Resources.getResource("jarTest/standard/noDependencyJar.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute(
                "jar",
                "--target=docker://cli-gcr-base",
                "--from=gcr.io/google-appengine/openjdk:8",
                jarPath.toString());
    assertThat(exitCode).isEqualTo(0);
    String output = new Command("docker", "run", "--rm", "cli-gcr-base").run();
    assertThat(output).isEqualTo("Hello World");
  }

  public static void createJarFile(
      String name, String className, String classPath, String mainClass)
      throws IOException, URISyntaxException {
    Path javaFilePath =
        Paths.get(Resources.getResource("jarTest/standard/" + className + ".java").toURI());
    Path workingDir = Paths.get(Resources.getResource("jarTest/standard/").toURI());

    // compile the java file
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    Preconditions.checkNotNull(compiler);
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits =
        fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(javaFilePath.toFile()));
    Iterable<String> options = Arrays.asList("-source", "1.8", "-target", "1.8");
    JavaCompiler.CompilationTask task =
        compiler.getTask(null, fileManager, null, options, null, compilationUnits);
    boolean success = task.call();
    assertThat(success).isTrue();

    // Create a manifest file
    Manifest manifest = new Manifest();
    Attributes attributes = new Attributes();
    attributes.putValue("Manifest-Version", "1.0");
    if (classPath != null) {
      attributes.putValue("Class-Path", classPath);
    }
    if (mainClass != null) {
      attributes.putValue("Main-Class", mainClass);
    }
    manifest.getMainAttributes().putAll(attributes);

    // Create JAR
    File jarFile = workingDir.resolve(name).toFile();
    jarFile.getParentFile().mkdirs();
    try (FileOutputStream fileOutputStream = new FileOutputStream(jarFile);
        JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream, manifest)) {
      ZipEntry zipEntry = new ZipEntry(className + ".class");
      jarOutputStream.putNextEntry(zipEntry);