Skip to content

Commit 686be74

Browse files
author
Glyn Normington
committed
Specify port for Spring Boot Java Main applications
Detect such applications by specific Spring Boot main classnames. [#55145080]
1 parent 44c521d commit 686be74

5 files changed

Lines changed: 105 additions & 31 deletions

File tree

docs/container-java-main.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Command line arguments may optionally be configured.
1515
</table>
1616
Tags are printed to standard output by the buildpack detect script
1717

18+
## Spring Boot
19+
20+
If the main class is Spring Boot's `JarLauncher` or `WarLauncher`, the Java Main Class Container adds a `--server.port` argument to the command so that the
21+
application uses the correct port.
22+
1823
## Configuration
1924
For general information on configuring the buildpack, refer to [Configuration and Extension][].
2025

lib/java_buildpack/container/main.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ def release
6565
java_opts_string = ContainerUtils.space(ContainerUtils.to_java_opts_s(@java_opts))
6666
main_class_string = ContainerUtils.space(main_class)
6767
arguments_string = ContainerUtils.space(arguments)
68+
port_string = ContainerUtils.space(port)
6869

69-
"#{java_string}#{classpath_string}#{java_opts_string}#{main_class_string}#{arguments_string}"
70+
"#{java_string}#{classpath_string}#{java_opts_string}#{main_class_string}#{arguments_string}#{port_string}"
7071
end
7172

7273
private
@@ -108,6 +109,10 @@ def manifest_class_path
108109
value.nil? ? [] : value.split(' ')
109110
end
110111

112+
def port
113+
main_class =~ /^org\.springframework\.boot\.loader\.[JW]arLauncher$/ ? '--server.port=$PORT' : nil
114+
end
115+
111116
end
112117

113118
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Main-Class: org.springframework.boot.loader.JarLauncher
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Main-Class: org.springframework.boot.loader.WarLauncher

spec/java_buildpack/container/main_spec.rb

Lines changed: 92 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,35 @@ module JavaBuildpack::Container
2424

2525
it 'should detect with main class configuration' do
2626
detected = Main.new(
27-
app_dir: 'spec/fixtures/container_none',
28-
configuration: { 'java_main_class' => 'test-java-main-class' }
27+
app_dir: 'spec/fixtures/container_none',
28+
configuration: { 'java_main_class' => 'test-java-main-class' }
2929
).detect
3030

3131
expect(detected).to be_true
3232
end
3333

3434
it 'should detect with main class manifest entry' do
3535
detected = Main.new(
36-
app_dir: 'spec/fixtures/container_main',
37-
configuration: {}
36+
app_dir: 'spec/fixtures/container_main',
37+
configuration: {}
3838
).detect
3939

4040
expect(detected).to be_true
4141
end
4242

4343
it 'should not detect without main class manifest entry' do
4444
detected = Main.new(
45-
app_dir: 'spec/fixtures/container_main_no_main_class',
46-
configuration: {}
45+
app_dir: 'spec/fixtures/container_main_no_main_class',
46+
configuration: {}
4747
).detect
4848

4949
expect(detected).to be_false
5050
end
5151

5252
it 'should not detect without manifest' do
5353
detected = Main.new(
54-
app_dir: 'spec/fixtures/container_main_none',
55-
configuration: {}
54+
app_dir: 'spec/fixtures/container_main_none',
55+
configuration: {}
5656
).detect
5757

5858
expect(detected).to be_false
@@ -64,11 +64,11 @@ module JavaBuildpack::Container
6464
Dir.mkdir lib_directory
6565

6666
command = Main.new(
67-
app_dir: root,
68-
java_home: 'test-java-home',
69-
java_opts: %w(test-opt-2 test-opt-1),
70-
lib_directory: lib_directory,
71-
configuration: { 'java_main_class' => 'test-java-main-class' }
67+
app_dir: root,
68+
java_home: 'test-java-home',
69+
java_opts: %w(test-opt-2 test-opt-1),
70+
lib_directory: lib_directory,
71+
configuration: { 'java_main_class' => 'test-java-main-class' }
7272
).release
7373

7474
expect(command).to eq('test-java-home/bin/java -cp . test-opt-1 test-opt-2 test-java-main-class')
@@ -81,11 +81,11 @@ module JavaBuildpack::Container
8181
Dir.mkdir lib_directory
8282

8383
command = Main.new(
84-
app_dir: 'spec/fixtures/container_main',
85-
java_home: 'test-java-home',
86-
java_opts: [],
87-
lib_directory: lib_directory,
88-
configuration: {}
84+
app_dir: 'spec/fixtures/container_main',
85+
java_home: 'test-java-home',
86+
java_opts: [],
87+
lib_directory: lib_directory,
88+
configuration: {}
8989
).release
9090

9191
expect(command).to eq('test-java-home/bin/java -cp .:alpha.jar:bravo.jar:charlie.jar test-main-class')
@@ -98,14 +98,14 @@ module JavaBuildpack::Container
9898
Dir.mkdir lib_directory
9999

100100
command = Main.new(
101-
app_dir: root,
102-
java_home: 'test-java-home',
103-
java_opts: [],
104-
lib_directory: lib_directory,
105-
configuration: {
106-
'java_main_class' => 'test-java-main-class',
107-
'arguments' => 'some arguments'
108-
}
101+
app_dir: root,
102+
java_home: 'test-java-home',
103+
java_opts: [],
104+
lib_directory: lib_directory,
105+
configuration: {
106+
'java_main_class' => 'test-java-main-class',
107+
'arguments' => 'some arguments'
108+
}
109109
).release
110110

111111
expect(command).to eq('test-java-home/bin/java -cp . test-java-main-class some arguments')
@@ -120,16 +120,78 @@ module JavaBuildpack::Container
120120
Dir['spec/fixtures/additional_libs/*'].each { |file| system "cp #{file} #{lib_directory}" }
121121

122122
command = Main.new(
123-
app_dir: root,
123+
app_dir: root,
124+
java_home: 'test-java-home',
125+
java_opts: [],
126+
lib_directory: lib_directory,
127+
configuration: { 'java_main_class' => 'test-java-main-class' }
128+
).release
129+
130+
expect(command).to eq('test-java-home/bin/java -cp .:.lib/test-jar-1.jar:.lib/test-jar-2.jar test-java-main-class')
131+
end
132+
133+
end
134+
135+
it 'should release Spring boot applications with a JarLauncher in the MANIFEST.MF by specifying a port' do
136+
command = Main.new(
137+
app_dir: 'spec/fixtures/container_main_spring_boot_jar_launcher',
124138
java_home: 'test-java-home',
125139
java_opts: [],
126-
lib_directory: lib_directory,
127-
configuration: { 'java_main_class' => 'test-java-main-class' }
140+
configuration: {}
141+
).release
142+
143+
expect(command).to eq('test-java-home/bin/java -cp . org.springframework.boot.loader.JarLauncher --server.port=$PORT')
144+
end
145+
146+
it 'should release Spring boot applications with a WarLauncher in the MANIFEST.MF by specifying a port' do
147+
command = Main.new(
148+
app_dir: 'spec/fixtures/container_main_spring_boot_war_launcher',
149+
java_home: 'test-java-home',
150+
java_opts: [],
151+
configuration: {}
152+
).release
153+
154+
expect(command).to eq('test-java-home/bin/java -cp . org.springframework.boot.loader.WarLauncher --server.port=$PORT')
155+
end
156+
157+
it 'should release Spring boot applications with a JarLauncher in the configuration by specifying a port' do
158+
Dir.mktmpdir do |root|
159+
lib_directory = File.join(root, '.lib')
160+
Dir.mkdir lib_directory
161+
162+
command = Main.new(
163+
app_dir: root,
164+
java_home: 'test-java-home',
165+
java_opts: [],
166+
lib_directory: lib_directory,
167+
configuration: {
168+
'java_main_class' => 'org.springframework.boot.loader.JarLauncher',
169+
'arguments' => 'some arguments'
170+
}
128171
).release
129172

130-
expect(command).to eq('test-java-home/bin/java -cp .:.lib/test-jar-1.jar:.lib/test-jar-2.jar test-java-main-class')
173+
expect(command).to eq('test-java-home/bin/java -cp . org.springframework.boot.loader.JarLauncher some arguments --server.port=$PORT')
131174
end
175+
end
132176

177+
it 'should release Spring boot applications with a WarLauncher in the configuration by specifying a port' do
178+
Dir.mktmpdir do |root|
179+
lib_directory = File.join(root, '.lib')
180+
Dir.mkdir lib_directory
181+
182+
command = Main.new(
183+
app_dir: root,
184+
java_home: 'test-java-home',
185+
java_opts: [],
186+
lib_directory: lib_directory,
187+
configuration: {
188+
'java_main_class' => 'org.springframework.boot.loader.WarLauncher',
189+
'arguments' => 'some arguments'
190+
}
191+
).release
192+
193+
expect(command).to eq('test-java-home/bin/java -cp . org.springframework.boot.loader.WarLauncher some arguments --server.port=$PORT')
194+
end
133195
end
134196

135197
end

0 commit comments

Comments
 (0)