简体   繁体   中英

Maven profile properties are not “overriding”

I have Maven multi-module project with such structure:

parent-pom-project

-- module1

-- module2

At the parent-pom-project I have such pom.xml

    <modules>
        <module>module1</module>
    </modules>
...
    <profiles>
        <profile>
           <id>local</id>
           <properties>
               <prop>local_prop</prop>
           </properties>
        </profile>
        <profile>
           <id>test</id>
           <modules>
                <module>module2</module>
           </modules>
           <properties>
               <prop>test_prop</prop>
           </properties>
        </profile>
    </profiles>

At all pom.xml files I have such tag:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

At module1 and module2 in resource directory I have properties files with such text:

prop=${prop}

The problem is that after

mvn clean install

or

mvn clean install -Ptest

or even

mvn clean install -P test

I get

prop=local_prop

If I user test profile for build module2 is also builded, but properties are used from local profile. I use Maven 3.0.3. Anybody have any ideas?

You could try to use the mvn help:effective-pom -Ptest command to see the paramters used in your build.

See http://maven.apache.org/plugins/maven-help-plugin/plugin-info.html for more details.

Add a ${basedir} in front of your resource directories:

<directory>${basedir}/src/main/resources</directory>

This should fix your problem. My explanation would be that in a multi-module project it's not picking up the path correctly (for within the child module), if you're building from the top-level. Thus when trying to filter, it applies it to a different directory (the actual root-level aggregator), instead of the child.

I hope it helps.

I resolve problem uninstalling current maven plugin for eclipse and use another one. Now I use this ones: - Maven Integration: http://m2eclipse.sonatype.org/sites/m2e - Maven Integration for WTP: http://m2eclipse.sonatype.org/sites/m2e-extras/

Early I was using this one http://download.eclipse.org/technology/m2e/releases/ . I cannot explain such behavior but may be some configuration was changed by plugin.

Although old I had the same problem and didn't find the solution here. For me the problem was Eclipse which I use parallel to mvn on the command line. Eclipse instantly called process-resources after I did so on the command line.

Thus the solution was to select the profile in Eclipse (Project->Maven->Select Maven Profiles).

I can't figure out how maven can resolve your property if you do not specify any profile. So, to see what's really there, I tried myself, following exactly the schema you described and... I did not experience the problem you have. In your case, it really behaves like if the property was defined outside the profile -as bugske suggested. What happened if you comment temporarily both profiles ?

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