Maven3 system.property user.home vs environment variable HOME - maven

There seems to be a problem with the system properties in Maven. It seems that system properties don't correspond to the environment variables.
The situation is that I would like to the change the absolute location of the user-specific settings.xml file, as the Maven document says
If you need to create user-specific settings from scratch, it’s
easiest to copy the global settings from your Maven installation to
your ${user.home}/.m2 directory
which means that the only way to change the absolute location of the user-specific file is to change the user.home property. But it seems that this is not exactly the HOME or HOMEPATH environment variable. In fact, I deleted all the environment variables such as HOME and HOMEPATH, and run mvn using mvn -X just to see the user-specific settings.xml that mvn uses. The result is the following (I am in Windows 7 and I run the command in cmd.exe):
Maven still finds a user.home property, however, I haven't set such a environment variable. To verify this, I then run the set command to see all the environment variables but find nothing about the path d:\Userfiles\xili\ which is used by Maven as the user.home system property. By the way, there is no settings.xml file in this path, because I haven't put any file in this path.
How maven figure out such a path as user.home?

The answer is here!
http://www.timehat.com/javas-user-home-is-wrong-on-windows/
In fact, in Windows, the JVM uses the PATH_TO_DESKTOP_FOLDER_AS_SET_IN_THE_REGISTRY as the reference to determine wheres is the user.home.
user.home is just the parent folder of the above path. That't it, this has nothing to do with the HOME or HOMEPATH enviroment variable.
In the source code of Maven, there should be some code like System.getProperty('user.home').

Related

Error while run the maven framework through command prompt but it's running fine with eclipse

I faced the issue with the surefire plugin "2.19.1" as below. I have tried with the version 2.19.1,2.12.4,3.0.0-M1
You're maven seems to be corrupt. Assuming you're on a windows machine:
1) set M2_HOME environment variable to the folder where maven is. (if in doubt download it and put it somewhere, it's just a java program.
2) set JAVA_HOME environment variable to the folder where your java is.
3) add %JAVA_HOME%/bin:%M2_HOME%/bin to the start of your PATH environment variable.
4) Make sure your ~/.m2/ folder has a settings.xml in it, you can use the one from the maven download to start. (you may need to add the proxy details)
5) Test with
mvn -version
6) You can clear your local repository by deleting files in ~/.m2/repository if you've not done much and have a reasonable network just blow the whole thing away.

Can we use Ant jar for ANT_HOME on Windows?

JAVA_HOME environment variable works fine (java -version works).
ANT_HOME pointing to C:\antdir\ant-1.8.2.jar isn't working.
Path command clearly displays that both JAVA_HOME and ANT_HOME are added.
...;C:\javadir\jdk\bin;C:\antdir\ant-1.8.2.jar;
Is ant jar valid for ANT_HOME configuration?
In Windows, Ant is launched from ant.bat, which exists in the bin directory within the downloadable zip. Simply extract the zip to wherever you want ANT_HOME to be, and point your PATH environment variable to its bin subdirectory.
Theoretically, you could run Ant straight from the jar, but you'd first have to reproduce much of the complicated configuration that takes place within ant.bat. Just adding it to your path wouldn't be enough. Probably wouldn't be worth it unless you have a very specific reason to do so.

How to specify an alternate location for the .m2 folder or settings.xml permanently?

I am using Maven 3.0, and my .m2 folder location is C:\Users\me\.m2.
However, I do not have write access to that folder, but I want to change the repository location from the one defined in the settings.xml.
Due to restricted access, I am not able to edit the settings.xml to change the repository location.
How can I override the values of my settings.xml -or change the default location of the .m2 folder- without editing my C:\Users\me\.m2\conf\settings.xml file?
You need to add this line into your settings.xml (or uncomment if it's already there).
<localRepository>C:\Users\me\.m2\repo</localRepository>
Also it's possible to run your commands with mvn clean install -gs C:\Users\me\.m2\settings.xml - this parameter will force maven to use different settings.xml then the default one (which is in $HOME/.m2/settings.xml)
It's funny how other answers ignore the fact that you can't write to that file...
There are a few workarounds that come to my mind which could help use an arbitrary C:\redirected\settings.xml and use the mvn command as usual happily ever after.
mvn alias
In a Unix shell (or on Cygwin) you can create
alias mvn='mvn --global-settings "C:\redirected\settings.xml"'
so when you're calling mvn blah blah from anywhere the config is "automatically" picked up.
See How to create alias in cmd? if you want this, but don't have a Unix shell.
mvn wrapper
Configure your environment so that mvn is resolved to a wrapper script when typed in the command line:
Remove your MVN_HOME/bin or M2_HOME/bin from your PATH so mvn is not resolved any more.
Add a folder to PATH (or use an existing one)
In that folder create an mvn.bat file with contents:
call C:\your\path\to\maven\bin\mvn.bat --global-settings "C:\redirected\settings.xml" %*
Note: if you want some projects to behave differently you can just create mvn.bat in the same folder as pom.xml so when you run plain mvn it resolves to the local one.
Use where mvn at any time to check how it is resolved, the first one will be run when you type mvn.
mvn.bat hack
If you have write access to C:\your\path\to\maven\bin\mvn.bat, edit the file and add set MAVEN_CMD_LINE_ARG to the :runm2 part:
#REM Start MAVEN2
:runm2
set MAVEN_CMD_LINE_ARGS=--global-settings "C:\redirected\settings.xml" %MAVEN_CMD_LINE_ARGS%
set CLASSWORLDS_LAUNCHER=...
mvn.sh hack
For completeness, you can change the C:\your\path\to\maven\bin\mvn shell script too by changing the exec "$JAVACMD" command's
${CLASSWORLDS_LAUNCHER} "$#"
part to
${CLASSWORLDS_LAUNCHER} --global-settings "C:\redirected\settings.xml" "$#"
Suggestion/Rant
As a person in IT it's funny that you don't have access to your own home folder, for me this constitutes as incompetence from the company you're working for: this is equivalent of hiring someone to do software development, but not providing even the possibility to use anything other than notepad.exe or Microsoft Word to edit the source files. I'd suggest to contact your help desk or administrator and request write access at least to that particular file so that you can change the path of the local repository.
Disclaimer: None of these are tested for this particular use case, but I successfully used all of them previously for various other software.
Nobody suggested this, but you can use -Dmaven.repo.local command line argument to change where the repository is at. In addition, according to settings.xml documentation, you can set -Dmaven.home where it looks for the settings.xml file.
See: Settings.xml documentation
Below is the configuration in Maven software by default in MAVEN_HOME\conf\settings.xml.
<settings>
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
Add the below line under this configuration, will fulfill the requirement.
<localRepository>custom_path</localRepository>
Ex: <localRepository>D:/MYNAME/settings/.m2/repository</localRepository>
You can change the default location of .m2 directory in m2.conf file. It resides in your maven installation directory.
add modify this line in
m2.conf
set maven.home C:\Users\me\.m2
You can point to a different-settings.xml when you deploy your project. When deployed from the project folder you can have a relative path to get back to your home folder:
mvn clean deploy -s ../../.m2/different-settings.xml

Maven home directory not specified - Windows 7 & IntelliJ

Maven runs on Netbeans just fine. I'm trying to figure out why it doesn't like IntelliJ. I've done some Googling and can't figure out what's wrong. I've read things about specifying a path in my environmental variables, but I have no idea what my "maven install path" is.
I have followed these instructions which are listed on Maven's Download Page...
Windows 2000/XP
Unzip the distribution archive, i.e. apache-maven-3.0.4-bin.zip to the directory you wish to install Maven 3.0.4. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apache-maven-3.0.4 will be created from the archive.
Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.0.4. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven 2.0.9, also be sure that the M2_HOME doesn't have a '\' as last character.
In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in your Path environment variable.
Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.
Turns out I had downloaded the source, not the binary. Once I downloaded the binary it worked fine.

How to tell maven to get MAVEN_OPTS from specific file?

mvn command, among others, have following options:
-f,--file <arg> Force the use of an alternate POM file. (This is for pointing file instead of default pom.xml file.)
-gs,--global-settings <arg> Alternate path for the global settings file. (This one is for pointing the settings.xml file, which is by default in .m2 directory.)
Still there is yet one config file uncovered by these options - .mavenrc
So, my question is - Is there a way to tell maven from which file it should get MAVEN_OPTS?
MAVEN_OPTS is a environment variable from the OS. You can set it anyway you want before launching maven.
In bash (linux):
export MAVEN_OPTS=...
On windows:
set MAVEN_OPTS=...
I think you could even edit the 'mvn' of 'mvn.bat' shell script to get different variables.
Starting with Maven 3.3.1, you can put these settings into the .mvn/maven.config file in your project repository.
References:
JVM and Command Line Options
MNG-5767

Resources