This is a new (to me) trick for my programming toolbox. I discovered (copied) it from the Apache CXF project. (I don’t know if they originated it or not.) Basically, you can use a Maven profile configuration in your pom.xml to easily run a test program in your project that has a
public static void main(String args) method. I discovered this technique reading the README.txt of the Apache CXF sample “
java_first_jaxws“. Here is what the Maven command looks like:
Here is what the profile configuration in the pom.xml looks like:
... <profiles> <profile> <id>server</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>demo.hw.server.Server</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> ... </profiles> ...
When the “
mvn -Pserver” command is executed, the
exec-maven-plugin will run the
demo.hw.server.Server class. The
id identifies the profile id used in the mvn command after the “
mainClass element defines the class with the main method to execute. The
exec-maven-plugin does all of the hard work. One of the benefits of this technique is the standard Maven classpath for the project defined in the pom to build and unit test your software is used to run the
mainClass. This comes in quite handy if your program uses many jar files. I used to use a Windows batch file (copied from the Tomcat startup script
catalina.bat) to run little test programs. This Maven profile technique is much easier to use for me, since I use Maven for most all of my Java development.