Skip to content

Commit 643fa27

Browse files
committed
Additional JARs on classpath
As part of the work to integrate Ratpack into the buildpack, an adjustment to the Groovy classpath has been made. Previously, Groovy applications were responsible for maintaining their own classpath. Instead, the buildpack should be doing this. The new behavior is that all JAR files (regardless of their location) will be added to the Groovy classpath at runtime. [#63888002]
1 parent 3a39cd5 commit 643fa27

10 files changed

Lines changed: 163 additions & 5 deletions

File tree

docs/container-groovy.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The Groovy Container allows uncompiled Groovy files (i.e. `*.groovy`) to be run.
1919
</table>
2020
Tags are printed to standard output by the buildpack detect script
2121

22+
Any JAR files found in the application are automatically added to the classpath at runtime.
23+
2224
## Configuration
2325
For general information on configuring the buildpack, refer to [Configuration and Extension][].
2426

lib/java_buildpack/container/groovy.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ def compile
4444

4545
# @macro base_component_release
4646
def release
47+
add_libs
48+
4749
[
4850
@droplet.java_home.as_env_var,
4951
@droplet.java_opts.as_env_var,
5052
qualify_path(@droplet.sandbox + 'bin/groovy', @droplet.root),
5153
@droplet.additional_libraries.as_classpath,
5254
relative_main_groovy,
5355
relative_other_groovy
54-
].compact.join(' ')
56+
].flatten.compact.join(' ')
5557
end
5658

5759
protected
@@ -63,6 +65,10 @@ def supports?
6365

6466
private
6567

68+
def add_libs
69+
(@droplet.root + '**/*.jar').glob.each { |jar| @droplet.additional_libraries << jar }
70+
end
71+
6672
def main_groovy
6773
candidates = JavaBuildpack::Util::GroovyUtils.groovy_files(@application)
6874

lib/java_buildpack/container/java_main.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def release
4747
main_class,
4848
arguments,
4949
port
50-
].compact.join(' ')
50+
].flatten.compact.join(' ')
5151
end
5252

5353
private

lib/java_buildpack/container/spring_boot_cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def release
4343
relative_groovy_files,
4444
'--',
4545
'--server.port=$PORT'
46-
].compact.join(' ')
46+
].flatten.compact.join(' ')
4747
end
4848

4949
protected

lib/java_buildpack/container/tomcat.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def release
7777
@droplet.java_opts.as_env_var,
7878
"$PWD/#{(@droplet.sandbox + 'bin/catalina.sh').relative_path_from(@droplet.root)}",
7979
'run'
80-
].compact.join(' ')
80+
].flatten.compact.join(' ')
8181
end
8282

8383
protected

lib/java_buildpack/util/play/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def release
5454
@droplet.java_home.as_env_var,
5555
qualify_path(start_script, @droplet.root),
5656
java_opts
57-
].compact.join(' ')
57+
].flatten.compact.join(' ')
5858
end
5959

6060
# @macro versioned_dependency_component_supports
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
class Alpha {
18+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.lang.management.ManagementFactory
18+
19+
20+
class Main {
21+
22+
static {
23+
if (System.getenv()['FAIL_INIT'] != null) {
24+
throw new RuntimeException('$FAIL_INIT caused initialisation to fail')
25+
}
26+
}
27+
28+
static void main(String[] args) {
29+
if (System.getenv()['FAIL_OOM'] != null) {
30+
Thread.start {
31+
println "Provoking OOM..."
32+
byte[] _ = new byte[Integer.MAX_VALUE]
33+
}
34+
}
35+
36+
def runtimeMxBean = ManagementFactory.getRuntimeMXBean()
37+
def data = new TreeMap()
38+
data["Class Path"] = runtimeMxBean.classPath.split(':')
39+
data["Environment Variables"] = System.getenv()
40+
data["Input Arguments"] = runtimeMxBean.inputArguments
41+
42+
map(data, new IndentingPrintStream(System.out))
43+
44+
println ''
45+
println "Sleeping for 1 minute..."
46+
Thread.sleep(60 * 1000)
47+
}
48+
49+
def static list(data, out) {
50+
data.each { value -> out.println value }
51+
}
52+
53+
def static map(data, out) {
54+
data.keySet().each { key ->
55+
out.println key
56+
57+
def value = data[key]
58+
def indented = out.indent()
59+
60+
if(value instanceof List) {
61+
list value, indented
62+
} else if (value.getClass().isArray()) {
63+
list Arrays.asList(value), indented
64+
} else if (value instanceof Map) {
65+
map value, indented
66+
} else if (value instanceof String) {
67+
indented.println value
68+
} else {
69+
indented.println "Unknown value type '" + value.getClass().simpleName + "'"
70+
}
71+
}
72+
}
73+
74+
static class IndentingPrintStream {
75+
76+
def indent
77+
78+
def out
79+
80+
IndentingPrintStream(PrintStream out) {
81+
this(0, out)
82+
}
83+
84+
IndentingPrintStream(int indent, PrintStream out) {
85+
this.indent = indent
86+
this.out = out
87+
}
88+
89+
void println(String s) {
90+
def sb = new StringBuilder()
91+
92+
for (int i = 0; i < indent; i++) {
93+
sb.append '\t'
94+
}
95+
96+
sb.append s
97+
98+
out.println sb.toString()
99+
}
100+
101+
IndentingPrintStream indent() {
102+
return new IndentingPrintStream(indent + 1, out)
103+
}
104+
}
105+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
class Beta {
18+
}

spec/java_buildpack/container/groovy_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@
9191
'directory/Beta.groovy')
9292
end
9393

94+
it 'should return command with included JARs',
95+
app_fixture: 'container_groovy_with_jars' do
96+
97+
expect(component.release).to eq("#{java_home.as_env_var} JAVA_OPTS=#{java_opts_str} $PWD/.java-buildpack/groovy/bin/groovy " +
98+
'-cp $PWD/.additional_libs/test-jar-1.jar:' +
99+
'$PWD/.additional_libs/test-jar-2.jar:$PWD/Alpha.jar:$PWD/directory/Beta.jar ' +
100+
'Application.groovy')
101+
end
102+
94103
def java_opts_str
95104
"\"#{java_opts.sort.join(' ')}\""
96105
end

0 commit comments

Comments
 (0)