11package com .redhat .hacbs .container .build .preprocessor ;
22
3+ import static org .apache .commons .lang3 .StringUtils .isEmpty ;
34import static org .apache .commons .lang3 .StringUtils .isNotEmpty ;
45
56import java .io .IOException ;
@@ -32,8 +33,8 @@ public abstract class AbstractPreprocessor implements Runnable {
3233 @ CommandLine .Option (names = "--request-processor-image" , required = true )
3334 String buildRequestProcessorImage ;
3435
35- @ CommandLine .Option (names = "--java-home " , required = true )
36- String javaHome ;
36+ @ CommandLine .Option (names = "--java-version " , required = true )
37+ String javaVersion ;
3738
3839 @ CommandLine .Option (names = "--build-tool-version" , required = true )
3940 String buildToolVersion ;
@@ -54,45 +55,66 @@ public String toString() {
5455
5556 @ Override
5657 public void run () {
57-
58- Log .warnf ("### Using tool %s with version %s and javaHome %s" , type , buildToolVersion , javaHome );
59- Log .warnf ("### ENV %s" , System .getenv ("jvm-build-service" ));
60- Log .warnf ("### BUILD_SCRIPT %s" , System .getenv ("BUILD_SCRIPT" ));
61-
58+ Path workspaceSource = buildRoot .getParent ();
6259 Path jbsDirectory = Path .of (buildRoot .toString (), ".jbs" );
6360 //noinspection ResultOfMethodCallIgnored
6461 jbsDirectory .toFile ().mkdirs ();
6562
63+ String buildScript = System .getenv ("BUILD_SCRIPT" );
64+ Log .warnf ("### BUILD_SCRIPT %s" , buildScript );
65+ if (isEmpty (buildScript )) {
66+ Log .errorf ("Unable to find BUILD_SCRIPT in environment" );
67+ throw new RuntimeException ("No BUILD_SCRIPT found" );
68+ }
69+
70+ Log .warnf ("### WorkspaceSource %s" , workspaceSource );
71+ Log .warnf ("### Using tool %s with version %s and javaHome %s" , type , buildToolVersion , javaVersion );
72+ Log .warnf ("### ENV %s" , System .getenv ("jvm-build-service" ));
73+
74+ String javaHome = "" ;
75+ if (javaVersion .equals ("7" ) || javaVersion .equals ("8" )) {
76+ javaHome = "/lib/jvm/java-1." + javaVersion + ".0" ;
77+ } else {
78+ javaHome = "/lib/jvm/java-" + javaVersion ;
79+ }
80+
81+
82+ // TODO: Rename CACHE_URL to PROXY_URL to cover both Indy and JBS use-cases
6683 String runBuild = """
6784 #!/usr/bin/env bash
6885 set -o verbose
6986 set -eu
7087 set -o pipefail
7188
89+ #fix this when we no longer need to run as root
90+ export HOME=/root
91+
7292 export LANG="en_US.UTF-8"
7393 export LC_ALL="en_US.UTF-8"
7494 export JAVA_HOME=%s
7595 # This might get overridden by the tool home configuration below. This is
7696 # useful if Gradle/Ant also requires Maven configured.
7797 export MAVEN_HOME=/opt/maven/3.8.8
98+ # If we run out of memory we want the JVM to die with error code 134
99+ export MAVEN_OPTS="-XX:+CrashOnOutOfMemoryError"
100+ # If we run out of memory we want the JVM to die with error code 134
101+ export JAVA_OPTS="-XX:+CrashOnOutOfMemoryError"
78102 export %s_HOME=/opt/%s/%s
79103
104+ mkdir -p /var/workdir/workspace/artifacts /var/workdir/workspace/logs /var/workdir/workspace/packages /var/workdir/software/settings ${HOME}/.sbt/1.0
80105 cd %s
81- mkdir -p ../logs ../packages
82106
83107 if [ ! -z ${JAVA_HOME+x} ]; then
84108 echo "JAVA_HOME:$JAVA_HOME"
85109 PATH="${JAVA_HOME}/bin:$PATH"
86110 fi
87111
88112 if [ ! -z ${MAVEN_HOME+x} ]; then
89- echo "MAVEN_HOME:$MAVEN_HOME"
90- PATH="${MAVEN_HOME}/bin:$PATH"
113+ """ .formatted (javaHome , type .name (), type , buildToolVersion , buildRoot );
91114
92- if [ ! -d "${MAVEN_HOME}" ]; then
93- echo "Maven home directory not found at ${MAVEN_HOME}" >&2
94- exit 1
95- fi
115+ runBuild += getMavenSetup ();
116+
117+ runBuild += """
96118 fi
97119
98120 if [ ! -z ${GRADLE_HOME+x} ]; then
@@ -113,6 +135,29 @@ public void run() {
113135 echo "Ant home directory not found at ${ANT_HOME}" >&2
114136 exit 1
115137 fi
138+
139+ if [ ! z ${CACHE_URL+x} ]; then
140+ cat > ivysettings.xml << EOF
141+ <ivysettings>
142+ <property name="cache-url" value="${CACHE_URL}"/>
143+ <property name="default-pattern" value="[organisation]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"/>
144+ <property name="local-pattern" value="\\ ${user.home}/.m2/repository/[organisation]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"/>
145+ <settings defaultResolver="defaultChain"/>
146+ <resolvers>
147+ <ibiblio name="default" root="\\ ${cache-url}" pattern="\\ ${default-pattern}" m2compatible="true"/>
148+ <filesystem name="local" m2compatible="true">
149+ <artifact pattern="\\ ${local-pattern}"/>
150+ <ivy pattern="\\ ${local-pattern}"/>
151+ </filesystem>
152+ <chain name="defaultChain">
153+ <resolver ref="local"/>
154+ <resolver ref="default"/>
155+ </chain>
156+ </resolvers>
157+ </ivysettings>
158+ EOF
159+ fi
160+
116161 fi
117162
118163 if [ ! -z ${SBT_DIST+x} ]; then
@@ -123,16 +168,31 @@ public void run() {
123168 echo "SBT home directory not found at ${SBT_DIST}" >&2
124169 exit 1
125170 fi
171+
172+ if [ ! z ${CACHE_URL+x} ]; then
173+ cat > "$HOME/.sbt/repositories" <<EOF
174+ [repositories]
175+ local
176+ my-maven-proxy-releases: ${CACHE_URL}
177+ EOF
178+ fi
179+ # TODO: we may need .allowInsecureProtocols here for minikube based tests that don't have access to SSL
180+ cat >"$HOME/.sbt/1.0/global.sbt" <<EOF
181+ publishTo := Some(("MavenRepo" at s"file:%s/artifacts")),
182+ EOF
183+
184+
126185 fi
127186 echo "PATH:$PATH"
128187
129- #fix this when we no longer need to run as root
130- export HOME=/root
188+ """ .formatted (workspaceSource );
131189
190+ Log .warnf ("### runBuild is %s" , runBuild );
191+
192+
193+ runBuild += buildScript ;
132194
133- """ .formatted (javaHome , type .name (), type , buildToolVersion , buildRoot );
134195
135- Log .warnf ("### runBuild is %s" , runBuild );
136196
137197 String containerFile = """
138198 FROM %s
@@ -177,11 +237,110 @@ public void run() {
177237 COPY --from=0 /var/workdir/workspace/artifacts /
178238 """ ;
179239 }
240+
241+
242+
180243 try {
244+ Files .writeString (Paths .get (jbsDirectory .toString (), "run-build.sh" ), runBuild );
181245 Files .writeString (Paths .get (jbsDirectory .toString (), "Containerfile" ), containerFile );
182246 } catch (IOException e ) {
183247 Log .errorf ("Unable to write Containerfile" , e );
184248 throw new RuntimeException (e );
185249 }
186250 }
251+
252+
253+ private String getMavenSetup () {
254+ return """
255+ echo "MAVEN_HOME:$MAVEN_HOME"
256+ PATH="${MAVEN_HOME}/bin:$PATH"
257+
258+ if [ ! -d "${MAVEN_HOME}" ]; then
259+ echo "Maven home directory not found at ${MAVEN_HOME}" >&2
260+ exit 1
261+ fi
262+
263+ if [ ! z ${CACHE_URL+x} ]; then
264+ cat >"/var/workdir/software/settings"/settings.xml <<EOF
265+ <settings>
266+ <mirrors>
267+ <mirror>
268+ <id>mirror.default</id>
269+ <url>${CACHE_URL}</url>
270+ <mirrorOf>*</mirrorOf>
271+ </mirror>
272+ </mirrors>
273+ EOF
274+ else
275+ cat >"/var/workdir/software/settings"/settings.xml <<EOF
276+ <settings>
277+ EOF
278+ fi
279+ cat >>"/var/workdir/software/settings"/settings.xml <<EOF
280+ <!-- Off by default, but allows a secondary Maven build to use results of prior (e.g. Gradle) deployment -->
281+ <profiles>
282+ <profile>
283+ <id>secondary</id>
284+ <activeByDefault>true</activeByDefault>
285+ </activation>
286+ <repositories>
287+ <repository>
288+ <id>artifacts</id>
289+ <url>file:///var/workdir/workspace/artifacts</url>
290+ <releases>
291+ <enabled>true</enabled>
292+ <checksumPolicy>ignore</checksumPolicy>
293+ </releases>
294+ </repository>
295+ </repositories>
296+ <pluginRepositories>
297+ <pluginRepository>
298+ <id>artifacts</id>
299+ <url>file:///var/workdir/workspace/artifacts</url>
300+ <releases>
301+ <enabled>true</enabled>
302+ <checksumPolicy>ignore</checksumPolicy>
303+ </releases>
304+ </pluginRepository>
305+ </pluginRepositories>
306+ </profile>
307+ </profiles>
308+ </settings>
309+ EOF
310+
311+
312+ TOOLCHAINS_XML="/var/workdir/software/settings"/toolchains.xml
313+
314+ cat >"$TOOLCHAINS_XML" <<EOF
315+ <?xml version="1.0" encoding="UTF-8"?>
316+ <toolchains>
317+ EOF
318+
319+ if [ "%s" = "7" ]; then
320+ JAVA_VERSIONS="7:1.7.0 8:1.8.0 11:11"
321+ else
322+ JAVA_VERSIONS="8:1.8.0 9:11 11:11 17:17 21:21 22:22"
323+ fi
324+
325+ for i in $JAVA_VERSIONS; do
326+ version=$(echo $i | cut -d : -f 1)
327+ home=$(echo $i | cut -d : -f 2)
328+ cat >>"$TOOLCHAINS_XML" <<EOF
329+ <toolchain>
330+ <type>jdk</type>
331+ <provides>
332+ <version>$version</version>
333+ </provides>
334+ <configuration>
335+ <jdkHome>/usr/lib/jvm/java-$home-openjdk</jdkHome>
336+ </configuration>
337+ </toolchain>
338+ EOF
339+ done
340+
341+ cat >>"$TOOLCHAINS_XML" <<EOF
342+ </toolchains>
343+ EOF
344+ """ .formatted (javaVersion );
345+ }
187346}
0 commit comments