How to inject Maven profile value into properties file

后端 未结 2 553
感动是毒
感动是毒 2020-12-11 18:26

Little stuck here. I have a pom with 3 profiles. Theese profiles have different version name. I want to inject that version name into properties file when a specific profile

相关标签:
2条回答
  • 2020-12-11 18:54

    What you need to do is to add a new section to your <build> section of your POM file.

    Like this:

    <build>
        <resources>
            <resource>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    

    This will look inside the specified folder (src/main/resources) on the specified files **/*.properties and change the files when it encounters defined variables.

    So in order to this work your propertie file must be this way:

    projectName = ${defaultName}
    versionName = ${defaultVersion}
    

    Be aware with these variables name. Maven will replace it with the defined names by you or the names of the Maven structure like ${projectVersion} will be replaced by the <version>1.0</version> tag of your pom file.

    So instead of using:

    <properties>
         <projectVersion>1.0.0-Final</projectVersion>
    </properties>
    

    Change the name (and the version) of this variable to something else like:

    <properties>
         <defaultVersion>1.0.0-Final</defaultVersion>
         <defaultName>someName</defaultName>
    </properties>
    

    On all your profiles.

    And just run your maven command as:

    mvn install -Pprofilename
    
    0 讨论(0)
  • 2020-12-11 19:05

    Be careful with the profiles you shown. All of them are active by default and this is a problem because they all define the same maven property. Instead, you should mark only one as active by default.

    You also don't show <resources> filtering to process filter.properties, so this can be a mistake, as well.

    And a final though, you are controlling artifact version on maven profiles. I don't think it is a good idea. Please read about maven-release-plugin.

    0 讨论(0)
提交回复
热议问题