Skip to content

Commit

Permalink
Package: Add resource files to JAR file
Browse files Browse the repository at this point in the history
Closes #106.
  • Loading branch information
tindzk committed Jun 27, 2020
1 parent 64e6391 commit c0cfc71
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/seed/cli/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ object Package {

if (paths.isEmpty) log.error("No build paths were found")
else {
val files = collectFiles(paths)

val resolvedModule = build(module)
val jvmModule =
resolvedModule.module.jvm.getOrElse(resolvedModule.module)

val files = collectFiles(paths) ++ collectFiles(jvmModule.resources)

val classPath =
if (!libs) List()
else
Expand Down
46 changes: 45 additions & 1 deletion src/test/scala/seed/generation/PackageSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object PackageSpec extends TestSuite[Unit] {
override def tearDown(env: Unit): Unit = ()

testAsync("Package modules with same package") { _ =>
val path = Paths.get("test/package-modules")
val path = Paths.get("test", "package-modules")

val config = BuildConfig.load(path, Log.urgent).get
val outputPath = tempPath.resolve("package-modules")
Expand Down Expand Up @@ -63,4 +63,48 @@ object PackageSpec extends TestSuite[Unit] {
}
} yield assertEquals(result.trim, "42")
}

testAsync("Package resources") { _ =>
val path = Paths.get("test", "example-resources")

val config = BuildConfig.load(path, Log.urgent).get
val outputPath = tempPath.resolve("example-resources-package")
Files.createDirectory(outputPath)
val buildPath = outputPath.resolve("build")
val packageConfig = PackageConfig(
tmpfs = false,
silent = false,
ivyPath = None,
cachePath = None
)
cli.Generate.ui(
Config(),
config.projectPath,
outputPath,
config.resolvers,
config.build,
Command.Bloop(packageConfig),
Log.urgent
)

for {
result <- {
cli.Package.ui(
Config(),
outputPath,
Resolvers(),
config.build,
"example",
Some(buildPath),
libs = true,
progress = false,
packageConfig,
Log.urgent
)

util.TestProcessHelper
.runCommand(buildPath, List("java", "-jar", "example.jar"))
}
} yield assertEquals(result.trim, "hello world")
}
}
1 change: 1 addition & 0 deletions test/example-resources/build.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ scalaVersion = "2.11.11"
root = "."
sources = ["src/"]
resources = ["resources/"]
mainClass = "com.mkyong.Application"
20 changes: 11 additions & 9 deletions test/example-resources/src/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package com.mkyong;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -15,30 +17,30 @@ public class Application {
public static void main(String[] args) throws IOException {

Application main = new Application();
File file = main.getFileFromResources("test.txt");
InputStream stream = main.getFileFromResources("test.txt");

printFile(file);
printStream(stream);
}

// get file from classpath, resources folder
private File getFileFromResources(String fileName) {
private InputStream getFileFromResources(String fileName) {

ClassLoader classLoader = getClass().getClassLoader();

URL resource = classLoader.getResource(fileName);
InputStream resource = classLoader.getResourceAsStream(fileName);
if (resource == null) {
throw new IllegalArgumentException("file is not found!");
throw new IllegalArgumentException("File not found");
} else {
return new File(resource.getFile());
return resource;
}

}

private static void printFile(File file) throws IOException {
private static void printStream(InputStream stream) throws IOException {

if (file == null) return;
if (stream == null) return;

try (FileReader reader = new FileReader(file);
try (InputStreamReader reader = new InputStreamReader(stream);
BufferedReader br = new BufferedReader(reader)) {

String line;
Expand Down

0 comments on commit c0cfc71

Please sign in to comment.