tags:

views:

753

answers:

4

I'm setting up a maven build, and the destination server needs to be specified on the command line as a property (which is then used to select the appropriate profile), eg

mvn -Denv=test

I'd like the build to fail if the property's not set - is that possible?

Yes, I'm a Maven newbie.

EDIT: I've seen this link, which seems to imply it's not possible, but am not sure how up to date it is.

+1  A: 

My first inclination would be to make a Profile that's active whenever the env property is unset, and have it fail somehow. Perhaps you could write a Maven Plugin that tests for that property and fails if it's not present?

Alternately, you could probably test for it with a very small ant-build script.

Ed Brannin
A: 

To elaborate on edbrannin's alternate solution:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.yourcompany</groupId>
  <artifactId>yourproject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>checkParam</id>
            <phase>initialize</phase>
            <goals><goal>run</goal></goals>
            <configuration>
              <tasks>
                <fail message="'env' property must be set" unless="env"/>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

will give you the following output:

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: 'env' property must be set

IMHO the most straightforward way to do it (the one I'd go for personally).

You could even control a set of allowed values using a nested <condition> containing <or> and <equals> tags (see ANT's manual: http://ant.apache.org/manual/CoreTasks/conditions.html)

Olivier
This can be done more directly with the enforcer plugin.
Brian Fox
+1  A: 

Maybe you could use such a workaround: in Maven you can activate a profile if some property is not set:

<project>
...
    <profiles>
        <profile>
            <id>failure_profile</id>
            <activation>
                <property>
                    <name>!env</name>
                </property>
            </activation>
        </profile>
    </profiles>
</project>

Then you shall force this profile always fail, e.g. using maven-enforcer-plugin:

<profile>
    <id>failure_profile</id>
    ...
    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <executions>
              <execution>
                <id>enforce</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <AlwaysFail/>
                  </rules>
                  <fail>true</fail>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
</profile>

If you don't provide -Denv build will then fail:

[INFO] [enforcer:enforce {execution: enforce}]
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.AlwaysFail failed with message:
Always fails!
[INFO] ---------------------------------------------------------
[ERROR] BUILD ERROR

Well, it's much more verbose then Ant, but pure Maven :)

Giter
Excellent; thanks very much. As you say, it's a lot more verbose, so I'll stick with Olivier's answer as my accepted answer. That depsite me saying I'd like a pure Maven answer - apologies. I wish I could accept two answers.
Hobo
+5  A: 

Everyone is close, but there's a rule in the enforcer to specifically check for a property, no ant or funky profiles required: http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html

The rule can also check the value of the property.

Brian Fox
Much neater - just what I was looking for. Thanks!
Hobo