1

Is there a way to validate (even using another plugin) that the mainClass specified for the maven-jar-plugin is a valid class, and even better that it has a public static void main(String[] argv) method?

It is common that after refactoring, this is the one thing that gets missed and I would like to avoid this and check at the verify lifecycle stage.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
flungo
  • 1,050
  • 1
  • 7
  • 21

1 Answers1

1

You could use the exec-maven-plugin and its exec goal to execute the following command:

java -jar target\project.jar

Which would indeed execute it according to the manifest file (hence executing the configured main method) or fail.

You could thus add to the verify phase the following (sample) configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.sample.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.4.0</version>
            <executions>
                <execution>
                    <id>check-main</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>-jar</argument>
                            <argument>${project.build.directory}/${project.build.finalName}.jar</argument>
                            <argument>sanityCheck</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

In the snippet above we are:

  • Configuring the maven-jar-plugin to configure the main class in its manifest (a sample com.sample.Main in this case)
  • Configure the exec-maven-plugin to execute the java command above on the final artifact (note the usage of maven properties to avoid hard-coded artifact names and path, recommended)
  • As a suggestion, also pass to this execution a further argument which your main method should foresee as sanity check, that is, execute it for a dry-run like (just print an information to the console or simply return immediately)
  • The execution will run successfully if the information of the manifest are correct, fail otherwise (and as such your build).

Simply renaming the main method to main2 would break the build with the following message:

[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ simple ---   
[INFO] Building jar: C:\Development\workspace-mars\simple\target\simple-0.0.1-SNAPSHOT.jar   
[INFO]   
[INFO] --- exec-maven-plugin:1.4.0:exec (check-main) @ simple ---   
Error: Main method not found in class com.sample.Main, please define the main method as:   
   public static void main(String[] args)   
[INFO] ------------------------------------------------------------------------  
[INFO] BUILD FAILURE   
[INFO] ------------------------------------------------------------------------   

Note the message above is pretty much what you were looking for:

Error: Main method not found in class com.sample.Main, please define the main method as:
public static void main(String[] args)

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128