Bug 1109980 - Way to override JAVA_HOME hardcoded in /usr/bin/mvn script
Summary: Way to override JAVA_HOME hardcoded in /usr/bin/mvn script
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: OpenShift Online
Classification: Red Hat
Component: Image
Version: 2.x
Hardware: Unspecified
OS: Linux
unspecified
high
Target Milestone: ---
: ---
Assignee: Ben Parees
QA Contact: libra bugs
URL:
Whiteboard:
: 1130718 (view as bug list)
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2014-06-16 19:23 UTC by Rémi Bantos
Modified: 2015-05-15 00:43 UTC (History)
5 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2014-10-10 00:47:49 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)

Description Rémi Bantos 2014-06-16 19:23:11 UTC
Description of problem:

As discussed here: https://community.jboss.org/thread/241939
I cannot maven build my Java 8 application with JDK8 using  Wildfly 8.1 cartridge after having followed these instructions: https://community.jboss.org/people/fjuma/blog/2014/06/03/wildfly-810final-on-openshift--with-jdk-8-support

I would like a way to override JAVA_HOME setting prior to maven build execution in cartridge.
This setting is currently hardcoded/set to JDK7 in /usr/bin/mvn script used by this cartridge (and probably others).


Version-Release number of selected component (if applicable):
WildFly 8.1.0.Final cartridge

How reproducible:
Always

Steps to Reproduce:
1. Create a Java 8 application with a POM containing a plugin which compiles application code and which uses the same JVM as the one used by maven build (for code generation purpose, such as this one: https://github.com/querydsl/apt-maven-plugin)
2. Add maven compiler fork configuration to use JDK 8, as described here: https://community.jboss.org/people/fjuma/blog/2014/06/03/wildfly-810final-on-openshift--with-jdk-8-support
3. Create a class using specific Java 8 features in application, such as lambda expressions
4. Run the build

Actual results:
Plugin execution fails, during code compilation, as it uses JDK7 running maven build script.

Expected results:
With JAVA_HOME set to JDK8 as described bellow, build would perform successfully:
https://community.jboss.org/people/fjuma/blog/2014/06/03/wildfly-810final-on-openshift--with-jdk-8-support

Additional info:

Comment 1 Ben Parees 2014-06-16 20:37:33 UTC
The /usr/bin/mvn file is part of the maven3-3.0.4 rpm and precludes overriding JAVA_HOME which is used by maven.  Need to track down the package maintainer and find out what the point of that is.

Note that for now, other carts are not affected because we haven't enabled a jdk8 marker for them (but in theory you can't force them to maven build with jdk6 right now, either)

Comment 2 Ben Parees 2014-08-16 22:24:37 UTC
*** Bug 1130718 has been marked as a duplicate of this bug. ***

Comment 3 Harit Himanshu 2014-08-16 23:40:37 UTC
what's the resolution for this situation then? can we not use JAVA 8?

Comment 4 Ben Parees 2014-08-16 23:48:00 UTC
There is at the moment no simple resolution.  In theory you could fork the wildfly-8 cartridge and modify it's "mvn" invocation (in the "control" script) to invoke your own mvn wrapper script that respected JAVA_HOME, instead of the /usr/bin/mvn script which does not.

Alternatively you could use the existing cartridge and try creating a pre build hook that modified your path such that your custom mvn wrapper script appeared before /usr/bin/mvn (and then put you custom mvn script somewhere in your gear)

Comment 5 Ben Parees 2014-08-16 23:48:43 UTC
*** Bug 1130718 has been marked as a duplicate of this bug. ***

Comment 6 Ben Parees 2014-08-16 23:50:29 UTC
Note that /usr/bin/mvn is pretty simple:
#!/bin/sh
prog=$(basename $0)
export JAVA_HOME=/usr/lib/jvm/java
export JAVACMD=$JAVA_HOME/bin/java
export M2_HOME=/usr/share/java/apache-maven-3.0.4
exec $M2_HOME/bin/$prog "$@"

So if you want to write your own that respects the JAVA_HOME env variable properly, you can start from that template.

However the "right" fix for this issue is probably to get the upstream /usr/bin/mvn fixed such that it respects JAVA_HOME.

Comment 7 Harit Himanshu 2014-08-17 00:04:31 UTC
Are you recommending a pre-build hook which runs custom maven?

Comment 8 Ben Parees 2014-08-17 00:12:23 UTC
No, just a pre-build hook that sets $PATH to something like:
PATH=$OPENSHIFT_DATA_DIR/bin/my_mvn_script:$PATH

and the creating a my_mvn_script based off the /usr/bin/mvn script, but that respects JAVA_HOME.

you could also put the my_mvn_script in your app repo and put it in your path at that location.

Comment 9 Harit Himanshu 2014-08-17 03:44:43 UTC
Still doesn't seem to work Am I doing anything wrong?  

$ cat ~/app-root/data/bin/maven_jdk8 
#!/bin/sh
prog=$(basename $0)
export JAVA_HOME=$JAVA_HOME
export JAVACMD=$JAVA_HOME/bin/java
export M2_HOME=/usr/share/java/apache-maven-3.0.4
exec $M2_HOME/bin/$prog "$@"

$ PATH=$OPENSHIFT_DATA_DIR/bin/maven_jdk8:$PATH
$ echo $PATH
/var/lib/openshift/53ec289c5973ca0333000a6c/app-root/data//bin/maven_jdk8:/var/lib/openshift/53ec289c5973ca0333000a6c/app-root/data//bin/maven_jdk8:/var/lib/openshift/53ec289c5973ca0333000a6c/wildfly/usr/lib/jvm/jdk1.8.0_05/bin:/etc/alternatives/maven-3.0/bin:/var/lib/openshift/53ec289c5973ca0333000a6c/wildfly//bin/tools:/bin:/usr/bin:/usr/sbin

$ pwd
/var/lib/openshift/53ec289c5973ca0333000a6c/app-root/repo

$ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building pennyapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ pennyapp ---
[INFO] Deleting /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/runtime/repo/target
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ pennyapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/runtime/repo/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pennyapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/runtime/repo/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.135s
[INFO] Finished at: Sat Aug 16 23:44:32 EDT 2014
[INFO] Final Memory: 7M/111M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project pennyapp: Fatal error compiling: invalid target release: 1.8 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Comment 10 Ben Parees 2014-08-17 03:54:37 UTC
yeah you're missing some arguments, here's what our build script does:
https://github.com/openshift-cartridges/openshift-wildfly-cartridge/blob/master/bin/control#L249
https://github.com/openshift-cartridges/openshift-wildfly-cartridge/blob/master/bin/control#L257

so if you want to match it by hand, you'll need to pass the same args to maven.

you could also just run the "wildfly/bin/control build" to trigger the mvn invocation (since you've setup your path, the build script will use the mvn from your path...you'll notice it doesn't specify an absolute path to the mvn executable)

Comment 11 Ben Parees 2014-08-17 03:56:02 UTC
fyi I'm probably going to go ahead and submit a PR to the WF cartridge to include a custom mvn script and change the control script to use the custom script, but it probably won't be until monday.

I still think the "right" way to fix this is to get the /usr/bin/mvn script that's packaged in the maven3 package, fixed.

Comment 12 Harit Himanshu 2014-08-17 04:13:53 UTC
I agree with you Ben and thanks so much for your support over the weekend, very much appreciated.

Meanwhile I will try what you have mentioned and see if I can get it work

Also, keep me posted about the PR

Comment 13 Harit Himanshu 2014-08-17 04:21:02 UTC
I logged into the machine and did following, still doesn't work


[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> cat $OPENSHIFT_DATA_DIR/bi/mvn
cat: /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/data//bi/mvn: No such file or directory
[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> cat $OPENSHIFT_DATA_DIR/bin/mvn
#!/bin/sh
prog=$(basename $0)
export JAVA_HOME=$JAVA_HOME
export JAVACMD=$JAVA_HOME/bin/java
export M2_HOME=/usr/share/java/apache-maven-3.0.4
exec $M2_HOME/bin/$prog "$@"
[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> echo $JAVA_HOME
/var/lib/openshift/53ec289c5973ca0333000a6c/wildfly/usr/lib/jvm/jdk1.8.0_05
[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> export PATH=$OPENSHIFT_DATA_DIR/bin/mvn:$PATH
[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> echo $PATH
/var/lib/openshift/53ec289c5973ca0333000a6c/app-root/data//bin/mvn:/var/lib/openshift/53ec289c5973ca0333000a6c/wildfly/usr/lib/jvm/jdk1.8.0_05/bin:/etc/alternatives/maven-3.0/bin:/var/lib/openshift/53ec289c5973ca0333000a6c/wildfly//bin/tools:/bin:/usr/bin:/usr/sbin
[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> wildfly/bin/control build
Found pom.xml... attempting to build with 'mvn -e clean package -Popenshift -DskipTests'
Apache Maven 3.0.4 (r1232336; 2012-12-18 14:36:37-0500)
Maven home: /usr/share/java/apache-maven-3.0.4
Java version: 1.7.0_65, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.65/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-431.23.3.el6.x86_64", arch: "i386", family: "unix"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building pennyapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ pennyapp ---
[INFO] Deleting /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/runtime/repo/target
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ pennyapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/runtime/repo/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pennyapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/runtime/repo/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.212s
[INFO] Finished at: Sun Aug 17 00:20:27 EDT 2014
[INFO] Final Memory: 7M/111M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "openshift" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project pennyapp: Fatal error compiling: invalid target release: 1.8 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Comment 14 Ben Parees 2014-08-17 04:50:25 UTC
Not sure what to tell you as it appears to be working for me (see below).

did you make your new mvn script executable?  if you do "which mvn" does it point to your script?


[wildfly-bparees.dev.rhcloud.com 53f06b07a332b5a9d1000168]\> cat /tmp/mvn
#!/bin/sh
prog=$(basename $0)
export JAVA_HOME=$JAVA_HOME
export JAVACMD=$JAVA_HOME/bin/java
export M2_HOME=/usr/share/java/apache-maven-3.0.4
exec $M2_HOME/bin/$prog "$@"

[wildfly-bparees.dev.rhcloud.com 53f06b07a332b5a9d1000168]\> export PATH=/tmp:$PATH

[wildfly-bparees.dev.rhcloud.com 53f06b07a332b5a9d1000168]\> which mvn
/tmp/mvn

[wildfly-bparees.dev.rhcloud.com 53f06b07a332b5a9d1000168]\> wildfly/bin/control build
Found pom.xml... attempting to build with 'mvn -e clean package -Popenshift -DskipTests'
Apache Maven 3.0.4 (r1232336; 2012-12-18 14:36:37-0500)
Maven home: /usr/share/java/apache-maven-3.0.4
Java version: 1.8.0_05, vendor: Oracle Corporation
Java home: /var/lib/openshift/53f06b07a332b5a9d1000168/wildfly/usr/lib/jvm/jdk1.8.0_05/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-431.23.3.el6.x86_64", arch: "i386", family: "unix"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building wildfly 1.0
[INFO] ------------------------------------------------------------------------

Comment 15 Harit Himanshu 2014-08-17 05:28:11 UTC
chmod not working, sudo not available, what's missing?  

[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> ls -ltr $OPENSHIFT_DATA_DIR/bin
total 8
-rwx------. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 172 Aug 16 23:29 maven_jdk8
-rwx------. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 172 Aug 16 23:47 mvn
[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> sudo chmod +x  $OPENSHIFT_DATA_DIR/bin/mvn
bash: /usr/bin/sudo: Permission denied
[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> chmod +x  $OPENSHIFT_DATA_DIR/bin/mvn
[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> ls -ltr $OPENSHIFT_DATA_DIR/bin
total 8
-rwx------. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 172 Aug 16 23:29 maven_jdk8
-rwx------. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 172 Aug 16 23:47 mvn

Comment 16 Harit Himanshu 2014-08-17 05:36:55 UTC
chmod a+x was all I need

Comment 17 Harit Himanshu 2014-08-17 05:54:57 UTC
pre_build hook doesn't seem to work, am I missing anything?  

[api-pennyapp.rhcloud.com 53ec289c5973ca0333000a6c]\> cd ~/app-root/repo/
[api-pennyapp.rhcloud.com repo]\> ls
pom.xml  README.md  shippable.yml  src	target
[api-pennyapp.rhcloud.com repo]\> ls -la
total 36
drwxr-x---. 5 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 4096 Aug 17 01:51 .
drwxr-x---. 5 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 4096 Aug 17 01:51 ..
-rw-------. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c   21 Aug 17 01:50 .gitignore
drwx------. 3 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 4096 Aug 17 01:50 .openshift
-rw-------. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 3153 Aug 17 01:50 pom.xml
-rw-------. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c  235 Aug 17 01:50 README.md
-rw-------. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c  453 Aug 17 01:50 shippable.yml
drwx------. 4 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 4096 Aug 17 01:50 src
drwx------. 5 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 4096 Aug 17 01:51 target
[api-pennyapp.rhcloud.com repo]\> cat .openshift/action_hooks/pre_build 
export PATH=$OPENSHIFT_DATA_DIR/bin/mvn:$PATH[api-pennyapp.rhcloud.com repo]\> which mvn
/usr/bin/mvn
[api-pennyapp.rhcloud.com repo]\> ls -ltr /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/data/bin/mvn
-rwx--x--x. 1 53ec289c5973ca0333000a6c 53ec289c5973ca0333000a6c 172 Aug 16 23:47 /var/lib/openshift/53ec289c5973ca0333000a6c/app-root/data/bin/mvn

Comment 18 Ben Parees 2014-08-17 16:39:20 UTC
I've gone ahead and fixed the wildfly cartridge with a custom mvn script:
https://github.com/openshift-cartridges/openshift-wildfly-cartridge/pull/19

If you recreate your app you should get the desired behavior without having to do any manual workarounds.

Comment 19 Harit Himanshu 2014-08-17 16:43:14 UTC
Man!, that was quick, I will retry now and let you know

Comment 20 Harit Himanshu 2014-08-17 17:19:06 UTC
Ever since I have created a new app, I am facing this issue

git push -f openshift $BRANCH:master
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.


I re-added ssh keys and restarted the application as well, but no luck

Comment 21 Ben Parees 2014-08-17 17:29:26 UTC
Can you wipe out the local git repo on your client machine, and then re-clone it (rhc git-clone -a <appname>?

Comment 22 Harit Himanshu 2014-08-17 20:38:43 UTC
The issue was that my git remote openshift was referring to old app git repo.

I corrected that.  

I double checked and verified that everything works out fine. Thank you so much for your help.

Very much appreciated your efforts!

Comment 23 Ben Parees 2014-08-17 21:11:41 UTC
Yeah that's what i figured, that's why i suggested wiping it out and re-cloning it.  Glad things are working!

Comment 24 chunchen 2014-08-18 08:31:10 UTC
It's fixed, please refer to the following results:

1. Create a wildfly app
rhc app create wildfly https://cartreflect-claytondev.rhcloud.com/reflect?github=openshift-cartridges/openshift-wildfly-cartridge

2. Do some changes in the app local repo and perform git push
cd wildfly
touch test.txt
git add . && git commit -amp && git push

[master 997bbc9] p
 0 files changed
 create mode 100644 test.txt
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 248 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Stopping wildfly cart
remote: Sending SIGTERM to wildfly:14564 ...
remote: Building git ref 'master', commit 997bbc9
remote: Found pom.xml... attempting to build with 'mvn -e clean package -Popenshift -DskipTests'
remote: Apache Maven 3.0.4 (r1232336; 2012-12-18 14:36:37-0500)
remote: Maven home: /usr/share/java/apache-maven-3.0.4
remote: Java version: 1.8.0_05, vendor: Oracle Corporation
remote: Java home: /var/lib/openshift/53f1ec1aa91a771ef1000060/wildfly/usr/lib/jvm/jdk1.8.0_05/jre
remote: Default locale: en_US, platform encoding: ANSI_X3.4-1968
remote: OS name: "linux", version: "2.6.32-431.23.3.el6.x86_64", arch: "i386", family: "unix"
remote: [INFO] Scanning for projects...
remote: [INFO]                                                                         
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] Building wildfly 1.0
remote: [INFO] 

<------snip------>

remote: [INFO] Packaging webapp
remote: [INFO] Assembling webapp [wildfly] in [/var/lib/openshift/53f1ec1aa91a771ef1000060/app-root/runtime/repo/target/wildfly]
remote: [INFO] Processing war project
remote: [INFO] Copying webapp resources [/var/lib/openshift/53f1ec1aa91a771ef1000060/app-root/runtime/repo/src/main/webapp]
remote: [INFO] Webapp assembled in [119 msecs]
remote: [INFO] Building war: /var/lib/openshift/53f1ec1aa91a771ef1000060/app-root/runtime/repo/deployments/ROOT.war
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] BUILD SUCCESS
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] Total time: 7.204s
remote: [INFO] Finished at: Mon Aug 18 08:26:17 EDT 2014
remote: [INFO] Final Memory: 7M/17M
remote: [INFO] ------------------------------------------------------------------------
remote: Preparing build for deployment
remote: Deployment id is 91afce2c
remote: Activating deployment
remote: Deploying WildFly
remote: Starting wildfly cart
remote: Found 127.1.247.129:8080 listening port
remote: Found 127.1.247.129:9990 listening port
remote: /var/lib/openshift/53f1ec1aa91a771ef1000060/wildfly/standalone/deployments /var/lib/openshift/53f1ec1aa91a771ef1000060/wildfly
remote: /var/lib/openshift/53f1ec1aa91a771ef1000060/wildfly
remote: CLIENT_MESSAGE: Artifacts deployed: ./ROOT.war
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://53f1ec1aa91a771ef1000060.rhcloud.com/~/git/wildfly.git/
   74cefe3..997bbc9  master -> master


Note You need to log in before you can comment on or make changes to this bug.