0

I want to build a maven integrated project with a given java home property. At exactly i want to write a code in java that sets the java home property for an InvocationRequest object. The main goal is to build a project with the runtime given(by an algorithm) java home. So i would call getInvoker().execute(request); to execute maven goals where request is an InvocationRequest object.

I tried to set the request java.home property with
properties.setProperty(Goals.JAVA_HOME, javaHomePath); and call the method executeGoals(pom, new String[] { Goals.INSTALL, Goals.CLEAN }, properties); .
This executeGoals(...) method contains getInvoker().execute(request) call and the request object definition too.

Output is: Missing: 1) com.sun:tools:jar:1.5.0 #Solved

EDIT: solved the output problem, but a new one appeared: class file has wrong version 50.0, should be 49.0. Maybe i changed the jre home, so i think i'm compiling with a newer version of java than i'm running with.

Reminder: i want to build with a specified java home property = i want to change the compiler java home(or version) to the specified one. (In eclipse)
I would appreciate any help.

Diaby005
  • 1
  • 1
  • Please have a look at following question and the excepted answer: http://stackoverflow.com/questions/5756299/maven-3-artifact-problem – gclaussn Jan 29 '15 at 10:16

2 Answers2

0

I dont think it is configurable. It is part of the Maven Core to use the JAVA_HOME environment variable. Please see the accepted answer of: How to set specific java version to Maven

Also it is not possible to set or change environment variables (not system properties) within a Java process (for the current process). If you create another process from within your Java process, there will be methods to specify environment variables for this sub process.

Maybe the solution will be to execute a Maven command e.g. "mvn clean install" with a specific JAVA_HOME variable set as sub process (this requires, that Maven is installed and mvn is available as command). Use the ProcessBuilder to switch into the working directory, where the pom.xml of the target project is located and set the appropriate environment variable(s) before starting the process.

If Maven should not be installed at the enviroment your application is running on, you could also distribute a Maven installation with your application (maybe in a separate directory). Then you could run against the mvn.bat or mvn.sh of this distribution (depending on the os).

Community
  • 1
  • 1
gclaussn
  • 1,736
  • 16
  • 19
0

When using Eclipse, Build in Run as Configuration, go to the Environment tab and add the new JAVA_HOME variable. Do not forget to check the Replace native environment ..... option.

This will override your default OS variable. No need to change at the OS level.

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
Chandru
  • 1
  • 1