jpganz18
jpganz18

Reputation: 5858

how to specify the main class when packaging with maven?

I am wondering if there is any way to specify which main class I want my maven to use as reference, something like

mvn package -DmainClass=com.myMainClass

Dont want to modify the pom or the plugin itself, just send it as parameter, any idea?

Upvotes: 0

Views: 1762

Answers (1)

davidxxx
davidxxx

Reputation: 131376

The maven-jar-plugin jar goal defines a archive parameter with the org.apache.maven.archiver.MavenArchiveConfiguration type that defines the mainClass parameter.
But the archive parameter doesn't have a User property associated to that allows to provide the value as argument in the command line execution.
Note that the maven-assembly-plugin that allows also to package the artifact as a JAR will not be more helpful : the archive parameter is not defined as a User property either.

So you don't have the choice (at least with the maven-jar-plugin): you have to use the mainClass property in the pom.xml.

Now if it makes sense, you can make this value dynamic : value mainClass in the pom.xml with a custom user property that you pass from the command line.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
           <archive>
              <manifest>                
                <mainClass>${my.mainClass}</mainClass>
            </manifest>
           </archive>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

And execute :

mvn package -Dmy.mainClass=com.myMainClass

Upvotes: 5

Related Questions