Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject Maven profile value into properties file

Tags:

java

maven

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
like image 544
G.Spansky Avatar asked Dec 15 '15 12:12

G.Spansky


2 Answers

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
like image 53
Jorge Campos Avatar answered Oct 02 '22 13:10

Jorge Campos


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.

like image 34
malaguna Avatar answered Oct 02 '22 12:10

malaguna