Check existence of life cycle / phase and file in Maven and report an error

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

As a public practice, it is bad to pass authentication data to the source tree. The standard maven method described in settings such as username and password should not be distributed with pom.xml .

But I do not like this approach (I want every validation parameter, not dev-host !!) and would like to provide src / text / resources / auth.properties.example in VCS (SVN / GIT / HG), and you want to make a code that Maven checks for the existence of src / text / resources / auth.properties , which is owned by each developer (or ever for a project check !!), but only if the integration-test phase has been called (or any another after the integration-test phase). If any of the previous steps are performed (for example, compile or test ), these checks should be disabled.

Maven check the phase for checking assembly consistency (see introduction-to-the-lifecycle ). But there are no phase checks! Therefore, I use the pre-integration-test phase.

I am writing 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 a GNU Make guru, I don't like the code above. I'm right? Is using Maven wrong?

+7
source share
1 answer

Your approach is fine (if a little redesigned IMHO - I would just put this on the README / project wiki, the assembly should fail without a file when an attempt is made to read it)

You can also use the Enforcer target instead of antrun: http://maven.apache.org/enforcer/enforcer-rules/requireFilesExist.html

+2
source

All Articles