8
votes

I want to check in Maven if authentication data from property file is provided by developer during application testing if call integration-test life-cycle.

As state common practice it is bad to commit authentication data to source tree. Standard maven approach described at settings such as username and password should not be distributed along with the pom.xml.

But I don't like this approach (I want per checkout settings, not per dev-host!!) and want to provide src/text/resources/auth.properties.example in VCS (SVN/GIT/HG) as example and want to make code that check in Maven for existence of src/text/resources/auth.properties which is own per developer (or ever per project checkout!!) but only if integration-test phase was called (or any other after integration-test phase). If executed any previous phases (like compile or test) - this checks must be disabled.

Maven validate phase designed to check build consistency (refer to introduction-to-the-lifecycle). But there are no any checks for phases!! So I use pre-integration-test phase.

I write working code:

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>my-app</name>

  <profiles>

    <profile>
      <id>existent.properties</id>
      <activation>
        <file>
          <missing>auth.properties</missing>
        </file>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
              <execution>
                <phase>pre-integration-test</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <target>
                    <echo>In order to run integration-text life-cycle:</echo>
                    <echo>  1) Rename 'auth.properties.example' into 'auth.properties'.</echo>
                    <echo>  2) Fill 'auth.properties' with your own authentification data.</echo>
                    <fail message="Can't find 'auth.properties'."/>
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

  </profiles>

  <build>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
        <executions>

          <execution>
            <id>test</id>
            <phase>test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <echo>JUnit tests!</echo>
              </target>
            </configuration>
          </execution>

          <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <echo>Selenium tests!</echo>
              </target>
            </configuration>
          </execution>

        </executions>
      </plugin>

    </plugins>

  </build>

</project>

But as GNU Make guru I dislike above code. Am I right? Is it wrong use of Maven?

1

1 Answers

4
votes

Your approach is fine (if a little over-engineered IMHO - I'd just put that kind of thing in a README / project wiki; the build ought to fail without the file when an attempt is made to read it)

You might also want to use the Enforcer goal instead of antrun: http://maven.apache.org/enforcer/enforcer-rules/requireFilesExist.html