Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

java - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...