简体   繁体   English

在pom.xml文件中包括每个用户的配置

[英]Including per-user configuration in a pom.xml file

I'm working with a Java/Maven project. 我正在使用Java / Maven项目。 My version of Maven is "Apache Maven 2.2.1 (rdebian-10)". 我的Maven版本是“ Apache Maven 2.2.1(rdebian-10)”。

I need to define some custom configuration when building the project. 构建项目时,我需要定义一些自定义配置。 I'm used to C projects when you configure the project with various configuration flags (configure --this --that) and then the builds proceeds accordingly to the parameters defined/found by the configure script. 当您使用各种配置标志(configure --this --that)配置项目时,我习惯于C项目,然后构建会根据配置脚本定义/找到的参数进行。

I need to emulate this feature with a Java/Maven project, a configuration system would be OK but also isolating the user specific parameters in a given file should be fine as well. 我需要用Java / Maven项目模拟此功能,配置系统可以,但也可以在给定文件中隔离用户特定的参数。 For example I would like to be able to define per-user specific configurations in a file included by pom.xml (for example custom.pom.xml). 例如,我希望能够在pom.xml包含的文件(例如custom.pom.xml)中定义每个用户的特定配置。 For example in my project I need to specify the path where the program will write temporary files and such. 例如,在我的项目中,我需要指定程序将在其中写入临时文件等的路径。

My question is, is it possible to isolate the per-user specific configuration in a file included by pom.xml, and/or what is the custom way to deal with this problem with Maven projects? 我的问题是,是否有可能将每个用户的特定配置隔离在pom.xml包含的文件中,和/或用Maven项目处理该问题的自定义方式是什么?

Right now I'm committing a pom.xml.template file, which needs to be renamed to pom.xml and edited with the user-specific parameters, but this is awkward because when the Maven requirement changes (eg if a new dependency is added) the developer must update his pom.xml file. 现在,我要提交一个pom.xml.template文件,该文件需要重命名为pom.xml并使用用户特定的参数进行编辑,但这很尴尬,因为当Maven需求发生变化时(例如,如果添加了新的依赖项) ),开发人员必须更新其pom.xml文件。 This is the relevant snippet from my pom.xml.template file: 这是我pom.xml.template文件中的相关片段:

<profiles>
    <profile>
        <id>production</id>
        <properties>
            <database.url>jdbc:postgresql://localhost:5432/myProject</database.url>
            <database.user>myProject</database.user>
            <database.password>myProjectPassword</database.password>
            <repository_path>my/project/repo/path</repository_path>
            <external_tool_bin_path>/absolute/path/to/tool/</external_tool_bin_path>
            <external_scripts_path>/path/to/scripts/</external_scripts_path>
            <tmpdir>tmp</tmpdir>
        </properties>
        <build>
            <finalName>myProject</finalName>
        </build>
    </profile>

[...] [...]

External tool and scripts are located in per-user specific paths (depending on system/installation). 外部工具和脚本位于每个用户特定的路径中(取决于系统/安装)。

Another way would be to use distinct profiles for each developer, which is awkward in a different way (since I don't want to hardcode user profiles in the committed pom.xml file). 另一种方法是为每个开发人员使用不同的配置文件,这以不同的方式尴尬(因为我不想在已提交的pom.xml文件中对用户配置文件进行硬编码)。

Alternatively I could write a configure script but this looks overkill, and not very portable, unless I want to require the user to rely on MinGW or similar. 另外,我可以编写一个配置脚本,但是这看起来有点过分,而且不是很容易移植,除非我想要求用户依赖MinGW或类似的东西。

Thanks in advance. 提前致谢。

I suggest you use maven profiles , and the per user configuration, keep as environment variables which are referenced from the pom's profiles as ${env.PARAMETER} . 我建议您使用maven 配置文件 ,并将每个用户的配置保留为环境变量,这些环境变量从pom的配置文件中引用为${env.PARAMETER}
You will have to instruct the users to set their environment as needed. 您将必须指示用户根据需要设置他们的环境。

This way, you can enjoy both features and not have user specific values in source control. 这样,您可以同时使用这两个功能,并且在源代码管理中没有用户特定的值。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM