Skip to content

Commit 8c4c295

Browse files
authored
Merge pull request #260 from salesforce/plaird/packaging_test
fix git.properties
2 parents aa64cfc + 31a7b8a commit 8c4c295

File tree

4 files changed

+154
-51
lines changed

4 files changed

+154
-51
lines changed

examples/demoapp/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ java_test(
180180
deps = [":demoapp_lib"] + test_deps + springboottest_deps,
181181
)
182182

183+
java_test(
184+
name = "PackagingTest",
185+
srcs = ["src/test/java/com/salesforce/rules_spring/PackagingTest.java"],
186+
data = [":demoapp"],
187+
deps = [":demoapp_lib"] + test_deps,
188+
)
189+
183190
licenses_used(
184191
name = "demoapp_licenses",
185192
out = "demoapp_licenses.json",
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2021, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
package com.salesforce.rules_spring;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import java.io.File;
13+
import java.io.InputStream;
14+
import java.nio.charset.StandardCharsets;
15+
import java.util.Enumeration;
16+
import java.util.Iterator;
17+
import java.util.zip.ZipEntry;
18+
import java.util.zip.ZipFile;
19+
20+
import static org.junit.Assert.*;
21+
22+
/**
23+
*/
24+
public class PackagingTest {
25+
26+
File springbootJarFile = null;
27+
28+
// file contents
29+
String gitPropertiesContents = null;
30+
String buildPropertiesContents = null;
31+
String applicationPropertiesContents = null;
32+
33+
// file existence
34+
boolean exists_lib1 = false;
35+
boolean exists_lib2 = false;
36+
boolean exists_lib3_neverlink = false;
37+
38+
@Before
39+
public void setup() throws Exception {
40+
springbootJarFile = new File("./examples/demoapp/demoapp.jar");
41+
if (!springbootJarFile.exists()) {
42+
throw new IllegalStateException("Missing demoapp springboot jar; looked in path: " +
43+
springbootJarFile.getAbsolutePath());
44+
}
45+
extractFilesFromSpringBootJar();
46+
}
47+
48+
@Test
49+
public void gitPropertiesFileTest() {
50+
assertNotNull(gitPropertiesContents);
51+
assertTrue(gitPropertiesContents.contains("git.commit"));
52+
}
53+
54+
@Test
55+
public void buildPropertiesFileTest() {
56+
assertNotNull(buildPropertiesContents);
57+
assertTrue(buildPropertiesContents.contains("build.number"));
58+
}
59+
60+
@Test
61+
public void applicationPropertiesFileTest() {
62+
assertNotNull(applicationPropertiesContents);
63+
assertTrue(applicationPropertiesContents.contains("demoapp.config.internal"));
64+
}
65+
66+
@Test
67+
public void internalLibsAreIncludedTest() {
68+
assertTrue(this.exists_lib1);
69+
assertTrue(this.exists_lib2);
70+
}
71+
72+
@Test
73+
public void neverlinkLibsAreExcludedTest() {
74+
assertFalse(this.exists_lib3_neverlink);
75+
}
76+
77+
private void extractFilesFromSpringBootJar() throws Exception {
78+
try (ZipFile sbZip = new ZipFile(springbootJarFile)) {
79+
Enumeration<? extends ZipEntry> entries = sbZip.entries();
80+
for (Iterator<? extends ZipEntry> it = entries.asIterator(); it.hasNext(); ) {
81+
ZipEntry entry = it.next();
82+
String name = entry.getName();
83+
System.out.println(" zipentry: "+name);
84+
if (name.equals("BOOT-INF/classes/git.properties")) {
85+
InputStream is = sbZip.getInputStream(entry);
86+
gitPropertiesContents = new String(is.readAllBytes(), StandardCharsets.UTF_8);
87+
} else if (name.equals("BOOT-INF/classes/META-INF/build-info.properties")) {
88+
InputStream is = sbZip.getInputStream(entry);
89+
buildPropertiesContents = new String(is.readAllBytes(), StandardCharsets.UTF_8);
90+
} else if (name.equals("BOOT-INF/classes/application.properties")) {
91+
InputStream is = sbZip.getInputStream(entry);
92+
applicationPropertiesContents = new String(is.readAllBytes(), StandardCharsets.UTF_8);
93+
} else if (name.equals("BOOT-INF/lib/examples/demoapp/libs/lib1/liblib1.jar")) {
94+
exists_lib1 = true;
95+
} else if (name.equals("BOOT-INF/lib/examples/demoapp/libs/lib2/liblib2.jar")) {
96+
exists_lib2 = true;
97+
} else if (name.contains("neverlink")) {
98+
// we have a lib named lib3_neverlink that has neverlink = True, which means it should NOT be
99+
// included in the springboot jar
100+
exists_lib3_neverlink = true;
101+
}
102+
}
103+
}
104+
}
105+
}

springboot/springboot.bzl

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ def _banneddeps_rule_impl(ctx):
222222

223223
if found_banned:
224224
ctx.actions.write(output, "FAIL", is_executable = False)
225-
fail("Found banned jars in the springboot rule [" + ctx.label.name
226-
+ "] dependency list. Filenames:\n" + banned_filenames
227-
+ "\nYou can ignore these by setting deps_banned = [] on the springboot() rule.\n")
225+
fail("Found banned jars in the springboot rule [" + ctx.label.name +
226+
"] dependency list. Filenames:\n" + banned_filenames +
227+
"\nYou can ignore these by setting deps_banned = [] on the springboot() rule.\n")
228228
else:
229229
ctx.actions.write(output, "SUCCESS", is_executable = False)
230230
return [DefaultInfo(files = depset(outputs))]
@@ -237,11 +237,9 @@ _banneddeps_rule = rule(
237237
"deps_banned": attr.string_list(),
238238
"deps": attr.label_list(),
239239
"out": attr.string(),
240-
}
240+
},
241241
)
242242

243-
244-
245243
# ***************************************************************
246244
# Outer launcher script for "bazel run"
247245

@@ -297,21 +295,21 @@ def _springboot_rule_impl(ctx):
297295
# into the _bazelrun_script_template text defined above
298296
outer_bazelrun_script_contents = _bazelrun_script_template \
299297
.replace("%bazelrun_script%", ctx.attr.bazelrun_script.files.to_list()[0].short_path) \
300-
.replace("%name%", ctx.attr.name)
298+
.replace("%name%", ctx.attr.name)
301299

302300
# the bazelrun_java_toolchain optional, if set, we use it as the jvm for bazel run
303301
if ctx.attr.bazelrun_java_toolchain != None:
304-
# lookup the path to selected java toolchain, and string sub it into the bazel run script
305-
# text _bazelrun_script_template defined above
306-
java_runtime = ctx.attr.bazelrun_java_toolchain[java_common.JavaToolchainInfo].java_runtime
307-
java_bin = [f for f in java_runtime.files.to_list() if f.path.endswith("bin/java") or f.path.endswith("bin/java.exe")][0]
308-
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
309-
.replace("%java_toolchain_attr%", java_bin.path)
310-
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
311-
.replace("%java_toolchain_name_attr%", ctx.attr.bazelrun_java_toolchain.label.name)
302+
# lookup the path to selected java toolchain, and string sub it into the bazel run script
303+
# text _bazelrun_script_template defined above
304+
java_runtime = ctx.attr.bazelrun_java_toolchain[java_common.JavaToolchainInfo].java_runtime
305+
java_bin = [f for f in java_runtime.files.to_list() if f.path.endswith("bin/java") or f.path.endswith("bin/java.exe")][0]
306+
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
307+
.replace("%java_toolchain_attr%", java_bin.path)
308+
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
309+
.replace("%java_toolchain_name_attr%", ctx.attr.bazelrun_java_toolchain.label.name)
312310
else:
313-
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
314-
.replace("%java_toolchain_attr%", "")
311+
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
312+
.replace("%java_toolchain_attr%", "")
315313

316314
outer_bazelrun_script_file = ctx.actions.declare_file("%s" % ctx.label.name)
317315
ctx.actions.write(outer_bazelrun_script_file, outer_bazelrun_script_contents, is_executable = True)
@@ -323,9 +321,9 @@ def _springboot_rule_impl(ctx):
323321

324322
# and add any data files to runfiles
325323
if ctx.attr.bazelrun_data != None:
326-
for data_target in ctx.attr.bazelrun_data:
327-
for data_target in data_target.files.to_list():
328-
runfiles_list.append(data_target)
324+
for data_target in ctx.attr.bazelrun_data:
325+
for data_target in data_target.files.to_list():
326+
runfiles_list.append(data_target)
329327

330328
return [DefaultInfo(
331329
files = outs,
@@ -347,10 +345,8 @@ _springboot_rule = rule(
347345
"javaxdetect_rule": attr.label(),
348346
"banneddeps_rule": attr.label(),
349347
"apprun_rule": attr.label(),
350-
351-
"bazelrun_script": attr.label(allow_files=True),
352-
"bazelrun_data": attr.label_list(allow_files=True),
353-
348+
"bazelrun_script": attr.label(allow_files = True),
349+
"bazelrun_data": attr.label_list(allow_files = True),
354350
"bazelrun_java_toolchain": attr.label(
355351
mandatory = False,
356352
default = "@bazel_tools//tools/jdk:current_java_toolchain",
@@ -368,7 +364,7 @@ def springboot(
368364
boot_app_class,
369365
boot_launcher_class = "org.springframework.boot.loader.JarLauncher",
370366
deps = None,
371-
deps_banned = [ "junit", "mockito", ], # detects common mistake of test dep pollution
367+
deps_banned = ["junit", "mockito"], # detects common mistake of test dep pollution
372368
deps_exclude = None,
373369
deps_exclude_paths = None,
374370
deps_index_file = None,
@@ -377,7 +373,7 @@ def springboot(
377373
dupeclassescheck_ignorelist = None,
378374
javaxdetect_enable = None,
379375
javaxdetect_ignorelist = None,
380-
include_git_properties_file=True,
376+
include_git_properties_file = True,
381377
bazelrun_java_toolchain = None,
382378
bazelrun_script = None,
383379
bazelrun_jvm_flags = None,
@@ -391,17 +387,16 @@ def springboot(
391387
visibility = None,
392388
bazelrun_addopens = [],
393389
bazelrun_addexports = [],
394-
jartools_toolchains = ["@bazel_tools//tools/jdk:current_host_java_runtime"], # Issue 250
395-
exclude = [], # deprecated
396-
classpath_index = "@rules_spring//springboot:empty.txt", # deprecated
397-
use_build_dependency_order = True, # deprecated
398-
fail_on_duplicate_classes = False, # deprecated
399-
duplicate_class_allowlist = None, # deprecated
400-
jvm_flags = "", # deprecated
401-
data = [], # deprecated
390+
jartools_toolchains = ["@bazel_tools//tools/jdk:current_host_java_runtime"], # Issue 250
391+
exclude = [], # deprecated
392+
classpath_index = "@rules_spring//springboot:empty.txt", # deprecated
393+
use_build_dependency_order = True, # deprecated
394+
fail_on_duplicate_classes = False, # deprecated
395+
duplicate_class_allowlist = None, # deprecated
396+
jvm_flags = "", # deprecated
397+
data = [], # deprecated
402398
restricted_to = None,
403-
target_compatible_with = [],
404-
):
399+
target_compatible_with = []):
405400
"""Bazel rule for packaging an executable Spring Boot application.
406401
407402
Note that the rule README has more detailed usage instructions for each attribute.
@@ -610,22 +605,20 @@ def springboot(
610605
visibility = visibility,
611606
)
612607

613-
614-
615608
# SUBRULE 3B: GENERATE THE ENV VARIABLES USED BY THE BAZELRUN LAUNCHER SCRIPT
616609
genbazelrunenv_out = name + "_bazelrun_env.sh"
617610
native.genrule(
618611
name = genbazelrunenv_rule,
619612
srcs = bazelrun_data,
620-
cmd = "$(location @rules_spring//springboot:write_bazelrun_env.sh) " + name + " " + _get_springboot_jar_file_name(name)
621-
+ " " + _get_relative_package_path() + " $@ " + _convert_starlarkbool_to_bashbool(bazelrun_background)
622-
+ " $(SRCS)"
623-
+ " start_flags"
624-
+ " " + " ".join(["--add-exports=" + element for element in bazelrun_addexports])
625-
+ " " + " ".join(["--add-opens=" + element for element in bazelrun_addopens])
626-
+ " " + bazelrun_jvm_flags
627-
+ " start_envs"
628-
+ " " + bazelrun_env_flags,
613+
cmd = "$(location @rules_spring//springboot:write_bazelrun_env.sh) " + name + " " + _get_springboot_jar_file_name(name) +
614+
" " + _get_relative_package_path() + " $@ " + _convert_starlarkbool_to_bashbool(bazelrun_background) +
615+
" $(SRCS)" +
616+
" start_flags" +
617+
" " + " ".join(["--add-exports=" + element for element in bazelrun_addexports]) +
618+
" " + " ".join(["--add-opens=" + element for element in bazelrun_addopens]) +
619+
" " + bazelrun_jvm_flags +
620+
" start_envs" +
621+
" " + bazelrun_env_flags,
629622
# message = "SpringBoot rule is writing the bazel run launcher env...",
630623
tools = ["@rules_spring//springboot:write_bazelrun_env.sh"],
631624
outs = [genbazelrunenv_out],
@@ -727,10 +720,8 @@ def springboot(
727720
dupecheck_rule = dupecheck_rule_label,
728721
javaxdetect_rule = javaxdetect_rule_label,
729722
apprun_rule = ":" + apprun_rule,
730-
731723
bazelrun_script = bazelrun_script,
732724
bazelrun_data = bazelrun_data,
733-
734725
tags = tags,
735726
testonly = testonly,
736727
restricted_to = restricted_to,
@@ -747,7 +738,7 @@ def _get_springboot_jar_file_name(name):
747738

748739
def _convert_starlarkbool_to_bashbool(starlarkbool):
749740
if starlarkbool:
750-
return "true"
741+
return "true"
751742
return "false"
752743

753744
def _get_relative_package_path():

springboot/springboot_pkg.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ echo "DEBUG: finished copying transitives into BOOT-INF/lib, elapsed time (secon
245245
if [[ "$include_git_properties_file" == true ]]; then
246246
echo "DEBUG: adding git.properties" >> $debugfile
247247
cat $ruledir/$gitpropsfile >> $debugfile
248-
cp -f $ruledir/$gitpropsfile $working_dir/BOOT-INF/classes
248+
cp -f $ruledir/$gitpropsfile $working_dir/BOOT-INF/classes/git.properties
249249
fi
250250

251251
# Inject the classpath index (unless it is the default empty.txt file). Requires Spring Boot version 2.3+

0 commit comments

Comments
 (0)