将java程序打包为一个可执行jar包的另一种方式

针对pom.xml修改

第一步,指定打包为jar包

<packaging>jar</packaging>

 第二步,build节点下使用maven-assembly-plugin

 <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>  <!--执行器 mvn assembly:assembly-->
                    <execution>
                        <id>xxx</id><!--名字任意 -->
                        <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
                        <goals>
                            <goal>single</goal><!-- 只运行一次 -->
                        </goals>
                        <configuration>
                            <descriptors> <!--描述文件路径-->
                                <descriptor>src/main/resources/zip.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

 第三步,在第二步指定的路径下,新建文件,zip.xml

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>release</id>
    <formats>
        <format>tar</format>  <!-- 打包的类型,可以是tar或者zip等类型-->
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}\src\main\resources</directory>
            <!-- 过滤 -->
            <excludes>
                <exclude>*.xml</exclude>
            </excludes>
            <outputDirectory>\</outputDirectory>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory><!-- 将scope为runtime的依赖包打包到lib目录下。 -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

 第四步:mvn clean package

第五部:解压tar文件,执行java -jar xx

猜你喜欢

转载自turbosky.iteye.com/blog/2274064