Maven打Jar包方法

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.csdn.net/bluishglc/article/details/7596118

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值