Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for life-cycle/phase and file existence in Maven and report error

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?

like image 939
gavenkoa Avatar asked Dec 20 '12 15:12

gavenkoa


People also ask

What are the 3 build lifecycle of Maven?

There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's web site.

What is the function of the verify phase in the Maven build life cycle?

validate - validate the project is correct and all necessary information is available. compile - compile the source code of the project. test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.

What is a Maven list its phases or life cycle command to run our project through the Maven?

Maven makes the day-to-day work of Java developers easier and helps with the building and running of any Java-based project. Maven Lifecycle: Below is a representation of the default Maven lifecycle and its 8 steps: Validate, Compile, Test, Package, Integration test, Verify, Install and Deploy.

Which phase in Maven life cycle processes the source code for example filter any value?

Post-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes. Generate any test source code to be included in compilation phase. Process the test source code, for example, filter any values.


1 Answers

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

like image 115
artbristol Avatar answered Sep 22 '22 13:09

artbristol