Last active
October 31, 2024 08:37
-
-
Save Zetten/4918d56da9e4edb7bdc7192b9f648b8f to your computer and use it in GitHub Desktop.
Bazel spring-boot jar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java_binary( | |
name = "spring_boot_packager", | |
srcs = ["src/SpringBootPackager.java"], | |
main_class = "src.SpringBootPackager", | |
visibility = ["//visibility:public"], | |
deps = ["//third_party/java:org_springframework_boot_spring_boot_loader_tools"], | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _boot_jar_impl(ctx): | |
deps = ctx.attr.target.java.transitive_runtime_deps + ctx.attr._spring_boot_loader.java.transitive_runtime_deps | |
args = ctx.actions.args() | |
args.add(ctx.attr.include_launch_script) | |
args.add(ctx.file.target.path) | |
args.add(ctx.outputs.executable.path) | |
args.add([d.path for d in deps]) | |
ctx.actions.run( | |
inputs = deps + [ctx.file.target], | |
outputs = [ctx.outputs.executable], | |
executable = ctx.executable._spring_boot_packager, | |
arguments = [args], | |
) | |
_pkg_boot_jar = rule( | |
attrs = { | |
"target": attr.label(allow_single_file=[".jar"]), | |
"include_launch_script": attr.bool(), | |
"_spring_boot_loader": attr.label(allow_single_file=[".jar"], default="//third_party/java:org_springframework_boot_spring_boot_loader"), | |
"_spring_boot_packager": attr.label(executable=True, cfg="host", default="//tools/spring-boot:spring_boot_packager"), | |
}, | |
fragments = ["jvm"], | |
host_fragments = ["jvm"], | |
implementation = _boot_jar_impl, | |
executable = True | |
) | |
def boot_jar(name, target, include_launch_script = True, **kwargs): | |
_pkg_boot_jar( | |
name = name, | |
target = target, | |
include_launch_script = include_launch_script, | |
**kwargs | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package src; | |
import org.springframework.boot.loader.tools.DefaultLaunchScript; | |
import org.springframework.boot.loader.tools.Libraries; | |
import org.springframework.boot.loader.tools.Library; | |
import org.springframework.boot.loader.tools.LibraryScope; | |
import org.springframework.boot.loader.tools.Repackager; | |
import java.io.IOException; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
/** | |
* <p>Command line utility wrapping Spring Boot's {@link Repackager} tool.</p> | |
* <p>Usage: <code><command> includeLaunchScript inputJar outputJar dependencyJar dependencyJar...</code></p> | |
* <p>Example: <code><command> true /path/to/mylibrary.jar /path/to/springbootapp.jar /path/to/dependencies/dep1.jar /path/to/dependencies/dep2.jar</code></p> | |
*/ | |
public final class SpringBootPackager { | |
public static void main(String[] args) throws IOException { | |
List<String> arguments = Arrays.asList(args); | |
boolean includeLaunchScript = Boolean.parseBoolean(arguments.get(0)); | |
Path inputJar = Paths.get(arguments.get(1)); | |
Path outputJar = Paths.get(arguments.get(2)); | |
Repackager repackager = new Repackager(inputJar.toFile()); | |
Libraries libraries = callback -> { | |
List<Path> libs = arguments.subList(3, arguments.size()).stream().map(Paths::get).collect(Collectors.toList()); | |
for (Path lib : libs) { | |
callback.library(new Library(lib.toFile(), LibraryScope.RUNTIME)); | |
} | |
}; | |
if (includeLaunchScript) { | |
repackager.repackage(outputJar.toFile(), libraries, new DefaultLaunchScript(null, Collections.emptyMap())); | |
} else { | |
repackager.repackage(outputJar.toFile(), libraries); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for sharing this! Out of curiosity, why wrap the rule in a macro?