Vincent
Vincent

Reputation: 6188

Create project-specific Maven settings

I have a general settings.xml file for all Maven projects where a default profile and servers and mirrors are defined. How can I use project-specific settings to ignore the default?

Can I point to a local settings.xml file for one specific Maven project?

Passing a local settings file with --settings parameter also works but isn't there a way to do this permanently?

Upvotes: 56

Views: 35749

Answers (5)

bric3
bric3

Reputation: 42223

I think I have a solution to that problem, you need a recent maven version, I'm using maven 3.5.2 but the feature was introduced in maven 3.3.1 I believe, not sure.

The idea is to use the local .mvn folder (in the parent project's folder) where it is possible to configure several things like JVM options, maven options that are always used, etc.

For maven options just create a file maven.config inside the .mvn folder with the content

For Maven 3.9.2+ users you can use session.rootDirectory, meaning you can use the maven command anywhere in the project modules.

--settings 
${session.rootDirectory}/.mvn/local-settings.xml

(or the long form without space between the option and the value --settings=${session.rootDirectory}/.mvn/local-settings.xml)

For Maven 3.5.2 -> 3.9.1, session.rootDirectory is not available, and you need to execute the command in the project root directory.

--settings 
./.mvn/local-settings.xml

and that should be about it. Of course the local-settings.xml should be a valid maven settings file.

Note the content has to be multiline after Maven 3.9 (see MNG-7684, thanks to Matthias for pointing this out).

Here's the structure within the maven project root folder :

parent-mvn-project
   ├── .mvn
   │   ├── local-settings.xml
   │   └── maven.config
   ├── submodule-A (if any submodules)
   └── submodule-B (if any submodules)

Upvotes: 84

AkSh
AkSh

Reputation: 228

Just a sample settings.xml file in case anyone needs it.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.home}/personal-wks/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>

change your <localRepository> accordingly.

Refer to this answer to set up the project specific settings.xml.

detailed blog on baeldung

Upvotes: 1

James Z.M. Gao
James Z.M. Gao

Reputation: 646

After maven 3.3.1, use the project-settings-extension to load the project settings, and put project specific mirrors into ${basedir}/.mvn/settings.xml in each project. 

in ${basedir}/.mvn/extensions.xml

<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
  <extension>
    <groupId>com.github.gzm55.maven</groupId>
    <artifactId>project-settings-extension</artifactId>
    <version>0.3.5</version>
  </extension>
</extensions>

in ${basedir}/.mvn/settings.xml

  <settings>

    <mirrors>
      <mirror>
        <id>id</id>
        <url>https://url-for-this-project/</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    </mirrors>

    <profiles>
      <!-- profiles for this project, such as corp internal repositories -->
    </profiles>

  </settings>

Upvotes: 12

Jason
Jason

Reputation: 66

You may set repositories in the Maven project pom.xml file, and this is one option suggested by its official website https://maven.apache.org/guides/mini/guide-multiple-repositories.html.

Here is an example.

<project>
...
  <repositories>
    <repository>
      <id>my-repo1</id>
      <name>your custom repo</name>
      <url>http://jarsm2.dyndns.dk</url>
    </repository>
    <repository>
      <id>my-repo2</id>
      <name>your custom repo</name>
      <url>http://jarsm2.dyndns.dk</url>
    </repository>
  </repositories>
...
</project>

Upvotes: 1

p3quod
p3quod

Reputation: 1679

In IntelliJ you can set a different settings file for any project:

  1. Go to Settings -> Build, Execution, Deployment -> Build tools -> Maven
  2. Set the user settings file to local-settings.xml

Upvotes: 11

Related Questions