What is the order of precedence when a property is set using convention mapping?


(Matt Khan) #1

For example, ‘JavaBasePlugin’ has the following code

project.getTasks().withType(AbstractCompile.class, new Action<AbstractCompile>() {
            public void execute(final AbstractCompile compile) {
                ConventionMapping conventionMapping = compile.getConventionMapping();
                conventionMapping.map("sourceCompatibility", new Callable<Object>() {
                    public Object call() throws Exception {
                        return javaConvention.getSourceCompatibility().toString();
                    }
                });
                conventionMapping.map("targetCompatibility", new Callable<Object>() {
                    public Object call() throws Exception {
                        return javaConvention.getTargetCompatibility().toString();
                    }
                });
            }
        });

Which value wins if a build explicitly sets the value? e.g. if one were to do the following.

project.tasks.withType(AbstractCompile, { it.sourceCompatibility = someValue } )

Use case is that I have someone who wants to compile 1 sourceset with java7 and another with java6.


(Peter Niederwieser) #2

The purpose of convention mapping is to provide (lazy) defaults. As soon as a property is set explicitly, convention mapping will no longer kick in.


(Matt Khan) #3

can conventions be overridden? i.e. if I set another convention mapping does the last one set win?

I also notice ‘MappedProperty’ has a cached method which causes the value to not be re evaluated. How should one use this? After declaration of the mapping?


(Peter Niederwieser) #4

Yes, they can be overridden, though it wouldn’t typically be necessary. You probably shouldn’t use the ‘cached’ method. I didn’t even know it exists.


(Matt Khan) #5

OK so if multiple things want to set a default on something then there is no way to manage the conflict or be aware of the conflict apart from looking through the src code, is that correct?


(Peter Niederwieser) #6

That’s correct. Setting default values (esp. via convention mapping) is the responsibility of the plugin that adds the domain object to the domain model.