maven-打可执行jar包

在使用maven来管理项目时,项目除了web项目,还有可能为控制台程序,一般用于开发一些后台服务的程序(默认情况下,maven在做mvn pakage时,只是将项目编译打包到一个jar中,其他的类库则需要引用才行)。

用到 assembly插件

一.简单实用打jar包

在pom.xml中加入以下插件
 
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>cn.vsp.TestMain</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>assembly</goal>
            </goals>
        </execution>
    </executions>
</plugin>
上述代码中在<mainClass></mainClass>之间是填写程序的入口类,即含main方法的类
 
2.编辑完上述插件后,再执行
 
mvn assembly:assembly
这是就会在target目录下生成   *.jar 文件
 
3.运行jar文件
 
java -jar *.jar

二.详细应用插件配置

assembly  目前还支持zip,tar,tar.gz,tar.bz2,jar,dir,war格式

更多的插件配置需要 另外的配置:

使用maven assembly插件需要在pom文件中配置,添加这个插件

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>make-zip</id>
                        <!-- 绑定到package生命周期阶段上 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 绑定到package生命周期阶段上 -->
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors> <!--描述文件路径-->
                                <descriptor>src/assembly/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

 

其中execution节点,我们配置了执行maven assembly插件的一些配置,descriptor节点配置指向assembly.xml的路径。
在assembly.xml配置了,我们打包的目录以及相应的设置

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}\src\main\resources</directory>
            <outputDirectory>\</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}\src\bin</directory>
            <outputDirectory>\bin</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <!-- 将scope为runtime的依赖包打包到lib目录下。 -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets></assembly>

 

更多节点的用法可以去官网查询
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

猜你喜欢

转载自feiteyizu.iteye.com/blog/2236229