0

After reading through a lot of SO questions as well as other sites I still haven't been able to exactly address this problem.

We have a long build cycle (10-20 mins) because there are a lot of dependencies. It sometimes happens that you start a build with everything up do date, but while it's being done, someone pushes new changes to the remote svn.

I would like Maven to check on the validate and verify phases if svn is still up to date basically, on all dependent projects.

I've tried using the Enforcer plugin, and the Build number plugin with no success yet. The enforcer seems like it could do the trick, but I haven't figured out which rules to set. The build number plugin on the other hand checks if there are no local modifications, but I don't think it checks the remote changes.

I don't think the POM is very relevant to the question, but if anyone needs it, or some parts please let me know and I'll update with it.

aurbano
  • 3,324
  • 1
  • 25
  • 39

2 Answers2

1

I would try a combination of the maven-scm-plugin's diff goal and Enforcer.

scm:diff may be configured to write output to a file. Run that when there are no changes and see how big the file is, or, if it generates the file at all if there are no changes. Then, use the Enforcer plugin's requireFilesDontExist and/or requireFileSize rules to make sure the scm:diff output file is the "no changes" size you determined. If it's larger than that, changes were committed during this build.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • Sounds like a really good idea! I'll try tomorrow and see how that goes. Do you think I could run `scm:diff` automatically from the same Maven lifecycle? Perhaps at the end? – aurbano Mar 13 '14 at 17:34
  • Absolutely. To run in both validate & verify phases, you add both the SCM and Enforcer plugins to your POM (in that order). You then add 2 executions for each plugin with the config you determined. First execution of each is bound to the validate phase, second to verify. Maven will run both goals at both the beginning & end of the build. – user944849 Mar 13 '14 at 18:22
  • It is almost working though, scm diff generates the file and then the enforcer checks its size. I have one problem with the scm plugin though, I can't get to it ignore files using the config. Any idea there? – aurbano Mar 14 '14 at 09:39
  • Another option I was thinking was generating the diff file at the beginning and at the end, and comparing both. Although I'm still looking for a way to compare both using enforcer. – aurbano Mar 14 '14 at 10:04
  • Found another solution, I will post it as another answer in case anyone needs it. – aurbano Mar 14 '14 at 11:17
1

After a lot of testing I found another solution. This solution is for people that work with SVN and only want to commit changes once the build succeeds, and need to use the latest revision for a build.

What this will do is retrieve the latest revision number from SVN and update the working copy. At the end of the build process it will check the revision number again, to ensure that no one has pushed any changes.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
<executions>
    <execution>
        <id>get-svn-local-revision-before</id>
        <phase>validate</phase>
        <goals>
            <goal>create</goal>
        </goals>
        <configuration>
            <doCheck>false</doCheck>
            <doUpdate>true</doUpdate>
            <buildNumberPropertyName>buildNumberLocal</buildNumberPropertyName>
            <useLastCommittedRevision>true</useLastCommittedRevision>
        </configuration>
    </execution>
    <execution>
        <id>get-svn-remote-revision-before</id>
        <phase>validate</phase>
        <goals>
            <goal>create</goal>
        </goals>
        <configuration>
            <doCheck>false</doCheck>
            <doUpdate>false</doUpdate>
            <buildNumberPropertyName>buildNumberRemote</buildNumberPropertyName>
            <useLastCommittedRevision>false</useLastCommittedRevision>
        </configuration>
    </execution>
    <!-- Repeat after everything is done -->
    <execution>
        <id>get-svn-remote-revision-after</id>
        <phase>verify</phase>
        <goals>
            <goal>create</goal>
        </goals>
        <configuration>
            <doCheck>false</doCheck>
            <doUpdate>false</doUpdate>
            <buildNumberPropertyName>buildNumberRemote</buildNumberPropertyName>
            <useLastCommittedRevision>false</useLastCommittedRevision>
        </configuration>
    </execution>
</executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.3.1</version>
    <executions>
    <execution>
        <id>check-svn-revisions-before</id>
        <phase>process-test-resources</phase>
        <goals>
            <goal>enforce</goal>
        </goals>
        <configuration>
            <rules>
                <evaluateBeanshell>
                     <condition>${buildNumberLocal} == ${buildNumberRemote}</condition>
                     <message>[ERROR] Local build (${buildNumberLocal}) doesn't match remote build (${buildNumberRemote})</message>
                </evaluateBeanshell>
             </rules>
             <fail>true</fail>
        </configuration>
    </execution>
    <!-- Repeat after everything is done -->
    <execution>
        <id>check-svn-revisions-after</id>
        <phase>verify</phase>
        <goals>
            <goal>enforce</goal>
        </goals>
        <configuration>
            <rules>
                <evaluateBeanshell>
                    <condition>${buildNumberLocal} == ${buildNumberRemote}</condition>
                    <message>[ERROR] Local build (${buildNumberLocal}) doesn't match remote build (${buildNumberRemote})</message>
                </evaluateBeanshell>
             </rules>
             <fail>true</fail>
        </configuration>
    </execution>
</executions>
</plugin>
aurbano
  • 3,324
  • 1
  • 25
  • 39