简体   繁体   中英

How to inject Maven profile value into properties file

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 is building.

My profiles:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <projectVersion>DEV</projectVersion>
        </properties>
    </profile>

    <profile>
        <id>test</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <projectVersion>1.0.0-RC1</projectVersion>
        </properties>
    </profile>

    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <projectVersion>1.0.0-Final</projectVersion>
        </properties>
    </profile>
</profiles>

and filter.properties looks like this:

projectName = defaultName
versionName = defaultVersion

How to do that? Im building project by command:

mvn clean install -D profile_name

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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