maven打包含有多个main程序的jar包运行方式

时间:2021-12-15 12:57:23

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.4</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>

        <configuration>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
          <transformers>
            <transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>com.java.patchInstall.PatchInstall</mainClass>
            </transformer>
            <transformer
                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.handlers</resource>
            </transformer>
            <transformer
                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.schemas</resource>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>

如果一个jar中含有多个主程序,而你没有配置默认主程序,或者想要运行指定主程序,则可以通过如下命令执行:

java -cp example03-1.0-SNAPSHOT.jar com.java.common.HelloAgent

-cp <目录和 zip/jar文件的类搜索路径>

example03-1.0-SNAPSHOT.jar   jar

com.java.common.HelloAgent  指定的主程序类