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.