Skip to content

Commit 07908d6

Browse files
author
Glyn Normington
committed
Support java-main arguments
Add support for command line arguments to the java-main container. Improve the way java-main constructs the return value of release so that extraneoous spaces are not added. Document arguments configuration parameter and improve the layout of the java-main documentation. [#52116083]
1 parent 62f9621 commit 07908d6

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

docs/container-java-main.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Java Main Class Container
2-
The Java Main Class Container allows applications that provide a class with a `main()` method in it to be run. These applications are run with a command that looks like `./java/bin/java -cp . com.gopivotal.SampleClass`.
2+
The Java Main Class Container allows an application that provides a class with a `main()` method to be run. The application is executed with a command of the form:
3+
4+
./java/bin/java -cp . com.gopivotal.SampleClass
5+
6+
Command line arguments may optionally be configured.
37

48
<table>
59
<tr>
@@ -18,6 +22,7 @@ The container can be configured by modifying the [`config/main.yml`][main_yml] f
1822

1923
| Name | Description
2024
| ---- | -----------
25+
| `arguments` | Optional command line arguments to be passed to the Java main class.
2126
| `java_main_class` | The Java class name to run. Values containing whitespace are rejected with an error, but all others values appear without modification on the Java command line. If not specified, the Java Manifest value of `Main-Class` is used.
2227

2328

lib/java_buildpack/container/main.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,21 @@ def compile
5656
#
5757
# @return [String] the command to run the application.
5858
def release
59-
"#{@java_home}/bin/java -cp . #{java_opts} #{main_class}"
59+
"#{@java_home}/bin/java -cp .#{java_opts}#{space main_class}#{arguments}"
6060
end
6161

6262
private
6363

64-
CONFIGURATION_PROPERTY = 'java_main_class'.freeze
64+
MAIN_CLASS_PROPERTY = 'java_main_class'.freeze
65+
66+
ARGUMENTS_PROPERTY = 'arguments'.freeze
6567

6668
CONTAINER_NAME = 'java-main'.freeze
6769

6870
MANIFEST_PROPERTY = 'Main-Class'.freeze
6971

7072
def java_opts
71-
@java_opts.compact.sort.join(' ')
73+
space @java_opts.compact.sort.join(' ')
7274
end
7375

7476
def manifest
@@ -78,7 +80,15 @@ def manifest
7880
end
7981

8082
def main_class
81-
@configuration[CONFIGURATION_PROPERTY] || manifest[MANIFEST_PROPERTY]
83+
@configuration[MAIN_CLASS_PROPERTY] || manifest[MANIFEST_PROPERTY]
84+
end
85+
86+
def arguments
87+
space @configuration[ARGUMENTS_PROPERTY]
88+
end
89+
90+
def space(value)
91+
value.nil? || value == '' ? '' : ' ' + value
8292
end
8393

8494
end

spec/java_buildpack/container/main_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ module JavaBuildpack::Container
6060

6161
expect(command).to eq('test-java-home/bin/java -cp . test-opt-1 test-opt-2 test-java-main-class')
6262
end
63+
64+
it 'should return command line arguments when they are specified' do
65+
command = Main.new(
66+
:java_home => 'test-java-home',
67+
:java_opts => [],
68+
:configuration => { 'java_main_class' => 'test-java-main-class',
69+
'arguments' => 'some arguments'
70+
}).release
71+
72+
expect(command).to eq('test-java-home/bin/java -cp . test-java-main-class some arguments')
73+
end
6374
end
6475

6576
end

0 commit comments

Comments
 (0)