Maven打Jar包方法

Maven打包插件详解:从基础到进阶

1、maven-jar-plugin(默认的打包方式)

需设置Main入口,在pom.xml中添加如下配置:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>package.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

注意:此方法打出的Jar包不包含依赖的其他Jar包,可能会报类找不到的异常。

2、maven-shade-plugin(推荐)

使用方法:把下面的插件配置加入pom.xml中,修改mainClass。再使用mvn package命令打包即可。生成的jar包在target目录中。

<span style="white-space:pre">  </span><plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.wind.option.server.StartUp</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>

shade插件默认是能把配置文件也一起打到jar包中的,如果用mvnclean package会打不进来,在Eclipse里面clean然后用mvn package是可以的。

不知道为啥,暂时记一下。。。

3、maven-assembly-plugin

没用过,暂时记一下。用法与2相同。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <!-- get all project dependencies -->
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!-- MainClass in mainfest make a executable jar -->
        <archive>
          <manifest>
            <mainClass>com.mkyong.core.utils.App</mainClass>
          </manifest>
        </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
                            <!-- bind to the packaging phase -->
        <phase>package</phase> 
        <goals>
            <goal>single</goal>
        </goals>
      </execution>
    </executions>
</plugin>

4、maven-onejar-plugin(没用过)


注意:

如果出现找不到Spring配置项的问题,可参考

http://blog.youkuaiyun.com/bluishglc/article/details/7596118

打Jar包时,跳过测试代码的配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>


使用 MavenJAR 通常涉及在 `pom.xml` 文件中进行配置,下面详细介绍方法和配置: ### 基本配置 在 `pom.xml` 中添加 `maven-jar-plugin` 插件,以下是一个基本的配置示例: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.2</version> <configuration> <!-- 配置参数,如设置 JAR 文件的名称 --> <archive> <manifest> <!-- 设置主类,使 JAR 可执行 --> <mainClass>com.example.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> ``` 上述配置中,指定了 `maven-jar-plugin` 的版本,并且通过 `archive` 标签下的 `manifest` 标签设置了主类,这样生成的 JAR 可以直接通过 `java -jar` 命令执行。 ### 自定义 JAR 名称 可以通过 `finalName` 参数指定生成的 JAR 文件的名称: ```xml <configuration> <finalName>custom-jar-name</finalName> </configuration> ``` ### 含或排除文件 使用 `includes` 和 `excludes` 参数可以指定哪些文件需要含在 JAR 中,哪些文件需要排除: ```xml <configuration> <includes> <include>com/example/**</include> </includes> <excludes> <exclude>com/example/test/**</exclude> </excludes> </configuration> ``` ### 打带有依赖的可执行 JAR 要打带有依赖的可执行 JAR ,可以使用 `maven-shade-plugin` 或 `maven-assembly-plugin` 等插件。以 `maven-shade-plugin` 为例,配置如下: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.example.MainClass</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 配置完成后,在项目根目录下执行 `mvn clean package` 命令,Maven 会调用相应的插件将项目打JAR 文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值