You cannot do that, and that's the intended behaviour.
In the settings file, the <profile>
element is not allowed to have all of the elements you could find in a project's POM. From the Settings Reference:
The profile
element in the settings.xml
is a truncated version of the pom.xml
profile
element. It consists of the activation
, repositories
, pluginRepositories
and properties
elements. The profile
elements only include these four elements because they concerns themselves with the build system as a whole (which is the role of the settings.xml
file), not about individual project object model settings.
This explain your error: the tag <build>
is illegal in the settings. Also, since the default value for the build directory is ${project.basedir}/target
, you would need to change the project base directory (i.e. the location of the POM) to override that from the command line with a system property.
All of this makes sense and it is perfectly normal not to be able to do that. A project build directory is specific to each project: it contains the temporary files that are created during the build of that specific project. If you could share this folder with multiple projects, you would soon have problems about files overwriting each others in a multi-module Maven project for example. Take two modules module-a
and module-b
: if they share the same build directory, it also means they will share the same classes directory (my-new-build-directory/classes
) and you will have a folder that contains classes of the two modules; impossible now to create the correct JAR of module-a
with only its classes, how would they be differentiated? It is also possible that one resource from a module would then overwrite one resource from another module, since they would both end in the same temporary folder.
The idea is that since the build directory is inhently tied to the project being built, it needs to be located in a specific directory for each project. By default, Maven places it inside a folder target
for each project, at the location of the POM. And if you want to override that (and maybe shoot yourself in the foot by sharing it with other projects), you can, but you'll need to explicitly modify the POM of the project.