Monday, April 24, 2017

Maven: the most weird error causing failure to add JAR to local repository


To add a 3rd party JAR into Maven local repository on your local machine, you can run:

$ mvn install:install-file -Dfile=yourJarFile.jar -DgroupId=yourGroupID -DartifactId=yourArtifactId -Dversion=1.0 -Dpackaging=jar

The template of this command is available all over the internet. It is convenient to copy and paste the template and make your own changes to add the JARs you want. However, if you run the command and see this error:

[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (......). Please verify you invoked Maven from the correct directory. -> [Help 1]

you don't need to scratch your head for the whole afternoon just like me.

Googling on the internet tells different reasons that may cause this problem. Some are weird, such as that in Powershell you have to add quotes for the parameters, i.e.

mvn install:install-file "-Dfile=yourJarFile.jar" "-DgroupId=yourGroupID" "-DartifactId=yourArtifactId" "-Dversion=1.0" "-Dpackaging=jar"

The reason causing my failure was even more weird. After a long time of research and experiment, I finally found out that the dash in my "-Dfile" was not an ASCII dash, but a UTF-8 en dash (0xE2 0x80 0x93). As my terminal was using UTF-8 charset, nobody could tell the difference -- but Maven.

So the lesson is, first, I should always set the terminal to use the ASCII charset. Secondly, try not to blindly copy and paste from the internet, where articles can be pasted in UTF-8.

And when Maven complains "The goal you specified requires a project to execute but there is no POM in this directory", we must understand it does not know what it is talking about. The reason could be that the input parameters are invalid. Don't hurry to add a pom.xml, just verify that your parameters are all good, char by char, and make sure they are all just simple ASCII.

No comments:

 
Get This <