diff --git a/.allstar/binary_artifacts.yaml b/.allstar/binary_artifacts.yaml index cf26dacfe8..0b70480559 100644 --- a/.allstar/binary_artifacts.yaml +++ b/.allstar/binary_artifacts.yaml @@ -1,8 +1,4 @@ # Ignore reason: jars are used for testing purposes only ignorePaths: - - jib-cli/src/integration-test/resources/jarTest/standard/dependency1.jar - - jib-cli/src/integration-test/resources/jarTest/standard/directory/dependency2.jar - - jib-cli/src/integration-test/resources/jarTest/standard/jarWithCp.jar - - jib-cli/src/integration-test/resources/jarTest/standard/noDependencyJar.jar - jib-gradle-plugin/src/integration-test/resources/gradle/projects/default-target/libs/dependency-1.0.0.jar - jib-gradle-plugin/src/integration-test/resources/gradle/projects/simple/libs/dependency-1.0.0.jar \ No newline at end of file diff --git a/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java b/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java index 6b557196f2..d1c2bce75b 100644 --- a/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java +++ b/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java @@ -20,18 +20,32 @@ import com.google.cloud.tools.jib.Command; import com.google.cloud.tools.jib.api.HttpRequestTester; +import com.google.common.base.Preconditions; import com.google.common.io.Resources; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; import java.util.jar.Attributes; import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; import javax.annotation.Nullable; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; import org.junit.After; +import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import picocli.CommandLine; @@ -43,6 +57,15 @@ public class JarCommandTest { @Nullable private String containerName; + @BeforeClass + public static void createJars() throws IOException, URISyntaxException { + createJarFile( + "jarWithCp.jar", "HelloWorld", "dependency1.jar directory/dependency2.jar", "HelloWorld"); + createJarFile("noDependencyJar.jar", "HelloWorld", null, "HelloWorld"); + createJarFile("dependency1.jar", "dep/A", null, null); + createJarFile("directory/dependency2.jar", "dep2/B", null, null); + } + @After public void tearDown() throws IOException, InterruptedException { if (containerName != null) { @@ -315,4 +338,47 @@ public void testJar_baseImageSpecified() 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 compilationUnits = + fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(javaFilePath.toFile())); + Iterable 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); + jarOutputStream.write(Files.readAllBytes(workingDir.resolve(className + ".class"))); + jarOutputStream.closeEntry(); + } + } } diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/HelloWorld.java b/jib-cli/src/integration-test/resources/jarTest/standard/HelloWorld.java new file mode 100644 index 0000000000..558d6f9908 --- /dev/null +++ b/jib-cli/src/integration-test/resources/jarTest/standard/HelloWorld.java @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.print("Hello World"); + } +} diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/dep/A.java b/jib-cli/src/integration-test/resources/jarTest/standard/dep/A.java new file mode 100644 index 0000000000..96d933ef79 --- /dev/null +++ b/jib-cli/src/integration-test/resources/jarTest/standard/dep/A.java @@ -0,0 +1,7 @@ +package dep; + +public class A { + public static void getResult() { + System.out.print("Hello "); + } +} diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/dep2/B.java b/jib-cli/src/integration-test/resources/jarTest/standard/dep2/B.java new file mode 100644 index 0000000000..41ff5a9aa4 --- /dev/null +++ b/jib-cli/src/integration-test/resources/jarTest/standard/dep2/B.java @@ -0,0 +1,7 @@ +package dep2; + +public class B { + public static void getResult() { + System.out.print("World"); + } +} diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/dependency1.jar b/jib-cli/src/integration-test/resources/jarTest/standard/dependency1.jar deleted file mode 100644 index 84b923b25b..0000000000 Binary files a/jib-cli/src/integration-test/resources/jarTest/standard/dependency1.jar and /dev/null differ diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/directory/dependency2.jar b/jib-cli/src/integration-test/resources/jarTest/standard/directory/dependency2.jar deleted file mode 100644 index 79583ea652..0000000000 Binary files a/jib-cli/src/integration-test/resources/jarTest/standard/directory/dependency2.jar and /dev/null differ diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/jarWithCp.jar b/jib-cli/src/integration-test/resources/jarTest/standard/jarWithCp.jar deleted file mode 100644 index 0ae4bd379a..0000000000 Binary files a/jib-cli/src/integration-test/resources/jarTest/standard/jarWithCp.jar and /dev/null differ diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/noDependencyJar.jar b/jib-cli/src/integration-test/resources/jarTest/standard/noDependencyJar.jar deleted file mode 100644 index 754cd1ba2e..0000000000 Binary files a/jib-cli/src/integration-test/resources/jarTest/standard/noDependencyJar.jar and /dev/null differ