【问题标题】:Can Maven have local, project properties?Maven 可以有本地的项目属性吗?
【发布时间】:2023-03-24 00:40:01
【问题描述】:

我们有一个 Maven 项目,每个开发人员都需要使用他们的本地项目设置。由于它们不应存储在 Git 存储库中,因此我们无法在 pom.xml 文件中指定它们。

我们曾考虑使用 ~/.m2/settings.xml 文件,但由于此文件存储属性用户范围而不是每个项目,它们会干扰每个其他。例如:

<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>

如果我尝试使用这个文件,当我在 project1 上工作时,我也会从 project2 获取本地属性。它们必须具有相同的名称,因为很多都依赖于它们。

有什么方法可以在 Maven 中为每个项目设置本地属性?

【问题讨论】:

  • 拥有 2 个 poms 怎么样?适合 git 的那个和自定义的只有本地的?
  • @AnthonyAccioly 这看起来很有希望。会试试看。
  • 为什么在 Maven 项目中需要 sftp 访问权限?

标签: maven properties-file


【解决方案1】:
  1. 环境变量
    您可以为每个环境使用环境脚本,该脚本定义了sftp-endpoint和sftp-password等环境变量。然后将这些文件添加到.gitignore 并手动分发给开发团队。然后你可以在你的 pom 中使用这些属性,比如${env.VARIABLE_NAME} 或${env}.VARIABLE_NAME

  2. 密码加密
    使用Maven内置password encryption

【讨论】:

    【解决方案2】:

    @AnthonyAccioly 评论提供了重要线索。

    我想发布我的解决方案(与提出的重复问题不同),因为它可能对其他人有用。我最终写了一个小的 Mojo 插件来处理这个问题。

    在 pom.xml(存储在 Git 中)中包含了常用属性。它看起来像:

    <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>
    

    dev.properties 文件(未存储在 Git 中)如下所示:

    local.prop3=value3
    local.prop4=value4
    

    这样,我的插件从存储在 Git 中的 pom.xml 中检索公共属性,而本地属性(每个开发人员不同)存储在文件 dev.properties 中。一个简单的 Java 方法使用 java.util.Properties 类读取属性文件。很简单。

    【讨论】:

      猜你喜欢
      • 2010-12-10
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 2019-08-06
      • 2011-01-22
      • 2010-11-16
      • 2021-01-10
      相关资源
      最近更新 更多