Jonathan
Jonathan

Reputation: 417

Maven: Check Argument Before Running Test

I would like maven to run test phase only if a parameter is defined. For example:

mvn test -Dserver=hosname

If server was not specified (or empty) - I don't want the test to run (not in mvn test, nor in mvn install).

How do I do that? How do I get the value of the parameter (e.g. "server") inside my code?

Upvotes: 0

Views: 230

Answers (3)

Girish
Girish

Reputation: 1717

Profile can do that in maven what u want

for ex:

    <profiles>
        <profile>
          <id>server</id>
          <activation>
            <property>
              <name>server</name>
              <value>${hosname}</value>
            </property>
          </activation>
          <build>
            <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                  <skipTests>true</skipTests>
                </configuration>
              </plugin>
            </plugins>
          </build>
        </profile>
</profiles>

Upvotes: 1

khmarbaise
khmarbaise

Reputation: 97507

The profile can be used like this:

<profiles>
  <profile>
    <activation>
      <property>
        <name>server</name>
      </property>
    </activation>
    <!-- do whatever you need to do -->
  </profile>
</profiles>

Upvotes: 0

user180100
user180100

Reputation:

Assuming you are using junit:

  • If one test: use Assume to check the property (see @BeforeClass below) and skip the test if the property is null
  • If many, create a parent class and make your tests extend it:

.

public class RunIfServerProperty {
    @BeforeClass
    public static void oneTimeSetup() {
        Assume.assumeNotNull(System.getProperty("server"));
    }
}

see also Assume javadoc

Upvotes: 2

Related Questions