Search code examples
springspring-bootmavenspring-boot-maven-plugin

Access Maven settings.xml in pom.xml for Spring Boot Maven Plugin


I would like to access the user-specific settings (passwords, usernames etc. for a shared private repository) that are contained within the settings.xml in my pom.

I need these settings for the Spring Boot Maven Plugin, because I want to use the publish feature there (pushing a created docker image to our private docker repository).

Is there a way to achieve this?

Of course I don't want to save any user-specific passwords inside the pom.xml.

The maven documentation states that it is possible to access the settings.xml (e.g. here: https://maven.apache.org/guides/getting-started/index.html#how-do-i-filter-resource-files), but it does not explain how to to that.

I expecting something like this in my pom:

<someTag>${userSettings.some.property}</someTag>

Solution

  • Suppose we have following configuration for spring-boot-maven-plugin

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <image>
                <name>docker.example.com/library/${project.artifactId}</name>
                <publish>true</publish>
            </image>
            <docker>
                <publishRegistry>
                    <username>user</username>
                    <password>secret</password>
                    <url>https://docker.example.com/v1/</url>
                    <email>[email protected]</email>
                </publishRegistry>
            </docker>
        </configuration>
    </plugin>
    

    and do not want to store our credentials in pom.xml since it is available to everyone who has access to version control system. Maven way is to define profile in ~/.m2/setting.xml with our credentials and and replace credentials in pom.xml with corresponding placeholders, like:

    ~/.m2/settings.xml:

    ...
    <profiles>
    ...    
        <profile>
            <id>registry-example.com</id>
            <properties>
                <registry.username>user</registry.username>
                <registry.password>secret</registry.password>
                <registry.url>https://docker.example.com/v1/</registry.url>
                <registry.email>[email protected]</registry.email>
            </properties>
        </profile>
    ...    
    </profiles>
    ...
    

    pom.xml:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <image>
                <name>docker.example.com/library/${project.artifactId}</name>
                <publish>true</publish>
            </image>
            <docker>
                <publishRegistry>
                    <username>${registry.username}</username>
                    <password>${registry.password}</password>
                    <url>${registry.url}</url>
                    <email>${registry.email}</email>
                </publishRegistry>
            </docker>
        </configuration>
    </plugin>
    

    Now, in order to tell maven to take into account the profile we have created we need to run maven with -P flag specifying id of our profile:

    mvn spring-boot:build-image -Pregistry-example.com