Issue
Small question regarding the usage of maven assembly plugin, when the project have multiple main files please.
First, I had a project with only one main class. This main class uses a lot of dependencies. Therefore, I went to package a "uber jar" with the maven assemble plugin.
I understand it is not "common" but want to ask regarding the technicalities.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.my.only.one.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<outputDirectory>./build/</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Everything fine, I do get a single jar file, quite heavy, but doing the job, if I java -jar, it will run the only one Main fine, very happy.
Now, we have multiple Main files. With the exact same maven configuration, of course, even if the jar is compiled, when running the jar, only the main is run.
<manifest>
<mainClass>com.my.only.one.Main</mainClass>
</manifest>
Therefore, I am doing something which I believe is very dumb. When I want to run the second main, I change the maven file to
<manifest>
<mainClass>com.my.second.MainFile</mainClass>
</manifest>
Then, let it compile, build, copy the jar somewhere, changing its name to "secondMain" and then, changing the code back and forth between the first and second main file, creating the jar.
I think this is very dumb and was wondering what is the correct way to perform this, please?
Thank you
Solution
The solution is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>build-one</id>
<configuration>
<archive>
<manifest>
<mainClass>com.my.only.one.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-one</finalName>
<outputDirectory>./some-dir/</outputDirectory>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>build-two</id>
<configuration>
<archive>
<manifest>
<mainClass>com.my.second.MainFile</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-two</finalName>
<outputDirectory>./some-dir/</outputDirectory>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Answered By - PatPatPat