You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by "Brian E. Fox" <br...@reply.infinity.nu> on 2007/07/07 18:20:27 UTC

finding maven.home in the invoker

I recently had some trouble with the invoker getting a good maven.home
value from the mvn script. I came across this code in the eclipse tests
that seems to work pretty well. Is there any objection to moving this
code into the invoker in a fall back scenario? (ie if the executable
can't be found via the normal means, try this) The code is basically
walking through the path to find mvn...in most cases a user will have
mvn on their path so it seems like a good assumption.

 

        String mavenHome = System.getProperty( "maven.home" );

 

        // maven.home is set by surefire when the test is run with
maven, but better make the test run in IDEs without

        // the need of additional properties

        if ( mavenHome == null )

        {

            String path = System.getProperty( "java.library.path" );

            String[] paths = StringUtils.split( path,
System.getProperty( "path.separator" ) );

            for ( int j = 0; j < paths.length; j++ )

            {

                String pt = paths[j];

                if ( new File( pt, "mvn" ).exists() )

                {

                    System.setProperty( "maven.home", new File( pt
).getAbsoluteFile().getParent() );

                    break;

                }

 

            }

        }


RE: finding maven.home in the invoker

Posted by "Brian E. Fox" <br...@reply.infinity.nu>.
I was confused when I first looked at the code also, but on XP at least it does work. (I stepped through it with the debugger).

The details on the problems I found are in MNG-3085, but in short sometimes the variable is retrieved literally as "${env.M2_HOME}" so it's not resolving correctly somewhere. This happens with the 2.0.6 and 2.0.7-SNAPSHOT invoker.

-----Original Message-----
From: Kenney Westerhof [mailto:kenney@apache.org] 
Sent: Sunday, July 08, 2007 7:55 AM
To: Maven Developers List
Subject: Re: finding maven.home in the invoker



Brian E. Fox wrote:
> I recently had some trouble with the invoker getting a good maven.home
> value from the mvn script. I came across this code in the eclipse tests
> that seems to work pretty well. Is there any objection to moving this
> code into the invoker in a fall back scenario? (ie if the executable
> can't be found via the normal means, try this) The code is basically
> walking through the path to find mvn...in most cases a user will have
> mvn on their path so it seems like a good assumption.

The code below is bad. The system path is NOT set in java.library.path - this
is for dll/so files (jni stuff).

Assuming maven is in the path when no env var can be found is ok though,
i'm not sure why the maven home is even needed.

Btw, i already updated both the invoker and the verifier to also
check the M2_HOME env var, which should fix things on some installations.

Back to the code below. If it's changed to 'path = System.getenv( "PATH" )
it may work.

However, if you look at the 'mvn' script, it sets the maven.home variable:

exec "$JAVACMD" \
  $MAVEN_OPTS \
  -classpath "${M2_HOME}"/lib/maven-embedder-*.jar \
  "-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
  "-Dmaven.home=${M2_HOME}"  \
  ${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS

so ideally the maven.home property should just work. I don't know why it doesn't,
but maybe we should look into that as maven just provides this information
since version 2.0.

-- Kenney

> 
>  
> 
>         String mavenHome = System.getProperty( "maven.home" );
> 
>  
> 
>         // maven.home is set by surefire when the test is run with
> maven, but better make the test run in IDEs without
> 
>         // the need of additional properties
> 
>         if ( mavenHome == null )
> 
>         {
> 
>             String path = System.getProperty( "java.library.path" );
> 
>             String[] paths = StringUtils.split( path,
> System.getProperty( "path.separator" ) );
> 
>             for ( int j = 0; j < paths.length; j++ )
> 
>             {
> 
>                 String pt = paths[j];
> 
>                 if ( new File( pt, "mvn" ).exists() )
> 
>                 {
> 
>                     System.setProperty( "maven.home", new File( pt
> ).getAbsoluteFile().getParent() );
> 
>                     break;
> 
>                 }
> 
>  
> 
>             }
> 
>         }
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: finding maven.home in the invoker

Posted by Kenney Westerhof <ke...@apache.org>.

Brian E. Fox wrote:
> I recently had some trouble with the invoker getting a good maven.home
> value from the mvn script. I came across this code in the eclipse tests
> that seems to work pretty well. Is there any objection to moving this
> code into the invoker in a fall back scenario? (ie if the executable
> can't be found via the normal means, try this) The code is basically
> walking through the path to find mvn...in most cases a user will have
> mvn on their path so it seems like a good assumption.

The code below is bad. The system path is NOT set in java.library.path - this
is for dll/so files (jni stuff).

Assuming maven is in the path when no env var can be found is ok though,
i'm not sure why the maven home is even needed.

Btw, i already updated both the invoker and the verifier to also
check the M2_HOME env var, which should fix things on some installations.

Back to the code below. If it's changed to 'path = System.getenv( "PATH" )
it may work.

However, if you look at the 'mvn' script, it sets the maven.home variable:

exec "$JAVACMD" \
  $MAVEN_OPTS \
  -classpath "${M2_HOME}"/lib/maven-embedder-*.jar \
  "-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
  "-Dmaven.home=${M2_HOME}"  \
  ${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS

so ideally the maven.home property should just work. I don't know why it doesn't,
but maybe we should look into that as maven just provides this information
since version 2.0.

-- Kenney

> 
>  
> 
>         String mavenHome = System.getProperty( "maven.home" );
> 
>  
> 
>         // maven.home is set by surefire when the test is run with
> maven, but better make the test run in IDEs without
> 
>         // the need of additional properties
> 
>         if ( mavenHome == null )
> 
>         {
> 
>             String path = System.getProperty( "java.library.path" );
> 
>             String[] paths = StringUtils.split( path,
> System.getProperty( "path.separator" ) );
> 
>             for ( int j = 0; j < paths.length; j++ )
> 
>             {
> 
>                 String pt = paths[j];
> 
>                 if ( new File( pt, "mvn" ).exists() )
> 
>                 {
> 
>                     System.setProperty( "maven.home", new File( pt
> ).getAbsoluteFile().getParent() );
> 
>                     break;
> 
>                 }
> 
>  
> 
>             }
> 
>         }
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org