How to run Maven plugin execution only if final output is not presented yet

I inherited a POM that tries to avoid repeating build steps by using a profile that only activates when the step's output doesn't exist:

<profile>
 <id>run-once</id>
  <activation>
    <file>
     <missing>target/some-output</missing>
    </file>
  </activation>
  <build>
   <plugins>
    <plugin>
     ...
     <executions>
      <execution>
       ... slow process to produce target/some-output ...
      </execution>
     </executions>
    </plugin>
   </plugins>
  </build>
 </activation>

      

However, as the maven experts no doubt realized right away that this would not work if the developer spoke mvn clean install

. Maven calculates active profiles once, before starting clean

, and if target/some-output

present, the profile run-once

is inactive. The result target/some-output

is removed by the clean phase, but not recreated in the phase install

, and the subsequent WAR does not work because it is some-output

missing.

Is there a standard solution to this problem (besides the exception mvn clean install

)? I'm going to make the plugin unconditional to prevent silent creation of a broken WAR.

More generally, is there a standard technique to prevent mvn from recreating type artifacts some-output

that are relevant? Or the idea that if make-style dependency management is important, gradle or rake should be used instead of maven?

+3


source to share


1 answer


I don't think there is a standard solution to this problem. There are various options I can think of (there are likely others):



  • you could explicitly activate the profile manually:, mvn clean install -Prun-once

    but then you have to remember that every time, of course,
  • configure the maven-enforcer-plugin along with requireFilesExist to ensure the files exist and the assembly fails if they don't. (at least then you don't get the silent creation of a broken war).
  • change the profile so that it creates files in a folder in your folder src

    (i.e. src/main/gen

    ) that is excluded from checking into the source repository (if you are using it) and then configure the maven-resources-plugin and its copy-resources purpose to copy those resources to the correct location in your build directory. Thus, cleaning will not remove them.
0


source







All Articles