0

I'm developing an Eclipse plugin. This Eclipse plugin contains a New Project wizard and in the performFinish method of my wizard I want to use Maven (M2E) to execute an archetype:generate command.

I then found this resource and I'm able to get the maven invoker to generate archetypes programmatically, however it demands that I define a maven.home so that it knows where the mvn executable is.

If the best way to achieve this is to use maven invoker, how can I get the path to the embedded m2e mvn (m2e's maven.home)?

Ring
  • 2,249
  • 5
  • 27
  • 40

1 Answers1

1

Well - I only know that: you'll probably have to search for it. Mostly one of the environment variables M2_HOME, M3_HOME or MAVEN_HOME are set during the setup/installation process. But I'm not so sure, if that's always the case. If you can start maven directly from command prompt, it's probably on the PATH environment. So - search the environment for the Maven:

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

if (mavenHomePath == null) {
    Map<String,String> filteredMap = new HashMap<>();
    final List<String> searchEnv = Arrays.asList(new String[]{"m3_home", "m3home", "m2_home", "m2home", "maven_home", "mavenhome", "path"}); 
    for (final Map.Entry<String, String> entry : System.getenv().entrySet()) {
        final String key = entry.getKey().toLowerCase();
        if (searchEnv.contains(key)){
            filteredMap.put(key, entry.getValue());
        }
    }
    for (String key : searchEnv) {
        for (final String pathEnv : filteredMap.values()) {
            for (final String path : pathEnv.split(File.pathSeparator)) {
                mavenHomePath = checkForMavenHomeIn(new File(path));
                if (mavenHomePath != null) break;
            }
            if (mavenHomePath != null) break;
        }
        if (mavenHomePath != null) break;
    }
}

System.setProperty("maven.home", mavenHomePath);

invokeYourPlugIn();

Unfortunately "PATH" is not always named "PATH" but could be "Path" or even "path" - at least on Windows systems. It could also be m2_home etc. So - I filtered the environment variables ignoring the case. (I actually didn't look for maven, but svn - but I had similar problems). So - I have often read "set m2home" or "set m2_home" etc. - I'm not sure if there is a convention.

The checkForMavenHomeIn should look for the parent directory of bin. "path" would point to "bin", the others to the maven home directory.

Otherwise you will probably have to prompt the user for the directory and save it to some eclipse workspace preferences - but it would be good to perform a little pre-searching.

michael_s
  • 2,515
  • 18
  • 24
  • - well I just see, that it would be better to add a `if (mavenExePath != null) break;` to each for loop at the end - but it would clutter the code for now and I just wanted to make the point. If the other plug-in however requires one of the mxxhome environment variables set, there is no way to set that programmatically in the current java session. But you can set `maven.home` property before invoking the other plug-in. – michael_s Feb 10 '13 at 13:37