Joe DiNottra
Joe DiNottra

Reputation: 1008

Can Maven have local, project properties?

We have a Maven project where each developer needs to use their local project settings. Since they should not be stored in the Git repository, we can't speficy them in the pom.xml file.

We have considered to use the ~/.m2/settings.xml file but since this file store properties user-wide and not per project, they interfere with each other. For example:

<profiles>

  <profile>
    <id>project1</id>
    <properties>
      <sftp-endpoint>10.201.50.14</sftp-endpoint>
      <sftp-password>secretpass</sftp-password>
    </properties>
  </profile>

  <profile>
    <id>project2</id>
    <properties>
      <sftp-endpoint>12.34.56.78</sftp-endpoint>
      <sftp-password>pencil</sftp-password>
    </properties>
  </profile>

</profiles>

<activeProfiles>
  <activeProfile>project1</activeProfile>
  <activeProfile>project2</activeProfile>
</activeProfiles>

If I try to use this file, when I work on project1 I also get local properties from project2. They must have the same names since a lot depend on them.

Is there any way I can have local properties per project in Maven?

Upvotes: 1

Views: 904

Answers (2)

Joe DiNottra
Joe DiNottra

Reputation: 1008

@AnthonyAccioly comment gave the big clue.

I wanted to post my solution (that differs from the proposed duplicate question) since it may be of use to someone else. I ended up writing a small Mojo plugin to deal with this.

In the pom.xml (stored in Git) includes the common properties. It looks like:

<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.mycompany.mytool</groupId>
      <artifactId>a1-maven-plugin</artifactId>
      <version>1.0.0</version>

      <configuration>
        <localproperties>dev.properties</localproperties>
        <common.prop1>value1</common.prop1>
        <common.prop2>value2</common.prop2>
      </configuration>
      ...
    <plugin>
  <plugins>
<build>

The dev.properties file (not stored in Git) looks like:

local.prop3=value3
local.prop4=value4

This way, my plugin retrieves the common properties from the pom.xml that is stored in Git, while the local properties (that differ per developer) are stored in the file dev.properties. A simple Java method reads the properties file using the java.util.Properties class. Easy.

Upvotes: 2

diginoise
diginoise

Reputation: 7620

  1. Environment variables
    You can use environment script per environment which defines environment variables like sftp-endpoint and sftp-password. These files are then added to .gitignore and distributed manually to the dev team. Then you can use these properties in your pom like ${env.VARIABLE_NAME} or ${env}.VARIABLE_NAME

  2. Password Encryption
    Use Maven built-in password encryption

Upvotes: 0

Related Questions