Issue
I have written a console app (.java class with main function), and I'd like to call this main (or, for example some static method before execution of my big project) how to do this with Maven (script?)?
Solution
go and look at the exec maven plugin. you can use it to execute either a native executable, a script, or a java main() at any point in the build. like so:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
you cannot run just any java method you want, but writing a small class with a main() method that invokes whatever you want is pretty easy.
another gotcha is that the class you want to run has to be compiled before you run it. this means that either this execution has to happen after the compile phase or you need to split your project into 2 modules - one containing the main class and the other containing the rest of you code (that will depend on the 1st module to get proper build order)
if you still insist on arbitrary code, there's always the groovy maven plugin, which allows you to write groovy code inline to be executed during the build.
Answered By - radai
Answer Checked By - Marilyn (JavaFixing Volunteer)