views:

1690

answers:

3

I have a Maven pom.xml with a plugin that I want to be able to control on the command line. Everything works otherwise fine, except even after searching the net a while I can't figure out how to set a default value for my control property:

<plugin>
    ...
    <configuration>
        <param>${myProperty}</param>
    </configuration>
    ...
</plugin>

So if I run Maven with

mvn -DmyProperty=something ...

everything's fine, but I'd like to have a specific value assigned to myProperty also without the -DmyProperty=... switch. How can this be done?

A: 

This might work for you:

<profiles>
  <profile>
    <id>default</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
     <plugin>
       <configuration>
        <param>Foo</param>
       </configuration>
     </plugin>
    </build>
    ...
  </profile>
  <profile>
    <id>nodefault</id>
    ...
     <build>
      <plugin>
        <configuration>
            <param>${myProperty}</param>
        </configuration>
     </plugin>
     </build>
    ...
  </profile>
</profiles>

That way,

mvn clean will use "foo" as your default param. In cases when you need to override, use mvn -P nodefault -DmyProperty=somthing

sal
+2  A: 

You could use something like below:

<profile>
    <id>default</id>
    <properties>
        <env>default</env>
        <myProperty>someValue</myProperty>            
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>
Taylor Leese
Right, that did it, thanks!
Eemeli Kantola
Great. No problem.
Taylor Leese
+3  A: 

Taylor L's approach works fine, but you don't need the extra profile. You can just declare property values in the POM file.

<project>
  ...
      <properties>
    <!-- Sets the location that Apache Cargo will use to install containers when they are downloaded. 
         Executions of the plug-in should append the container name and version to this path. 
         E.g. apache-tomcat-5.5.20 --> 
    <cargo.container.install.dir>${user.home}/.m2/cargo/containers</cargo.container.install.dir> 
  </properties> 
</project>

You can also set properties in your user settings.xml file in the event that you want each user to be able to set their own defaults. We use this approach to hide credentials that the CI server uses for some plug-ins from regular developers.

DavidValeri