Maven生成可运行jar

本文介绍了使用Maven进行项目的三种不同打包方式:采用maven-assembly-plugin、maven-jar-plugin+maven-dependency-plugin和maven-shade-plugin插件。详细解析了每种方式的特点及配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 使用maven-assembly-plugin插件打包

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>xxx.xxx.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

  打包方式:mvn package assembly:single
  打包后会在target目录下生成一个xxx-jar-with-dependencies.jar文件,这个文件不但包含了自己项目中的代码和资源,还包含了所有依赖包的内容。所以可以直接通过java -jar来运行。
  由于加了配置<phase>package</phase><goal>single</goal>,可以直接通过mvn package来打包,无需assembly:single。这个配置表示在执行package打包时,执行assembly:single,所以可以直接使用mvn package打包。
  这种方式会把第三方jar的代码解压后打包到xxx-jar-with-dependencies.jar中。

  
2. 使用maven-jar-plugin和maven-dependency-plugin插件打包

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>xxx.xxx.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
    <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
        </configuration>
    </execution>
</executions>
</plugin>

  maven-jar-plugin用于生成META-INF/MANIFEST.MF文件的部分内容,<mainClass>指定MANIFEST.MF中的Main-Class<addClasspath>会在MANIFEST.MF加上Class-Path项并配置依赖包,<classpathPrefix>指定依赖包所在目录。
  maven-dependency-plugin插件用于将依赖包拷贝到指定的位置,即lib目录下。
配置完成后,通过mvn package指令打包,会在target目录下生成jar包,并将依赖包拷贝到target/lib目录下。
  这种方式生成jar包只包含项目本身的代码、资源,所有的依赖包在lib目录中,运行时jar包必须和lib在同一个目录中。

  
3. 使用maven-shade-plugin插件打包

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <configuration>
        <shadedArtifactAttached>true</shadedArtifactAttached>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>xxx.xxx.Main</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

  执行mvn package后会在target目录下会生成两个jar包,xxx.jar和xxx-shaded.jar。xxx- shaded.jar和maven-assembly-plugin一样,包含了所有依赖,所以可以直接运行。

shade插件还有其它功能配置:
将部分jar包添加或排除

       <executions>
          <execution>
            <configuration>
              <artifactSet>
                <excludes>
                  <exclude>jmock:*</exclude>
                  <exclude>*:xml-apis</exclude>
                  <exclude>org.apache.maven:lib:tests</exclude>
                  <exclude>log4j:log4j:jar:</exclude>
                </excludes>
                <includes>
                    <include>junit:junit</include>
                </includes>
              </artifactSet>
            </configuration>
          </execution>
        </executions>

将依赖jar包内部资源添加或排除

       <executions>
          <execution>
            <configuration>
              <filters>
                <filter>
                  <artifact>junit:junit</artifact>
                  <includes>
                    <include>junit/framework/**</include>
                    <include>org/junit/**</include>
                  </includes>
                  <excludes>
                    <exclude>org/junit/experimental/**</exclude>
                    <exclude>org/junit/runners/**</exclude>
                  </excludes>
                </filter>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>

自动将所有不使用的类排除

       <executions>
          <execution>
            <configuration>
              <minimizeJar>true</minimizeJar>
            </configuration>
          </execution>
        </executions>

将依赖的类重命名并打包进来 (隔离方案)

           <configuration>
              <relocations>
                <relocation>
                  <pattern>io.netty</pattern>
                  <shadedPattern>shaded.io.netty</shadedPattern>
                  <excludes>
                  <exclude>org.appace</exclude>
                  </excludes>
                </relocation>
              </relocations>
            </configuration>

如果项目中用到了Spring Framework,将依赖打到一个jar包中,运行时会出现读取XML schema文件出错。原因是Spring Framework的多个jar包中包含相同的文件spring.handlersspring.schemas,如果生成一个jar包会互相覆盖。为了避免互相影响,可以使用AppendingTransformer来对文件内容追加合并:

<build>
	<plugins>
 
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-shade-plugin</artifactId>
			<version>2.4.1</version>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>shade</goal>
					</goals>
					<configuration>
						<transformers>
							<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
								<mainClass>com.xxg.Main</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>
 
	</plugins>
</build>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值