Skip to content

Commit 76c8e79

Browse files
committed
Detect Spring Boot Application via Manifest
Previously, the only way to determine a Spring Boot application was to inspect the filesystem looking for a specific JAR file. This change will make the first check the parsing of the application's manifest which should improve performance. [#114795971]
1 parent 26132cd commit 76c8e79

8 files changed

Lines changed: 60 additions & 48 deletions

File tree

lib/java_buildpack/container/java_main.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
require 'java_buildpack/util/dash_case'
2020
require 'java_buildpack/util/java_main_utils'
2121
require 'java_buildpack/util/qualify_path'
22+
require 'java_buildpack/util/spring_boot_utils'
2223

2324
module JavaBuildpack
2425
module Container
@@ -29,6 +30,14 @@ module Container
2930
class JavaMain < JavaBuildpack::Component::BaseComponent
3031
include JavaBuildpack::Util
3132

33+
# Creates an instance
34+
#
35+
# @param [Hash] context a collection of utilities used the component
36+
def initialize(context)
37+
super(context)
38+
@spring_boot_utils = JavaBuildpack::Util::SpringBootUtils.new
39+
end
40+
3241
# (see JavaBuildpack::Component::BaseComponent#detect)
3342
def detect
3443
main_class ? JavaMain.to_s.dash_case : nil
@@ -42,7 +51,10 @@ def compile
4251
def release
4352
@droplet.additional_libraries.insert 0, @application.root
4453
manifest_class_path.each { |path| @droplet.additional_libraries << path }
45-
@droplet.environment_variables.add_environment_variable 'SERVER_PORT', '$PORT' if boot_launcher?
54+
55+
if @spring_boot_utils.is?(@application)
56+
@droplet.environment_variables.add_environment_variable 'SERVER_PORT', '$PORT'
57+
end
4658

4759
release_text
4860
end
@@ -74,10 +86,6 @@ def arguments
7486
@configuration[ARGUMENTS_PROPERTY]
7587
end
7688

77-
def boot_launcher?
78-
main_class =~ /^org\.springframework\.boot\.loader\.(?:[JW]ar|Properties)Launcher$/
79-
end
80-
8189
def main_class
8290
JavaBuildpack::Util::JavaMainUtils.main_class(@application, @configuration)
8391
end

lib/java_buildpack/util/jar_finder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def is?(application)
3838
jar application
3939
end
4040

41-
# The version of of the JAR file used by the application
41+
# The version of the JAR file used by the application
4242
#
4343
# @param [Application] application the application to search
4444
# @return [String] the version of the JAR file used by the application

lib/java_buildpack/util/spring_boot_utils.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,42 @@
1717
require 'pathname'
1818
require 'java_buildpack/util'
1919
require 'java_buildpack/util/jar_finder'
20+
require 'java_buildpack/util/java_main_utils'
2021

2122
module JavaBuildpack
2223
module Util
2324

2425
# Utilities for dealing with Spring Boot applications
25-
class SpringBootUtils < JarFinder
26+
class SpringBootUtils
2627

2728
def initialize
28-
super(/.*spring-boot-([\d].*)\.jar/)
29+
@jar_finder = JavaBuildpack::Util::JarFinder.new(/.*spring-boot-([\d].*)\.jar/)
2930
end
3031

32+
# Indicates whether an application is a Spring Boot application
33+
#
34+
# @param [Application] application the application to search
35+
# @return [Boolean] +true+ if the application is a Spring Boot application, +false+ otherwise
36+
def is?(application)
37+
JavaBuildpack::Util::JavaMainUtils.manifest(application).key?(SPRING_BOOT_VERSION) ||
38+
@jar_finder.is?(application)
39+
end
40+
41+
# The version of Spring Boot used by the application
42+
#
43+
# @param [Application] application the application to search
44+
# @return [String] the version of Spring Boot used by the application
45+
def version(application)
46+
JavaBuildpack::Util::JavaMainUtils.manifest(application)[SPRING_BOOT_VERSION] ||
47+
@jar_finder.version(application)
48+
end
49+
50+
private
51+
52+
SPRING_BOOT_VERSION = 'Spring-Boot-Version'.freeze
53+
54+
private_constant :SPRING_BOOT_VERSION
55+
3156
end
3257

3358
end
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Main-Class: org.springframework.boot.loader.JarLauncher
1+
Spring-Boot-Version: 1.2.5.RELEASE
2+
Main-Class: org.springframework.boot.loader.JarLauncher
3+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
Spring-Boot-Version: 1.2.5.RELEASE
12
Main-Class: org.springframework.boot.loader.PropertiesLauncher
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
Spring-Boot-Version: 1.2.5.RELEASE
12
Main-Class: org.springframework.boot.loader.WarLauncher

spec/java_buildpack/container/java_main_spec.rb

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -115,41 +115,4 @@
115115
'test-jar-2.jar org.springframework.boot.loader.PropertiesLauncher')
116116
end
117117

118-
context do
119-
let(:configuration) { { 'java_main_class' => 'org.springframework.boot.loader.JarLauncher' } }
120-
121-
it 'releases Spring boot applications with a JarLauncher in the configuration by specifying a port' do
122-
123-
expect(component.release).to eq('JAVA_OPTS="test-opt-2 test-opt-1" && test-var-2 test-var-1 SERVER_PORT=$PORT ' \
124-
"eval exec #{qualify_path java_home.root, droplet.root}/bin/java $JAVA_OPTS " \
125-
'-cp $PWD/.:$PWD/.additional_libs/test-jar-1.jar:$PWD/.additional_libs/' \
126-
'test-jar-2.jar org.springframework.boot.loader.JarLauncher')
127-
end
128-
end
129-
130-
context do
131-
let(:configuration) { { 'java_main_class' => 'org.springframework.boot.loader.WarLauncher' } }
132-
133-
it 'releases Spring boot applications with a WarLauncher in the configuration by specifying a port' do
134-
135-
expect(component.release).to eq('JAVA_OPTS="test-opt-2 test-opt-1" && test-var-2 test-var-1 SERVER_PORT=$PORT ' \
136-
"eval exec #{qualify_path java_home.root, droplet.root}/bin/java $JAVA_OPTS " \
137-
'-cp $PWD/.:$PWD/.additional_libs/test-jar-1.jar:$PWD/.additional_libs/' \
138-
'test-jar-2.jar org.springframework.boot.loader.WarLauncher')
139-
end
140-
end
141-
142-
context do
143-
let(:configuration) { { 'java_main_class' => 'org.springframework.boot.loader.PropertiesLauncher' } }
144-
145-
it 'releases Spring boot applications with a PropertiesLauncher in the configuration by specifying a port' do
146-
147-
expect(component.release).to eq('JAVA_OPTS="test-opt-2 test-opt-1" && test-var-2 test-var-1 SERVER_PORT=$PORT ' \
148-
"eval exec #{qualify_path java_home.root, droplet.root}/bin/java $JAVA_OPTS " \
149-
'-cp $PWD/.:$PWD/.additional_libs/test-jar-1.jar:$PWD/.additional_libs' \
150-
'/test-jar-2.jar org.springframework.boot.loader.' \
151-
'PropertiesLauncher')
152-
end
153-
end
154-
155118
end

spec/java_buildpack/util/spring_boot_utils_spec.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,34 @@
3535
expect(utils.is?(application)).to be
3636
end
3737

38+
it 'detects a JAR Spring Boot application',
39+
app_fixture: 'container_main_spring_boot_jar_launcher' do
40+
41+
expect(utils.is?(application)).to be
42+
end
43+
3844
it 'does not detect a non-Spring Boot application',
3945
app_fixture: 'container_main' do
4046

4147
expect(utils.is?(application)).not_to be
4248
end
4349

44-
it 'determines the version a dist Spring Boot application',
50+
it 'determines the version of a dist Spring Boot application',
4551
app_fixture: 'container_spring_boot_dist' do
4652

4753
expect(utils.version(application)).to match(/1.0.0.RELEASE/)
4854
end
4955

50-
it 'determines the version a staged Spring Boot application',
56+
it 'determines the version of a staged Spring Boot application',
5157
app_fixture: 'container_spring_boot_staged' do
5258

5359
expect(utils.version(application)).to match(/1.0.0.RELEASE/)
5460
end
5561

62+
it 'determines the version of a JAR Spring Boot application',
63+
app_fixture: 'container_main_spring_boot_jar_launcher' do
64+
65+
expect(utils.version(application)).to match(/1.2.5.RELEASE/)
66+
end
67+
5668
end

0 commit comments

Comments
 (0)