情景
我有一个工程,由于一些原因,里面有两个入口(两个类都有main()String[] args)。而且我需要将这两个打包为两个独立的jar,并且每个jar中的配置文件只打包自己想要的。
比如下面的工程,划红线的两个类有各自的main函数,执行各自的逻辑:
附加需求:
1. 每个jar不需要bin和xmls的所有文件
2. logback文件各自使用各自的,也是就不要包含对方的logback.xml
实现
使用 maven-jar-plugin 插件实现。
<build>
<finalName>myApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>command-jar</id><!-- 包名,打出的jar包会被 finalName+id的方式命名-->
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<excludes><!-- 排除文件在这里配置 -->
<exclude>/logback-recall.xml</exclude>
<exclude>/*bin/**</exclude><!--/*bin无效,还是会保留bin以及下面的所有;/*bin/**会把bin以及下面的所有都排除;/bin/*只会排除bin下面的-->
<exclude>/*xmls/**</exclude>
<exclude>/assembly.xml</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.jfqqqq.command.CommandApp</mainClass>
</manifest>
</archive>
<classifier>command-jar</classifier>
</configuration>
</execution>
<execution>
<id>recaller-jar</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<excludes>
<exclude>/logback-command.xml</exclude>
<exclude>/*bin/**</exclude><!--/*bin无效,还是会保留bin以及下面的所有;/*bin/**会把bin以及下面的所有都排除;/bin/*只会排除bin下面的-->
<exclude>/*xmls/**</exclude>
<exclude>/assembly.xml</exclude>
</excludes>
<classifier>recaller-jar</classifier>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.jfqqqq.recall.RecallerApp</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
效果
解压后(以myApp-command-jar.jar为例):
完成
预告
接着本文,后边会在写一个将两个jar打包到一个公共的zip包下的demo。