Dims
Dims

Reputation: 51259

Path to local maven repository programmatically?

I want to determine location of local maven repository inside Matlab and do this in the same way mvn command does. Is this possible?

Also, I don't want to resemble entire Maven logic explicitly.

Are there any jars I can import and know where Maven expects local repository located?

Upvotes: 1

Views: 1775

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23695

The location of the repository is defined by the localRepository entry of the configuration file called setting.xml, which is located in {M2_HOME}\conf\ and looks like this:

<settings>
    <!-- ... -->
    <localRepository>...</localRepository>
    <!-- ... -->
</settings>

If the configuration file doesn't exist or doesn't contain the aforementioned entry, the default location is used, which corresponds to:

  • ~/.m2/repository under *NIX / MaxOS
  • C:\Documents and Settings\{USERNAME}\.m2\repository under Windows

The only solution for you is to locate the setting.xml file and parse it in order to extract the location of the repository. If the latter is not defined, you have to defaultize it properly depending on the current underlying operating system.

This process could be tricky, especially if you have to implement cross-system compatibility in your Matlab code. I suggest you an alternative which involves the system function:

[status,cmdout] = system('mvn help:effective-settings');

If the command runs without issues, the cmdout will contain the whole content of the setting.xml file. Once you have it, you can parse the XML data and find the folder.

Upvotes: 1

Related Questions